Computer Science
Constructors and Destructors…
</>
Saharsh Anand
</>
Constructor Function
giving the class object some sense…
Ever thought that what kind of
problems can an object create if it’s
not initialised.
It’s similar to giving your vehicle the
final gear just during the startup.
The Object
What if user
didn’t assign me
any value initially
(by chance he
forget) and finally
ask me my value?
I’ll be helpless.
So to make a
program
robust the
object should
be initialised
every time.
And in class we do this by constructors...
• Well! It is a special member function
that is used for initialization of objects
(data members).
• A constructor function is called
whenever an object is created.
• Constructors are also called when an
object is created as a part of another
object.
What is “ Constructor ” !!!
</>
Initialising a Constructor:
/* Outside the Class */
class twelfth{
int total; //Total twelfth of student
char sec; //Section of the class
public:
twelfth(void); //Constructor Function Declared
---
---
};
//Constructor Function Defined
twelfth :: twelfth( ){
total=50;
sec=‘A’;
}
Initialising a Constructor:
</> /* Outside the Class */
class twelfth{
int total; //Total twelfth of student
char sec; //Section of the class
public:
twelfth( ){ //Function Declared
total=50;
sec=‘A’;
}
Constructor Functions have “same
name” as that of class name.
They do not have return types, not even
void.
May or may not have parameters.
C++ has default constructors that are
called whenever a class is declared even
if no function with the same name exists
Some of it’s characteristics
Mind It!
They should be declared in “public
section”.
They are invoked “automatically” when
objects are created.
We cannot call it for the already
constructed object.
Default Constructor
• Because in C++ default constructor is there.
• This constructor has no arguments in it.
• Default Constructor is also called as no
argument constructor.
So you often didn’t
provide any argument to
constructor, but still
compiler didn’t bother to
show any error! How?
</>
Example for default constructor:
class person {
int DOB;
public:
person(){
cout<<“Contructor called";
}
};
void main(){
person obj;
getch();
}
Parameterised Constructor
• A parameterized constructor is just one that
has parameters specified in it.
• We can pass the arguments to constructor
function when object are created.
• A constructor that can take arguments are
called parameterized constructors.
</>
Example of a Parameterised Constructor:
class person{
int DOB;
public:
Creature(int year){ //Parameterized Constructor
DOB = year;
}
};
Copy Constructor
• Copy Constructor is used to declare and
initialize an object from another object.
• For example the statement:
abc c2(c1);
would define the object c2 and at the same
time initialize it to the value of c1.
• The process of initializing through a copy
constructor is known as copy initialization.
</>
Example of a Copy Constructor:
class abc{
int a, b;
public:
abc(int x, int y){
a = x;
b = y;
}
abc::abc(abc &p){
a = p.a;
b = p.b;
}
void showdata(){
cout << a << " " << b << endl;
}
};
continued…
void main(){
abc c1(10, 20);
abc c2(c1);
c1.showdata();
c2.showdata();
getch();
}
</>
continued…
Default Constructor
• Default argument is an argument to a
function that a programmer is not required
to specify.
• C++ allow the programmer to specify
default arguments that always have a value,
even if one is not specified when calling the
function.
• For example, in the following function
declaration:
int MyFunc(int a,int b,int c=12);
• The programmer may call this function in
two ways:
result = MyFunc(1, 2, 3);
result = MyFunc(1, 2);
• In the first case the value for the argument
called c is specified as normal. In the second
one, the argument is omitted, and the
default value of 12 will be used instead.
• It is possible to define constructors with
default arguments.
Something Important
• Automatically called when an object is
created.
• We can define our own constructors
• A constructor takes the same name as the
class name.
• We can’t define a constructor in the
private section.
• No return type is specified for a
constructor.
• Constructor must be defined in the
public.
• Overloading of constructors is possible.
• If an object is copied from another
object then the copy constructor is
called.
</>
Destructor Function
ending up the class data…
You won’t ever
want that your
program's
variable taking all
your hard disk
space.
In case if you want
that the program
should end up after
destroying the data
then you’ll have to
use destructors
•Destructors are special member
functions.
• Release dynamic allocated memory.
• Destructors are automatically named.
• Takes the same name of class name.
What is “ Destructor ” !!!
Syntax of Destructor
Well there is nothing to think about
the syntax of destructor if you are
familiar with the constructors.
Just add a tilde (~).
~class-name();
Something Important
• Destructors take the same name as
class name.
• As in constructors, destructors are also
defined in the public.
• Destructors cannot be overloaded.
• No return type is specified.
</>
Example of a Destructors:
class person{
int DOB;
public:
person(){
DOB=1998;
cout<<"constructor called"<<endl;
}
~person(){
cout<<"destructor called"<<endl;
}
};
continued…
void main(){
cout<<“n Main startn"<<endl;
{
person obj;
}
cout<<“n Main End"<<endl;
getch();
}
continued…
</>
</>
Another example of destructor:
#include<iostream.h>
#include<stdio.h>
class Line{
public:
void setLength(double len);
double getLength(void);
Line(); // This is the constructor declaration
~Line(); // This is the destructor: declaration
private:
double length;
};
continued…
// Member functions definitions including constructor
Line::Line(void){
cout << "Object is being created" << endl;
}
Line::~Line(void){
cout << "Object is being deleted" << endl;
}
void Line::setLength(double len){
length = len;
}
double Line::getLength(void){
return length;
}
// Main function for the program
</>
continued…
void main(){
Line line;
//Set line’s length
line.setLength(6.0);
cout<<"Length of line:"<<line.getLength()<<endl;
getch();
}
</>
continued…
Constructor
Destructor
Constructs (or
create) the data in
the objects of the
class.
Destroy (or delete)
the data in the
objects of the class.
Constructor & destructor

Constructor & destructor

  • 1.
    Computer Science Constructors andDestructors… </> Saharsh Anand
  • 2.
    </> Constructor Function giving theclass object some sense…
  • 3.
    Ever thought thatwhat kind of problems can an object create if it’s not initialised. It’s similar to giving your vehicle the final gear just during the startup.
  • 4.
    The Object What ifuser didn’t assign me any value initially (by chance he forget) and finally ask me my value? I’ll be helpless. So to make a program robust the object should be initialised every time. And in class we do this by constructors...
  • 5.
    • Well! Itis a special member function that is used for initialization of objects (data members). • A constructor function is called whenever an object is created. • Constructors are also called when an object is created as a part of another object. What is “ Constructor ” !!!
  • 6.
    </> Initialising a Constructor: /*Outside the Class */ class twelfth{ int total; //Total twelfth of student char sec; //Section of the class public: twelfth(void); //Constructor Function Declared --- --- }; //Constructor Function Defined twelfth :: twelfth( ){ total=50; sec=‘A’; }
  • 7.
    Initialising a Constructor: </>/* Outside the Class */ class twelfth{ int total; //Total twelfth of student char sec; //Section of the class public: twelfth( ){ //Function Declared total=50; sec=‘A’; }
  • 8.
    Constructor Functions have“same name” as that of class name. They do not have return types, not even void. May or may not have parameters. C++ has default constructors that are called whenever a class is declared even if no function with the same name exists Some of it’s characteristics Mind It!
  • 9.
    They should bedeclared in “public section”. They are invoked “automatically” when objects are created. We cannot call it for the already constructed object.
  • 10.
    Default Constructor • Becausein C++ default constructor is there. • This constructor has no arguments in it. • Default Constructor is also called as no argument constructor. So you often didn’t provide any argument to constructor, but still compiler didn’t bother to show any error! How?
  • 11.
    </> Example for defaultconstructor: class person { int DOB; public: person(){ cout<<“Contructor called"; } }; void main(){ person obj; getch(); }
  • 12.
    Parameterised Constructor • Aparameterized constructor is just one that has parameters specified in it. • We can pass the arguments to constructor function when object are created. • A constructor that can take arguments are called parameterized constructors.
  • 13.
    </> Example of aParameterised Constructor: class person{ int DOB; public: Creature(int year){ //Parameterized Constructor DOB = year; } };
  • 14.
    Copy Constructor • CopyConstructor is used to declare and initialize an object from another object. • For example the statement: abc c2(c1); would define the object c2 and at the same time initialize it to the value of c1. • The process of initializing through a copy constructor is known as copy initialization.
  • 15.
    </> Example of aCopy Constructor: class abc{ int a, b; public: abc(int x, int y){ a = x; b = y; } abc::abc(abc &p){ a = p.a; b = p.b; } void showdata(){ cout << a << " " << b << endl; } }; continued…
  • 16.
    void main(){ abc c1(10,20); abc c2(c1); c1.showdata(); c2.showdata(); getch(); } </> continued…
  • 17.
    Default Constructor • Defaultargument is an argument to a function that a programmer is not required to specify. • C++ allow the programmer to specify default arguments that always have a value, even if one is not specified when calling the function. • For example, in the following function declaration: int MyFunc(int a,int b,int c=12);
  • 18.
    • The programmermay call this function in two ways: result = MyFunc(1, 2, 3); result = MyFunc(1, 2); • In the first case the value for the argument called c is specified as normal. In the second one, the argument is omitted, and the default value of 12 will be used instead. • It is possible to define constructors with default arguments.
  • 19.
    Something Important • Automaticallycalled when an object is created. • We can define our own constructors • A constructor takes the same name as the class name. • We can’t define a constructor in the private section.
  • 20.
    • No returntype is specified for a constructor. • Constructor must be defined in the public. • Overloading of constructors is possible. • If an object is copied from another object then the copy constructor is called.
  • 21.
  • 22.
    You won’t ever wantthat your program's variable taking all your hard disk space. In case if you want that the program should end up after destroying the data then you’ll have to use destructors
  • 23.
    •Destructors are specialmember functions. • Release dynamic allocated memory. • Destructors are automatically named. • Takes the same name of class name. What is “ Destructor ” !!!
  • 24.
    Syntax of Destructor Wellthere is nothing to think about the syntax of destructor if you are familiar with the constructors. Just add a tilde (~). ~class-name();
  • 25.
    Something Important • Destructorstake the same name as class name. • As in constructors, destructors are also defined in the public. • Destructors cannot be overloaded. • No return type is specified.
  • 26.
    </> Example of aDestructors: class person{ int DOB; public: person(){ DOB=1998; cout<<"constructor called"<<endl; } ~person(){ cout<<"destructor called"<<endl; } }; continued…
  • 27.
    void main(){ cout<<“n Mainstartn"<<endl; { person obj; } cout<<“n Main End"<<endl; getch(); } continued… </>
  • 28.
    </> Another example ofdestructor: #include<iostream.h> #include<stdio.h> class Line{ public: void setLength(double len); double getLength(void); Line(); // This is the constructor declaration ~Line(); // This is the destructor: declaration private: double length; }; continued…
  • 29.
    // Member functionsdefinitions including constructor Line::Line(void){ cout << "Object is being created" << endl; } Line::~Line(void){ cout << "Object is being deleted" << endl; } void Line::setLength(double len){ length = len; } double Line::getLength(void){ return length; } // Main function for the program </> continued…
  • 30.
    void main(){ Line line; //Setline’s length line.setLength(6.0); cout<<"Length of line:"<<line.getLength()<<endl; getch(); } </> continued…
  • 31.
    Constructor Destructor Constructs (or create) thedata in the objects of the class. Destroy (or delete) the data in the objects of the class.