Topic :- Constructors & Destructors
Presented to :- Presented by :-
Dr. Rajeshwari Trivedi
Assistant Professor
( Dept. Of Computer Science )
Akshaya Parida
B.Sc. (I.T.) 2nd yr.
1
CONSTRUCTORS :-
2
Constructors :-
Constructors is a special member function
of class and it is used to initialize the
objects of its class.
It is treated as a special member function
because its name is the same as the class
name. These constructors get invoked
whenever an object of its associated class
is created.
 It is named as constructor because it
constructs the value of data member of a
3
Syntax :-
class class_name
{
int g, h;
public:
class_name(void); / / Constructor declared
. . .
. . .
};
class_name :: class_name() / / Constructor defined
{
g=1; h=2;
}
4
Special characteristics of Constructors :-
They should be declared in the public section.
They do not have any return type, not even void.
They get automatically invoked when the objects
are created.
They cannot be inherited though derived class can
call the base class constructor.
Like other functions, they can have default
arguments.
You cannot refer to their address.
Constructors cannot be virtual.
5
Types of Constructors :-
1 . Default constructor
2 . Parameterized constructor
3 . Copy constructor
 There are 3 types of
constructors :-
6
1. Default constructor :-
 The default constructor is the constructor
which doesn't take any argument. It has no
parameter .
 A default constructor is very important for
initializing object members, that even if we do
not define a constructor explicitly, the
compiler automatically provides a default
constructor implicitly.
#include <iostream>
class construct
{
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
}
};
int main()
{
construct c;
cout << "a: " << c.a << endl
<< "b: " << c.b;
return 0;
}
Output:
a: 10
b: 20
Syntax :-
7
2. 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.
#include <iostream>
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()
{
Point p1(10, 15);
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
return 0;
}
 When an object is declared in a parameterized constructor, the initial
values have to be passed as arguments to the constructor function.
The normal way of object declaration may not work. The
constructors can be called explicitly or implicitly.
Example e = Example(0, 50); // Explicit call
Example e(0, 50); // Implicit call
Output:
p1.x = 10, p1.y = 15
Syntax :-
8
3. Copy Constructors :-
 A copy constructor is a member
function which initializes an
object using another object of the
same class.
Syntax :-
class class-name (class class-name &)
{
. . .
}
#include<iostream>
class Point
{
private:
int x, y;
public:
Point(int x1, int y1) { x = x1; y = y1; }
// Copy constructor
Point(const Point &p2) {x = p2.x; y = p2.y; }
int getX() { return x; }
int getY() { return y; }
};
int main()
{
Point p1(10, 15);
Point p2 = p1;
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
cout << "np2.x = " << p2.getX() << ", p2.y = " << p2.getY();
return 0;
}
Syntax:-
Output:
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
9
Destructors :-
 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 (~).
 It is a good practice to declare the destructor after the end of
using constructor.
 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.
~Cube()
{
}
10
References :-
 Object Oriented Programming With C++
E BALAGURUSAMY
11
Thank you

constructors and destructors

  • 1.
    Topic :- Constructors& Destructors Presented to :- Presented by :- Dr. Rajeshwari Trivedi Assistant Professor ( Dept. Of Computer Science ) Akshaya Parida B.Sc. (I.T.) 2nd yr. 1
  • 2.
    CONSTRUCTORS :- 2 Constructors :- Constructorsis a special member function of class and it is used to initialize the objects of its class. It is treated as a special member function because its name is the same as the class name. These constructors get invoked whenever an object of its associated class is created.  It is named as constructor because it constructs the value of data member of a
  • 3.
    3 Syntax :- class class_name { intg, h; public: class_name(void); / / Constructor declared . . . . . . }; class_name :: class_name() / / Constructor defined { g=1; h=2; }
  • 4.
    4 Special characteristics ofConstructors :- They should be declared in the public section. They do not have any return type, not even void. They get automatically invoked when the objects are created. They cannot be inherited though derived class can call the base class constructor. Like other functions, they can have default arguments. You cannot refer to their address. Constructors cannot be virtual.
  • 5.
    5 Types of Constructors:- 1 . Default constructor 2 . Parameterized constructor 3 . Copy constructor  There are 3 types of constructors :-
  • 6.
    6 1. Default constructor:-  The default constructor is the constructor which doesn't take any argument. It has no parameter .  A default constructor is very important for initializing object members, that even if we do not define a constructor explicitly, the compiler automatically provides a default constructor implicitly. #include <iostream> class construct { public: int a, b; // Default Constructor construct() { a = 10; b = 20; } }; int main() { construct c; cout << "a: " << c.a << endl << "b: " << c.b; return 0; } Output: a: 10 b: 20 Syntax :-
  • 7.
    7 2. 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. #include <iostream> 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() { Point p1(10, 15); cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY(); return 0; }  When an object is declared in a parameterized constructor, the initial values have to be passed as arguments to the constructor function. The normal way of object declaration may not work. The constructors can be called explicitly or implicitly. Example e = Example(0, 50); // Explicit call Example e(0, 50); // Implicit call Output: p1.x = 10, p1.y = 15 Syntax :-
  • 8.
    8 3. Copy Constructors:-  A copy constructor is a member function which initializes an object using another object of the same class. Syntax :- class class-name (class class-name &) { . . . } #include<iostream> class Point { private: int x, y; public: Point(int x1, int y1) { x = x1; y = y1; } // Copy constructor Point(const Point &p2) {x = p2.x; y = p2.y; } int getX() { return x; } int getY() { return y; } }; int main() { Point p1(10, 15); Point p2 = p1; cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY(); cout << "np2.x = " << p2.getX() << ", p2.y = " << p2.getY(); return 0; } Syntax:- Output: p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 15
  • 9.
    9 Destructors :-  Asthe 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 (~).  It is a good practice to declare the destructor after the end of using constructor.  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. ~Cube() { }
  • 10.
    10 References :-  ObjectOriented Programming With C++ E BALAGURUSAMY
  • 11.