Dynamic Memory Allocation For Object and Object Array
Dynamic Memory Allocation For Object and Object Array
This is the mechanism of allocating memory dynamically for object and object array
Syntax:
Class_name *object_pointer;
Dynamic constructor
1.WAP with class named BOOK ,which will represent title,author ,price ,publisher.use constructor for
initialization of an objects and use dynamic memory allocation for string type member and destructor in
a proper way .(8)
class book
char *title;
char *author ;
float price;
public:
book(){
Declared inside the class body and also known as class member variable
There will be only one copy of static member variable for whole class
//
//class Sample{
//public:
//};
//int Sample::y=10;
//int main()
//{
// cout<<Sample::y<<endl;
// Sample s1;
//
// cout<<s1.y;
// cout<<endl<<endl;
// return 0;
//}
//#include <iostream>
//class Sample{
//private:
// int x;
// static int y;
//public:
// cout<<y<<endl;
// }
//};
//int Sample::y=10;
//int main()
//{
// Sample::display();
//
// Sample s1;
// s1.display();
// return 0;
//#include<iostream>
//class Circle{
//float r;
//public:
// Circle(float a):r(a){}
// void area(){
//};
//int main(){
//Circle c1(2);
//c1.area();
//return 0;
//}
//#include <iostream>
//
//class Laptop{
// string Lname;
//public:
// Laptop(string name):Lname(name){}
// }
//};
//int main(){
// Laptop L1("Dell"),L2("Acer");
// L1.display();
// L2.display();
// return 0;
//
Function overloading
Void add(int,int)
Void add(int,int)
Void add(int,int,int)
Default argument
Inline function
#include <iostream>
return x * x;
int main() {
int a = 5;
int b = 10;
std::cout << "The square of " << a << " is " << square(a) << std::endl;
std::cout << "The square of " << b << " is " << square(b) << std::endl;
return 0;}
friend class and friend function
A friend function of a class is defined outside that class scope but it has the right to access all
Friend function can become friend to more than one class.( it is unique behavior of friend
function ).
Sample Program:
#include <iostream>
class Sample{
int x;
public:
Sample(){
x=0;
}
void display(){
friend void add(Sample &s); //friend function's prototype. //pass by reference is opt.
};
s.x=s.x+4;
int main()
Sample s1;
s1.display();
s1.display();
return 0;
Your Work:
1. WAP to find sum of two distance (feet, inch) objects using friend function.
A friend class can access private and protected members of other class in which it is declared
as friend. It is sometimes useful to allow a particular class to access private members of other
class.
Following are some important points about friend functions and classes:
automatically.
#include <iostream>
class A {
int x;
public:
A(){
x=0;
friend class B;
};
class B{
public:
cout<<"Value of x: "<<a.x<<endl;
};
int main() {
A a1;
B b1;
b1.display(a1);
return 0;
Your Work:
1. Create a class mdistance to store the values in meter and centimeter and class edistance to store
values in feet and inches. Perform addition of object of mdistance and object of edistance by using
friend function