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

constructor and destructor - example

The document outlines the different types of constructors in C++, including Default, Parameterized, and Copy Constructors, along with their definitions and examples. It explains the purpose of each constructor type, how they initialize objects, and provides code snippets to illustrate their usage. Additionally, it discusses destructors, their characteristics, and the concept of dynamic constructors for memory allocation at runtime.

Uploaded by

23it062
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)
11 views

constructor and destructor - example

The document outlines the different types of constructors in C++, including Default, Parameterized, and Copy Constructors, along with their definitions and examples. It explains the purpose of each constructor type, how they initialize objects, and provides code snippets to illustrate their usage. Additionally, it discusses destructors, their characteristics, and the concept of dynamic constructors for memory allocation at runtime.

Uploaded by

23it062
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/ 19

Types of Constructors in C++

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:-

class_name(parameter1, parameter2, ...)

// 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,

that even if we do not define a constructor explicitly, the compiler will

provide a default constructor implicitly.

Example :-

class Cube
{
public:
int side;
};

int main()
{
Cube c;
cout << c.side;
}

In this case, default constructor provided by the compiler will be called

which will initialize the object data members to default value, that will be 0

or any random integer value in this case.


Example :-

For example, the following program illustrates the concept of a constructor :


//To demonstrate a constructor
#include <iostram.h>
#include <conio.h>
Class rectangle
{
private :
float length, breadth;
public:
rectangle ()//constructor definition
{
//displayed whenever an object is created
cout<<”I am in the constructor”;
length=10.0;
breadth=20.5;
}
float area()
{
return (length*breadth);
}
};
int main()
{

rectangle rect; //object declared


cout<<”\nThe area of the rectangle with default parameters
is:”<<rect.area()<<”sq.units\n”;
return 0;
}
Parameterized Constructors
These are the constructors with parameter. Using this Constructor you can
provide different values to data members of different objects, by passing
the appropriate values as argument.

class Cube
{
public:
int side;
Cube(int x)
{
side=x;
}
};

int main()
{
Cube c1(10);
Cube c2(20);
Cube c3(30);

cout << c1.side;


cout << c2.side;
cout << c3.side;
}
#include <iostream>
using namespace std;

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

// Access values assigned by constructor


cout << "p1.x = " << p1.getX() << ", p1.y = " <<
p1.getY();

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

initializing an object from another object.

The process of initializing through a copy constructor is called the copy

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

//Copy Constructor Invoked - Method 1


Example Object2(Object);

//Copy Constructor Invoked - Method 2


Example Object3 = Object;

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;

//Public block of member function to access data


members.
public:
//Declaration of parameterized constructor to
initialize data members.
Demo (int a, int b);
//Declaration of copy constructor to initialize data
members.
Demo (const Demo &d);
//To display output on screen.
void Display();

};//End of class

//Definition of parameterized constructor.


Demo:: Demo(int a, int b)
{
X = a;
Y = b;
}

//Definition of copy constructor.


Demo:: Demo(const Demo &d)
{
X = d.X;
Y = d.Y;
}

//Definition of Display () member function.


void Demo:: Display()
{
cout << endl << "X: " << X;
cout << endl << "Y: " << Y << endl;
}

int main()
{
Demo d1(10,20) ; //Ctor automatically call when object
is created.

//Display value of data member.


cout << endl <<"D1 Object: " << endl;
cout << "Value after initialization : " ;
d1.Display();

//Intialize object with other object using copy


constructor
Demo d2 = Demo(d1);//also write like this :Demo d2(d1);

//Display value of data member.


cout << endl << "D2 Object: " << endl;
cout << "Value after initialization : ";
d2.Display();

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

// student B initialized with roll no 11 and name John


Student B(11, "John");
}
What are the destructor ?
As the name implies, destructors are used to destroy the objects that
have been created by the constructor within the C++ program.
Destructor names are same as the class name but they are preceded by a
tilde (~).

~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.

Special Characteristics of Destructors:

(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

Purpose Constructor is used Destructor destroys


to initialize the the objects when
instance of a class. they are no longer
needed.
When Constructor is Called Destructor is called
Called when new instance of a when instance of a
class is created. class is deleted or
released.
Memory Constructor allocates Destructor releases
Manageme the memory. the memory.
nt
Arguments Constructors can have Destructor can not
arguments. have any arguments.
Overloadin Overloading of Overloading of
g constructor is possible. Destructor is not
possible.
Name Constructor has the Destructor also has
same name as class the same name as
name. class name but
with (~) tiled
operator.
Syntex ClassName(Arguments) ~ ClassName()
{ {
//Body of Constructor }
}
Dynamic Constructor
Allocation of memory to object at the time of their construction is known
as Dynamic Constructor of objects. In the dynamic constructor, new operator is
used for allocation of memory.

 Dynamic constructor is used to allocate the memory to the objects at the


run time.
 Memory is allocated at run time with the help of 'new' operator.
 By using this constructor, we can dynamically initialize the objects.

You might also like