Professional Documents
Culture Documents
Contents
• Constructors
• Parameterized constructors
• Constructor overloading.
• Copy constructor,
• Destructors,
• Default arguments
• Dynamic memory allocation- new, delete operators
• This pointer
• Friend classes and Friend functions.
constructor
• A class constructor is a special member function of a class
that is executed whenever we create new objects of that
class.
• A constructor will have exact same name as the class
ceases to exist.
destructors are called for all objects which exist at that time.
DESTRUCTOR
•Similar to constructors, a destructor must be declared in the public section of
a class so that it is accessible to all its users.
•Destructors have no return type.
•It is incorrect to even declare a void return type.
•A class cannot have more than one destructor
DESTRUCTOR
#include <iostream.h>
class Test
{
public: // ‘public’ function:
Test(); // the constructor
~Test(); // the destructor
};
Test::Test() // here is the definition of constructor
{
cout << “constructor of class Test called” << endl;
}
Test::~Test() // here is the definition of destructor
{
cout << “destructor of class Test called” << endl;
}
void main() Output:
{ constructor of class Test called
Test x; // constructor is called while creating terminating main()
cout << “terminating main()” << endl; destructor of class Test called
} // object x goes out of scope, destructor is called
CONSTRUCTOR OVERLOADING
• a class can have multiple constructors. This is
called constructor overloading.
• All the constructors have the same name as the
corresponding class, and they differ only in terms
of their signature.
• In terms of the number of arguments, or data
types of their arguments, or both
CONSTRUCTOR OVERLOADING
means providing space for the variable. The size and data type of the
size of the variable is not known in advance. Pointers play a major role
For example
Shallow Copy
• The default copy constructor can only produce the shallow copy.
Demo d2 = d1;
•Thus, the pointer p of both the objects point to the same memory location.
•Therefore, when the memory of a field is freed, the memory of another field is
also automatically freed as both the fields point to the same memory location.
Deep copy
• Deep copy dynamically allocates the memory for the copy
and then copies the actual value, both the source and copy
have distinct memory locations.
• In this way, both the source and copy are distinct and will
not share the same memory location.
•It creates the exact copy of the value types data and the
object pointed by the pointer p.
•Deep copy does not create the copy of a reference type
variable.
Deep copy
#include <iostream> void setdata(int x,int y,int z)
using namespace std; {
class Demo a=x;
{ b=y;
public: *p=z;
int a; }
int b; void showdata()
int *p; {
Demo() std::cout << "value of a is : " <<a<< std::endl;
{ std::cout << "value of b is : " <<b<< std::endl;
p=new int; std::cout << "value of *p is : " <<*p<< std::endl;
} }
Demo(Demo &d) };
{ int main()
a = d.a; {
b = d.b; Demo d1;
p = new int; d1.setdata(4,5,7);
*p = *(d.p); Demo d2 = d1;
} d2.showdata();
return 0;
}
FRIEND FUNCTIONS AND FRIEND CLASSES