WELCOME
CONSTRUCTOR AND DESTRUCTOR
What is Constructor?
Constructorisbasicallyaspecial
memberfunctionhavingsamename
asthatofclass.
PROPERTIES OF CONSTRUCTORS
• A constructor has the same name as that of class
• Constructor doesn’t have a return type not even void
• Like normal functions Constructor have default argument.
• They may not be static
• They cannot be inherited.
• They are executed automatically whenever objects of class are created.
• We can declare more than one constructor in a class
Syntax of constructor in C++
class CLASSNAME
{
…………….
}
Public:
CLASSNAME([parameter list])
{
…………….
}
};
class student
{
…………….
}
Public:
student ()
{
…………….
}
};
WHAT IS DESTRUCTOR?
A destructor is a member function having same name as that
of Constructor but followed by a tilde(~) symbol.
PROPERTIES OF DESTRUCTOR
A destructor has the same name as that of a class or constructor to which it belong
followed by tilde sign.
It is executed automatically and is used to destroy the objects of class.
It takes neither parameter nor any value.
There is only one constructor in a class
It cannot be overloaded.
It cannot be declared static or const.
They cannot be inherited.
Syntax of destructor in C++
Class CLASSNAME
{
………..
Public:
CLASSNAME()
//DESTRUCTOR DECLARATION
{
………………..
}
~CLASSNAME()
// DESTRUCTOR DEFINITION
{
}
};
DIFFERENCE BETWEEN CONTRUCTOR & DESTRUCTOR
CONSTRUCTOR
• Constructor allocates the memory
• They can have arguments
• Overloading is possible
• No special symbol needed for
recognition
• Classname(arguments)
{
// body of Constructor
}
DESTRUCTOR
• Destructor deallocates the memory
• They cannot have arguments.
• No overloading possible
• Tilde symbol needed for recognition
• ~classname()
{
…………………
}
THANKS

What is Constructors and Destructors in C++ (Explained with Example along with difference.)

  • 1.
  • 2.
    CONSTRUCTOR AND DESTRUCTOR Whatis Constructor? Constructorisbasicallyaspecial memberfunctionhavingsamename asthatofclass.
  • 3.
    PROPERTIES OF CONSTRUCTORS •A constructor has the same name as that of class • Constructor doesn’t have a return type not even void • Like normal functions Constructor have default argument. • They may not be static • They cannot be inherited. • They are executed automatically whenever objects of class are created. • We can declare more than one constructor in a class
  • 4.
    Syntax of constructorin C++ class CLASSNAME { ……………. } Public: CLASSNAME([parameter list]) { ……………. } }; class student { ……………. } Public: student () { ……………. } };
  • 5.
    WHAT IS DESTRUCTOR? Adestructor is a member function having same name as that of Constructor but followed by a tilde(~) symbol.
  • 6.
    PROPERTIES OF DESTRUCTOR Adestructor has the same name as that of a class or constructor to which it belong followed by tilde sign. It is executed automatically and is used to destroy the objects of class. It takes neither parameter nor any value. There is only one constructor in a class It cannot be overloaded. It cannot be declared static or const. They cannot be inherited.
  • 7.
    Syntax of destructorin C++ Class CLASSNAME { ……….. Public: CLASSNAME() //DESTRUCTOR DECLARATION { ……………….. } ~CLASSNAME() // DESTRUCTOR DEFINITION { } };
  • 8.
    DIFFERENCE BETWEEN CONTRUCTOR& DESTRUCTOR CONSTRUCTOR • Constructor allocates the memory • They can have arguments • Overloading is possible • No special symbol needed for recognition • Classname(arguments) { // body of Constructor } DESTRUCTOR • Destructor deallocates the memory • They cannot have arguments. • No overloading possible • Tilde symbol needed for recognition • ~classname() { ………………… }
  • 9.