1. Copy Constructor
It is used to declare and initialized an object from
another object.
It takes reference to an object of the same class as
itself as an arguments.
Example:
integer I1;
integer I2(I1);
5. In C++, We can have more than one
constructor in a class with same name,
as long as each has a different list of
arguments?
6. When is user defined copy constructor needed?
• Default copy constructor for each class which does a
member wise copy between objects
• Define our own copy constructor only if an object has
pointers or any run time allocation of resource like file
handle, a network connection..etc.
9. Copy constructor vs Assignment Operator
MyClass t1, t2;
MyClass t3 = t1; // ----> (1)
t2 = t1; // -----> (2)
Copy constructor is called when a new object is created from an
existing object, as a copy of the existing object.
Assignment operator is called when an already initialized object
is assigned a new value from another existing object.
In the above example (1) calls copy constrictor and
(2) calls assignment operator.
10. Can we make copy constructor private?
class A
{
private:
int x;
A() { x=7; cout<<“Constructor called”; }
public:
void show()
{
A();
cout<< “X is”<<x;
}
};
main()
{
A *a;
a->show();
}
11. Dynamic Constructors
• The constructor can also be used to allocate memory
while creating objects.
• To allocate the right amount of memory for each
object.
• Allocation of memory to objects at the time of their
construction is known as dynamic construction of
objects.
12. Example
int number()
{
for(int h=0;h<s;h++)
{
int n;
cout<<"enter number";
cin>>n;
return n;
}
}
void input()
{
for(int h=0;h<s;h++)
{
cout<<"enter number";
cin>>x[h];
}
}
class num
{
private:
int *x,s;
public:
num()
{
s=number();
x=new int[s]; }
void sum()
{
int adi=0;
for(int h=0;h<s;h++)
adi+=x[h];
cout<<"Sum is"<<adi;
}
};
main()
{
num n1;
n1.input();
n1.sum();
}
13. Dynamic Initialization of Objects
class construct
{
public:
float area;
construct()
{
area = 0;
}
construct(int a, int b)
{
area = a * b;
}
void disp()
{
cout<< area<< endl;
}
};
int main()
{
int x,y;
construct o;
cout<<“Enter variable values”;
cin>>x>>y;
construct o2(x,y);
o.disp();
o2.disp();
return 0;
}
15. Sample Program
class construct
{
public:
float area;
// Constructor with no parameters
construct()
{
area = 0;
}
// Constructor with two parameters
construct(int a, int b)
{
area = a * b;
}
void disp()
{
cout<< area<< endl;
}
};
int main()
{
// Constructor Overloading
// with two different constructors
construct o;
construct o2( 10, 20);
o.disp();
o2.disp();
return 0;
}
16. class Demo
{
private:
int X,Y;
public:
Demo()
{
X = 0; Y = 0;
}
Demo(int x, int y=20)
{
X = x; Y = y;
}
void putValues()
{
cout << endl << "Value of X : " << X;
cout << endl << "Value of Y : " << Y
}
};
int main()
{
Demo d1= Demo(10);
cout << endl <<"D1 Value Are : ";
d1.putValues();
Demo d2= Demo(30,40);
cout << endl <<"D2 Value Are : ";
d2.putValues();
return 0;
}
Constructors with default arguments
17. Const member functions in C++
A function becomes const when const keyword is used
in function’s declaration.
The idea of const functions is not allow them to
modify the object on which they are called
18. Local Classes
A class declared inside a function becomes local to that
function and is called Local Class in C++.
void fun()
{
class Test // local to fun
{
/* members of Test class */
};
}
int main()
{
return 0;
}
19. Interesting facts of local class
1) A local class type name can only be used in
the enclosing function.
20. 2) All the methods of Local classes must be
defined inside the class only
21. • A Local class cannot contain static data members.
It may contain static functions though
• Member methods of local class can only access
static and enum variables of the enclosing
function.
• Local classes can access global types, variables
and functions. Also, local classes can access other
local classes of same function.
22. Pointer to Members
A::* denotes pointer of member of class A
&A::m Address of m member of class A
24. Pointer to member function
return_type (class_name::*ptr_name) (argument_type) =
&class_name::function_name ;
#include<iostream>
using namespace std;
class Data
{ public:
void f () { cout<<"hi"; }
};
int main()
{
Data d;
void (Data::*fp1) () = &Data::f;
(d.*fp1)();
}
void (Data::*fp1) () = &Data::f;
25. Destructor
It is used to destroy the objects created by the
constructor.
It is called for the class object whenever it passes the
scope in the program.
Whenever new is used in the constructor to allocate
the memory delete should be used in the destructor
to free the memory for future use.
26. Characteristics of Destructor
It has same name as the class name but is preceded
by tilde (~) sign.
It has no return type and do not take any arguments.
It can not be overloaded.
It is called whenever the object get out of its scope.
27. Sample Program
#include<iostream.h>
class integer
{
int m,n;
public:
integer()
{
m=0;
n=0;
cout<<"Default
Constructor is
called"<<endl;
}
integer(int x, int y)
{
m=x;
n=y;
cout<<"Parameterize
d Constructor is
called"<<endl;
}
~integer()
{
cout<<"Object is
detroyed"<<endl;
}