0% found this document useful (0 votes)
5 views15 pages

C++ Constructors and Destructors Explained

The document explains constructors and destructors in C++, highlighting their roles in initializing and destroying class objects, respectively. It details types of constructors including default, parameterized, and copy constructors, along with examples of their usage. Additionally, it outlines the differences between constructors and destructors, noting that constructors can be overloaded while destructors cannot.

Uploaded by

Sudha Jacob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views15 pages

C++ Constructors and Destructors Explained

The document explains constructors and destructors in C++, highlighting their roles in initializing and destroying class objects, respectively. It details types of constructors including default, parameterized, and copy constructors, along with examples of their usage. Additionally, it outlines the differences between constructors and destructors, noting that constructors can be overloaded while destructors cannot.

Uploaded by

Sudha Jacob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Constructors and Destructors

Constructors in C++
• What is constructor?
A constructor is a member function of a class
which initializes objects of a class.
• In C++, Constructor is automatically called when
object is created.
• It is special member function of the class.
• How constructors are different from a normal
member function?
A constructor is different from normal functions in
following ways:
• Constructor has same name as the class itself
• Constructors don’t have return type
• If we do not specify a constructor, C++ compiler
generates a default constructor for us (expects no
Types of Constructors
• Default Constructors: Default constructor is
the constructor which doesn’t take any
argument. It has no parameters.
• Cpp program to illustrate the concept of
Constructors
#include <iostream>
using namespace std;
class construct
{
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
}
};

int main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout << "a: " << c.a << "b: " << c.b;
return 0;
}
Output:
Parameterized Constructors:
It is 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.
Usage:
– It is used to initialize the various data
elements of different objects with different
values when they are created.
– It is used to overload constructors.
#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 = " << [Link]() << ", p1.y =
" <<[Link]();
return 0;
}
Output:
p1.x = 10, p1.y = 15
Copy Constructor:
A copy constructor is a special type of constructor used to create a new object using an
existing object of the same class.
#include <iostream>
using namespace std;
class Transport
{
public:
int wheels;
Transport(int n)
{
wheels=n;
}
Transport(Transport &ob) //Defining a copy constructor
{
wheels=[Link];
}
};
int main()
{
Transport car1(4); // Calling the 1st constructor
Transport car2(car1); //Shallow copy
Transport car3=car2; //Deep Copy
cout<<"Car2 has "<<[Link]<<" wheels\n";
cout<<"Car3 has "<<[Link]<<" wheels";
return 0;
}
Output:
Car2 has 4 wheels
Car3 has 4 wheels
• What is destructor?
Destructor is a member function which
destructs or deletes an object.
• When is destructor called?
A destructor function is called automatically
when the object goes out of scope:
(1) the function ends
(2) the program ends
(3) a block containing local variables ends
(4) a delete operator is called
How destructors are different from a
normal member function?
Destructors have same name as the class
preceded by a tilde (~)
Destructors don’t take any argument and don’t
#include <iostream>
using namespace std; Output:
class Employee { Employee Created"
Name:Lilly
public:
Id:101
string name; Employee Record Deleted
int id;
Employee(string n, int i) {
name = n;
id = i;
cout << "Employee Created" ;
}
~Employee() { // Destructor
cout << "Employee Record Deleted " ;
}
};
int main() {
Employee e1("Lilly", 101);
cout<<“Name:”<<[Link];
cout<<“Id:”<<[Link];
• Can there be more than one
destructor in a class?
No, there can only one destructor in a
class with class name preceded by ~, no
parameters and no return type.
Difference between Constructors and Destructors
S. No. Constructor Destructor

1. Constructor helps to initialize the object of a class. Whereas destructor is used to destroy the instances.

2. Constructor can either accept arguments or not. While it can't have any arguments.

A constructor is called when an instance or object of a class is


3. It is called while object of the class is freed or deleted.
created.

While it is used to deallocate the memory of an object


4. Constructor is used to allocate the memory to an instance or object.
of a class.

5. Constructor can be overloaded. While it can't be overloaded.

Here, its name is also same as the class name preceded


6. The constructor's name is same as the class name.
by the tiled (~) operator.

7. In a class, there can be multiple constructors. While in a class, there is always a single destructor.

You might also like