Constructor in C++
Constructor in C++
• Constructor is a member function of a class, whose name is same as the class name.
• Constructor is a special type of member function that is used to initialize the data members for an
object of a class automatically, when an object of the same class is created.
• Constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the
object that is why it is known as constructor.
• Constructor do not return value, hence they do not have a return type.
Constructor can be defined inside the class declaration or outside the class declaration
<class-name>(list-of-parameters)
{
//constructor definition
}
<class-name>: :<class-name>(list-of-parameters)
{
//constructor definition
}
#include<iostream>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student()
{
cout<<"Enter the RollNo:";
cin>>rno;
cout<<"Enter the Name:";
cin>>name;
cout<<"Enter the Fee:";
cin>>fee;
}
void display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
};
int main()
{
student s; //constructor gets called automatically when we create the object of the class
s.display();
return 0;
#include<iostream>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student();
void display();
};
student::student()
{
cout<<"Enter the RollNo:";
cin>>rno;
cout<<"Enter the Name:";
cin>>name;
cout<<"Enter the Fee:";
cin>>fee;
}
void student::display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
int main()
{
student s;
s.display();
return 0;
}
Characteristics of constructor
Types of constructor
• Default constructor
• Parameterized constructor
• Overloaded constructor
• Constructor with default value
• Copy constructor
• Inline constructor
Default Constructor or Zero argument constructor
A constructor that has zero parameter list or in other sense, a constructor that accept no arguments
is called a zero argument constructor or default constructor.
If default constructor is not defined in the source code by the programmer, then the compiler
defined the default constructor implicitly during compilation.
If the default constructor is defined explicitly in the program by the programmer, then the compiler
will not defined the constructor implicitly, but it calls the constructor implicitly.
Example:
#include<iostream>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student() // Explicit Default constructor
{
cout<<"Enter the RollNo:";
cin>>rno;
cout<<"Enter the Name:";
cin>>name;
cout<<"Enter the Fee:";
cin>>fee;
}
void display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
};
int main()
{
student s;
s.display();
return 0;
}
Parameterized constructor
Like other member function, constructors also take arguments. We can pass the desired initial values to the
data members of the objects of a class as arguments or parameters to constructors when the objects for that
class are created.
The constructor which can take arguments during the object creation are called parameterized constructor.
Example:
#include<iostream>
#include<string.h>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student(int,char[],double);
void display();
};
void student::display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
int main()
{
student s(1001,"Manjeet",10000);
s.display();
return 0;
}
Note: when parameterized constructor is defined and no default constructor is defined explicitly, the
compiler will not implicitly call the default constructor and hence creating a simple object as
Student s;
Will flash an error
Copy Constructor
Copy constructor is used to initialize the members of a newly created object by copying the
members of an already existing object.
Copy constructor takes a reference to an object of the same class as an argument.
Sample(Sample &t)
{
id=t.id;
}
The process of initializing members of an object through a copy constructor is known as copy
initialization.
It is also called member-wise initialization because the copy constructor initializes one object with
the existing object, both belonging to the same class on a member by member copy basis.
The copy constructor can be defined explicitly by the programmer. If the programmer does not
define the copy constructor, the compiler does it for us.
class Sample
{ int id;
public:
void init(int x)
{
id=x;
}
void display()
{
cout<<endl<<"ID="<<id;
}
};
int main()
{
Sample obj1;
obj1.init(10);
obj1.display();
obj2.display();
return 0;
}
class Sample
{
int id;
public:
void init(int x)
{
id=x;
}
Sample(){} //default constructor with empty body
#include<iostream>
#include<string.h>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student(int,char[],double);
student(student &t) //copy constructor
{
rno=t.rno;
strcpy(name,t.name);
fee=t.fee;
}
void display();
};
int main()
{
student s(1001,"Manjeet",10000);
s.display();
return 0;
}
#include<iostream>
#include<string.h>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student(int,char[],double);
student(student &t) //copy constructor (member wise initialization)
{
rno=t.rno;
strcpy(name,t.name);
}
void display();
void disp()
{
cout<<endl<<rno<<"\t"<<name;
}
};
student::student(int no, char n[],double f)
{
rno=no;
strcpy(name,n);
fee=f;
}
void student::display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
int main()
{
student s(1001,"Manjeet",10000);
s.display();
return 0;
}
Output
Destructor
Destructor is also a special member function like constructor. Destructor destroys the class objects
created by constructor.
Destructor has the same name as their class name preceded by a tiled (~) symbol.
It is not possible to define more than one destructor.
The destructor is only one way to destroy the object create by constructor. Hence destructor can-
not be overloaded.
Destructor neither requires any argument nor returns any value.
It is automatically called when object goes out of scope.
Destructor release memory space occupied by the objects created by constructor.
In destructor, objects are destroyed in the reverse of an object creation.
Example:
class Test
{
public:
Test()
{
cout<<"\n Constructor executed";
}
~Test()
{
cout<<"\n Destructor executed";
}
};
main()
{
Test t;
return 0;
}
#include<iostream>
using namespace std;
class Test
{
public:
Test()
{
cout<<"\n Constructor executed";
}
~Test()
{
cout<<"\n Destructor executed";
}
};
main()
{
Test t,t1,t2,t3;
return 0;
}
#include<iostream>
using namespace std;
int count=0;
class Test
{
public:
Test()
{
count++;
cout<<"\n No. of Object created:\t"<<count;
}
~Test()
{
cout<<"\n No. of Object destroyed:\t"<<count;
--count;
}
};
main()
{
Test t,t1,t2,t3;
return 0;
}
Characteristics of a destructor
1. Destructor is invoked automatically by the compiler when its corresponding constructor goes out
of scope and releases the memory space that is no longer requires by the program.
2. Destructor neither requires any argument nor returns any value therefore it cannot be overloaded.
3. Destructor cannot be declared as static and const;
4. Destructor should be declared in the public section of the program.
5. Destructor is called in the reverse order of its constructor invocation.
Q: What are the functions that are generated by the compiler by default, if we do not provide them
explicitly?
Ans:
I. Default constructor
II. Copy constructor
III. Assignment operator
IV. Destructor