In C++, Constructor is used to initialize the variables of the class and to allocate memory. Constructor is similar to functions but with some differences.
Characteristics of Constructor:
In every C++ program there is a constructor known as default constructor. We can also create a default constructor which is parameterless.
Example 1: Default constructor. 1531
class example { public: example() { cout<<"Default Constructor Called"; } }; void main() { clrscr(); example ex; // default constructor called getch(); }
In constructor, we can pass parameters same as we did in functions.
Example 2: WAP to find Sum of 2 numbers using parameterized constructor. 308
class sum { int s; public: sum(int a, int b) { s=a+b; cout<<"Sum = "<<s; } }; void main() { int a,b; clrscr(); cout<<"Enter 1st Number: "; cin>>a; cout<<"Enter 2nd Number: "; cin>>b; sum s(a, b); // constructor called with 2 parameters getch(); }
In Constructor overloading, we can create multiple constructors that have same name but different parameters.
Example 3: WAP to calculate Area of Circle & Rectangle using constructor overloading. 233
class area { float a, a2; public: area(float r) { a = 3.14f * r * r; cout<<endl<<"Area of Circle = "<<a; } area(int l, int b) { a2 = l*b; cout<<"\nArea of Rectangle = "<<a2; } }; void main() { float r; int l, b; clrscr(); cout<<"Enter Radius: "; cin>>r; cout<<"Enter length: "; cout<<"Enter breadth: "; cin>>l; cin>>b; area x(r); area y(l,b); getch(); }
In parameterized constructor we have to match its arguments, but in default argument we can omit the argument value, In that case constructor takes default value of that variable assigned in the body of the constructor. There can be one or more default arguments in a constructor.
Example 4: WAP to find Simple Interest using constructor with default argument. 367
class si { float s; public: si(float p, float t, float r=10.5f) { s = p*r*t/100; cout<<endl<<"Simple Interest = "<<s; } }; void main() { float p, r, t; clrscr(); cout<<"Enter Principle: "; cout<<"Enter Rate: "; cout<<"Enter Time: "; cin>>p; cin>>r; cin>>t; si obj(p,t,r); si obj2(p,t); getch(); }
Copy constructor is used to copy values of one object into another object.
Example 5: Copy Constructor. 294
class copy { int a; public: copy() //default constructor { a=10; } copy(copy &obj1) //copy constructor { a = obj1.a; } void output() { cout<<endl<<"Value of A = "<<a; } }; void main() { clrscr(); copy obj1; //constructor called copy obj2 = obj1; //copy constructor called obj1.output(); obj2.output(); getch(); }
C++ Destructor is used to destroy the objects that have been created by a constructor, when they are no longer required. Destructor is used to clean up the memory storage.
Note: new keyword is used to allocate memory, and delete keyword is used to free that memory.
Rules to declare a Destructor
Example 6: Destructor. 306
class example { public: example { cout<<"Constructor Called"; } ~example { cout<<"Destructor Called"; } }; void main() { clrscr(); example obj; { example obj2; } { example obj3; } getch(); }