c++ unit-5
c++ unit-5
Characteristics of constructors
Constructor has the same name as that of its class name.
Generally it should be declared in public section of the class.
It is invoked automatically when object are created.
It can not return any value because it doesn’t have a return type not even void.
It can have default arguments.
It can not be a virtual function and we cannot refer to its address.
It cannot be a inherited.
It can be overloading
It makes implicit calls to operators new and delete when memory allocation is
required.
1
public: public:
test() { //default constructor sample(){ //default constructor
data=0; cout<<"this is inside constructer"<<endl;
a=10;
}
b=20;
void increment(){ }
data++; int add(){
} return(a+b);
void show() }
{ };
cout<<”\ndata=”<<data; int main(){
clrscr();
}
sample s; //default constructor called
}; cout<<"output is:"<<s.add()<<endl;
int main(){ getch();
test c1; //invokes default constructor return 0;
automatically to create object c1. }
c1.show();
c1.increment();
c1.show();
getch();
return 0;
}
2
public:
sample(int c) //parameterized constructor
{
data=c;
}
void show(){
cout<<"\ndata="<<data;
}
};
int main(){
clrscr();
sample A(5); // sample A= sample(5); // create object A and passes value 5 to A
A.show();
getch();
return 0; Output:
}
data=5
When the constructor has been parameterized, we must pass the initial values as a
arguments to the constructor function when object statement declared. In the above
example, the statement sample A(5) invoke the parameterized constructor and passes value
5 to it.
Example 5.5:
Write a program which read two number ; initialize an object with these two numbers
and then find out multiplication of these two number .
3
#include<iostream.h>
#include<conio.h>
class xyz
{
int a,b;
public:
xyz(int x ,int y){
a=x;
b=y;
}
int mult() {
return(a*b);
}
};
int main() {
int i,j;
clrscr();
cout<<”\nEnter first no:”;
cin>>I;
cout<<”\nEnter second no:”;
xyz c1(I,j);
cout<<”\nMultiplication is:”<<c1.mult()<<endl;
getch();
return 0;
}
4
Example 5.6:
class xyz {
int x, y, z;
public:
xyz()
{ x=0; y=0; z=0; }
xyz (int x1, int y1)
{ x =x1; y=y1; z=0; }
Xyz(int x1, int y1; int z1)
{ x=x1; y=yl; z=z1; }
};
In above example, we have declared three constructor in class xyz. The first constructor
receives no argument, second receives two arguments and third receives three arguments.
Now we can create three different types of objects for the class xyz like,
1. xyz a1;
2. xyz a2 (2,3);
3. xyz a3 (1,5,6);
Here object a1 automatically invokes the constructor which has no argument, so x, y and z
of object a1 are initialized by value zero (0).
In object a2 has two argument, so the value of x, y, z are 2, 3 & 0.
In object a3 which has three argument, so the value of x, y, z are 4,5 and 6.
Thus more than one constructor is possible in a class. Note that sharing the same name by
two or more functions is called function overloading. Similarly when more than one
constructor is defined in a class this is known as constructor overloading.
Copy Constructor
Copy constructor are used to copy the data elements of one object to the corresponding data
element of another object. The header of copy constructor is written as follows:
e.g.
abc (abc & c)
{
x=c.x;
y=c.y;
}
In above example abc is class name and c is reference object.
5
Example 5.7:
class cons{
int data;
public:
cons(int c){ // parameterized constructor
data =c;
}
cons ( cons &a) // copy constructor
{
data=a.data;
}
void display(){
cout<<data;
}
};
int main(){
cons A(5);
cons B(A); // or cons B=A;
cout<<”\n data in A:”; Output:
A.display();
cout<<”\n data in B:”; data in A :5
B.display(); data in B: 5
getch();
return();
}
In above program, the statement cons A(5); calls the first constructor (parameterized) and
copies value 5 or data for object A then the statement cons B(A); invokes second
constructor.
Destructor
A destructor is used to destroy the objects that have been created by constructor. A
destructor is a special member function like constructor. The name of destructor is same as
constructor (i.e. class_name) but it is preceded by a tilde (~).
Example: ~cons() { }
A destructor never takes arguments and return value. Destructor will automatically be
called by compiler upon exit from the program to clean up storage taken by objects. The
objects are destroyed in the reverse order from their creation.
Example 5.8:
6
#include<iostream.h>
#include<conio.h>
class test {
int data;
public: test(int c){
data=c;
cout<<”\n Inside constructor,”;
cout<<”data=”<<data;
}
~test(){
Cout<<”\n Memory Freed”; Output:
}
}; Inside constructor, data=5
int main() { Inside constructor, data=6
clrscr();
test A(5); Memory Freed
{ Inside constructor, data=6
test B(6);
Memory Freed
}
test C(6): Memory Freed
getch();
return ();
}
In the above example, the constructor is called to make object A and value 5 is passed to it.
Again object B is made and constructor called. The scope of object B is defined by the curly
braces. So the object B will be destroyed immediately after its creation. Then object C is
created thereby the constructor is called again. Up to this time all three objects have been
created. Now the object C will be destroyed and this will be done by calling a destroyer.
Since B has already been destroyed, the last object left to be destroyed is object A. In this
way, the objects A,B and C are destroyed in the reverse order of their creation.
7
Example 5.9:
WAP that generate a date class having constructor and which prints following output
Date1=7-4-2006
Date2=1-1-2005
Solution
#include<iostream.h>
#include<conio.h>
class date{
public:
date(int m=1, int d=1, int y=2005);
void print();
private: int mm,dd,yy;
};
date::date(int m, int d, int y){
mm=m;
dd=d;
yy=y;
}
void date:: print(){
cout<<mm<<”-“<<dd<<”-“<<yy;
}
int main(){
clrscr();
date date1(7,4,2006), date2;
cout<<”\n date1=”; Outputt:
date1.print();
Date1=7-4-2006
cout<<”\n date2=”;
date2=date1; Date2=1-1-2005
cout<<”\n \n after copying date1 and date2:”; After copying date1 to date2:
cout<<”\n date2=”;
date2.print(); Date2=7-4-2006
gatch();
return 0;
}
8
9