0% found this document useful (0 votes)
32 views

c++ unit-5

Chapter 5 discusses constructors and destructors in C++. It explains the types of constructors, including default, parameterized, and copy constructors, along with their characteristics and usage. The chapter also covers destructors, which are used to clean up objects created by constructors, and provides several code examples to illustrate these concepts.

Uploaded by

PUSAT College
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

c++ unit-5

Chapter 5 discusses constructors and destructors in C++. It explains the types of constructors, including default, parameterized, and copy constructors, along with their characteristics and usage. The chapter also covers destructors, which are used to clean up objects created by constructors, and provides several code examples to illustrate these concepts.

Uploaded by

PUSAT College
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Chapter 5

Constructors & Destructors


Introduction to Constructors
A constructors is a special member function which initializes the objects of its class. It is
called special because its name is same as that of the class name. Constructor is
automatically executed whenever an object is created. Thus a constructor helps to initialize
the objects without making a separate call to a member function. It is called constructor
because it constructs values of a data member of a class.

A constructors that accepts no arguments (parameter) is called Default constructors. If


there is no default constructor defined, then the compiler supplies default constructors.
For Example 5.1
Class cons {
int data;
public:
cons(){ //default constructor having same name as that of class name
data=0;
}
};
For above example, if we don’t define any default constructor then the statement, cons c1
inside main() function invokes default constructor to create object c1.

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.

Example 5.2: Example 5.3:


#include<iostream.h> #include<iostream.h>
#include<conio.h> #include<conio.h>
class sample
{
class test {
int a,b;
int data;

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;
}

Constructor with parameter (Parameterized constructor)

C++ permits us to achieves this objectives by passing arguments to the constructors


function when the object are created. The constructors that takes arguments is called
parameterized constructors.

Syntax of parameterized constructors :


class class_name
{
Public:
class_name(argument){ // parameterized constructor declare
//body
} };
Example 5.4:
#include<iostream.h>
#include<conio.h>
class sample
{
int data;

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.

The initial value can be passed as arguments to constructor in two ways:-


i) By calling constructor explicitly:
Syntax:
class_name obj_name = class_name(argument) ;
e.g. Sample A = sample(5);
ii) By calling constructor implicitly:
syntax:
class_name obj_name(argument);
e.g. sample A(5);
Since the implicit call is more conventment ,it is more popular.

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;
}

In above program we can’t create object as follows. Xyz c1;


This is because in above program there is constructor and that is parameterized. We can
create object by above method if class xyz has no constructor or a constructor with no
argument. As already mentioned above, a constructor with no argument is called default
constructor.

Multiple Constructors in a class (Constructor Overloading)


C++ allows us to declare more than one constructor within a class definition. In this case
we
Say that the constructor is overloaded. All of the constructors can have different arguments
as required.

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:

class_name ( class_name & object_reference )

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

After copying date1 and date2:


Date2=7-4-200

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

You might also like