Constructor Destructor
Constructor Destructor
EXAMPLE 1:
string dept;
department ()
{
dept=”department name”;
}
EXAMPLE 2:
string dept;
department (string d)
{
dept=d;
}
DEFAULT CONSTRUCTOR:
A default constructor is a constructor that doesn’t take any argument. It has no parameters. It is
also called a zero-argument constructor. A constructor without any arguments or with the default
value for every argument is said to be the default constructor.
A constructor that has zero parameter list or in other sense, a constructor that accepts 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
defines the default constructor implicitly during compilation.
If the default constructor is defined explicitly in the program by the programmer, then the
compiler will not define the constructor implicitly, but it calls the constructor implicitly.
PROGRAM:
Constructor definition inside the class
#include <iostream>
using namespace std;
class student
{
public string dept;
public:
student()
{
dept=”department name”;
cout<<"student department : "<<dept<<endl;
}
};
int main()
{
student s1; //creating an object of employee
student s2;
return 0;
}
PARAMETERIZED CONSTRUCTOR:
Parameterized constructors make it possible to pass arguments to constructors.
Typically, these arguments help initialize an object when it is created. To create a
parameterized constructor, simply add parameters to it the way you would to any other
function. When you define the constructor’s body, use the parameters to initialize the
object.
or
classname (arg1, arg2, arg3, ….., argn) {
// body of parameterized constructor
}
EXAMPLE 1:
student (string name, int age, string dept)
{
cout<<”name : “<<name<<endl;
cout<<”age : ”<<age<<endl;
cout<<”department : “<<dept<<endl;
}
EXAMPLE 2:
string name;
int age;
string dept;
student (string n, int a, string d)
{
name=n;
age=a;
dept=d;
cout<<”name : “<<name<<endl;
cout<<”age : ”<<age<<endl;
cout<<”department : “<<dept<<endl;
}
PROGRAM:
#include <iostream>
using namespace std;
class employee {
public:
int id; //data member
string name; //data member
float salary;
employee(int i, string n, float s)
{
id = i;
name = n;
salary = s;
}
void display()
{
cout<<”id : ”<<id<<”name : “<<name<<”salary : ”<<salary<<endl;
}
};
int main( ) {
employee e1 =employee(101, "sonoo", 890000);
employee e2=employee(102, "nakul", 59000);
e1.display();
e2.display();
return 0;
}
COPY CONSTRUCTOR:
A member function known as a copy constructor initializes an item using another object from
the same class-an in-depth discussion on copy constructors.
Every time we specify one or more non-default constructors (with parameters) for a class, we
also need to include a default constructor (without parameters), as the compiler won't supply
one in this circumstance. The best practice is to always declare a default constructor, even
though it is not required.
A reference to an object belonging to the same class is required by the copy constructor.
EXAMPLE 1:
sample(sample &t)
{
id=t.id;
}
PROGRAM:
#include <iostream>
using namespace std;
class student {
int rno;
string name;
double fee;
public:
student(int, string, double);
// copy constructor
student(student& t)
{
rno = t.rno;
name = t.name;
fee = t.fee;
cout << "copy constructor called" << endl;
}
// function to display student details
void display();
};
int main()
{
// create student object with parameterized constructor
student s(1001, "manjeet", 10000);
s.display();
return 0;
}
CONSTRUCTOR OVERLOADING
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. This concept is known as constructor overloading and is quite
similar to function overloading.
• Overloaded constructors essentially have the same name (exact name of the class) and
different by number and type of arguments.
• A constructor is called depending upon the number and type of arguments passed.
• While creating the object, arguments must be passed to let compiler know, which
constructor needs to be called.
void disp()
{
cout<<”output here”;
}
};
int main()
{
classname obj1;
classname obj2( val1,val2);
obj1.disp();
obj2.disp();
return 1;
}
PROGRAM:
// c++ program to illustrate
// constructor overloading
#include <iostream>
using namespace std;
class construct
{
public:
float area;
void disp()
{
cout<< area<< endl;
}
};
int main()
{
// constructor overloading
// with two different constructors
// of class name
construct o;
construct o2( 10, 20);
o.disp();
o2.disp();
return 1;
}
DESTRUCTORS
Destructor is an instance member function that is invoked automatically whenever an object is
going to be destroyed. Meaning, a destructor is the last function that is going to be called before
an object is destroyed.
In this article, we will learn about the destructors in c++, how they work, how and why to create
the user defined destructors with the code examples. At the end, we will look at some
commonly used questions about c++ destructors.
Program:
// C++ program to demonstrate the execution of constructor
// and destructor
#include <iostream>
using namespace std;
class Test {
public:
// User-Defined Constructor
Test() { cout << "\n Constructor executed"; }
// User-Defined Destructor
~Test() { cout << "\nDestructor executed"; }
};
main()
{
Test t;
return 0;
}
Output
Constructor executed
Destructor executed
Program 2:
// C++ program to demonstrate the number of times
// constructor and destructors are called
#include <iostream>
using namespace std;
// It is static so that every class object has the same
// value
static int Count = 0;
class Test {
public:
// User-Defined Constructor
Test()
{
// Number of times constructor is called
Count++;
cout << "No. of Object created: " << Count << endl;
}
// User-Defined Destructor
~Test()
{
// It will print count in decending order
cout << "No. of Object destroyed: " << Count
<< endl;
Count--;
// Number of times destructor is called
}
};
// driver code
int main()
{
Test t, t1, t2, t3;
return 0;
}
Output
No. of Object created: 1
No. of Object created: 2
No. of Object created: 3
No. of Object created: 4
No. of Object destroyed: 4
No. of Object destroyed: 3
No. of Object destroyed: 2
No. of Object destroyed: 1
#include <iostream>
using namespace std;
// base class
class Parent
{
public:
// sub class
class Child : public Parent
{
public:
// main function
int main() {
return 0;
}
// base class
class Parent {
int x;
public:
// base class's parameterized constructor
Parent(int i)
{
x = i;
cout << "Inside base class's parameterized "
"constructor"
<< endl;
}
};
// sub class
class Child : public Parent {
public:
// sub class's parameterized constructor
int z;
Child(int x): Parent(x),z(x+10)
{
cout << "Inside sub class's parameterized "
"constructor"
<< z<<endl;
}
};
// main function
int main()
{