constructor and destructor - example
constructor and destructor - example
1. Default Constructor
2. Parametrized Constructor
3. Copy COnstructor
Default Constructors
Default constructor is the constructor which doesn't take any argument. It has no parameter.
Syntax:-
// constructor Definition
Example :-
class Cube
{
int side;
public:
Cube()
{
side=10;
}
};
int main()
{
Cube c;
cout << c.side;
}
O/P :-
A default constructor is so important for initialization of object members,
Example :-
class Cube
{
public:
int side;
};
int main()
{
Cube c;
cout << c.side;
}
which will initialize the object data members to default value, that will be 0
class Cube
{
public:
int side;
Cube(int x)
{
side=x;
}
};
int main()
{
Cube c1(10);
Cube c2(20);
Cube c3(30);
class Point {
private:
int x, y;
public:
// Parameterized Constructor
Point(int x1, int y1)
{
x = x1;
y = y1;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
};
int main()
{
// Constructor called
Point p1(10, 15);
return 0;
}
Copy Constructors
C++ provides a special type of constructor which takes an object as an
argument and is used to copy values of data members of one object into
another object. In this case, copy constructors are used to declaring and
initialization
Example :-
Calc C2(C1);
Or
Calc C2 = C1;
Syntax:-
class-name (class-name &)
{
...
}
#include <iostream>
using namespace std;
class CopyCon
{
int a, b;
public:
CopyCon(int x, int y)
{
a = x;
b = y;
cout << "\nHere is the initialization of Constructor";
}
void Display()
{
cout << "\nValues : \t" << a << "\t" << b;
}
};
main()
{
CopyCon Object(30, 40);
//Copy Constructor
CopyCon Object2 = Object;
Object.Display();
Object2.Display();
}
class Example
{
int a, b;
public:
//Normal Constructor with Argument
Example(int x, int y)
{
// Assign Values In Constructor
a = x;
b = y;
cout << "\nI m Constructor";
}
//Copy Constructor with Obj Argument
Example( const Example& obj)
{
// Assign Values In Constructor
a = obj.a;
b = obj.b;
cout << "\nIm Copy Constructor";
}
void Display()
{
cout << "\nValues :" << a << "\t" << b;
}
};
int main()
{
//Normal Constructor Invoked
Example Object(10, 20);
Object.Display();
Object2.Display();
Object3.Display();
getch();
return 0;
}
/*C++ program to demonstrate example of Copy Constructor.*/
#include <iostream>
using namespace std;
//Class declaration.
class Demo
{
//Private block to declare data member( X,Y ) of
integer type.
private:
int X;
int Y;
};//End of class
int main()
{
Demo d1(10,20) ; //Ctor automatically call when object
is created.
return 0;
}
#include<iostream>
using namespace std;
class A
{
int x,y,z;
public:
A(int a=0,int b=0,int c=0)
{
x=a;y=b;z=c;
}
void putdata()
{
cout<<"\nX: "<<x<<"\nY: "<<y<<"\nZ: "<<z;
}
};
int main()
{
A a;
a.putdata();
A b(10,15,20);
b.putdata();
A c(12,45);
c.putdata();
}
Constructor Overloading in C++
(Multiple Constructors in a Class)
class Student
{
int rollno;
string name;
public:
// first constructo
Student(int x)
{
rollno=x;
name="None";
}
// second constructor
Student(int x, string str)
{
rollno=x ;
name=str ;
}
};
int main()
{
// student A initialized with roll no 10 and name None
Student A(10);
~Cube()
The destructor neither takes an argument nor returns any value and the
compiler implicitly invokes upon the exit from the program for cleaning
up storage that is no longer accessible.
(i) These are called automatically when the objects are destroyed.
(ii) Destructor functions follow the usual access rules as other
member functions.
(iii) These de-initialize each object before the object goes out of
scope.
(iv) No argument and return type (even void) permitted with
destructors.
(v) These cannot be inherited.
(vi) Static destructors are not allowed.
(vii) Address of a destructor cannot be taken.
(viii) A destructor can call member functions of its class.
(ix) An object of a class having a destructor cannot be a member of
a union.
Constructor