What is a Constructor?
• Special member function of a class which is called / executed implicitly
(automatically) on the creation of the object of that class.
• Same Name as Class Name
• No return type (…no void)
• Implicitly Called
• Constructor is used to initialize the allocated memory of an object
• Constructor cannot be called explicitly.
• Constructor can be called only once for a particular object (on the creation
of that object )
• If multiple objects of the same class are created then constructor will be
called separately for each object on its creation.
• Syntax:- Next Page ?
• Constructor can be overloaded ?
#include <iostream.h> void main ()
#include <conio.h>
{
class Test clrscr();
{
private: Test obj; // object is created;
int x; // object will occupy memory
// constructor will be called i
public: // allocated memory will be initialized
void setX()
{
cin>>x;
}
void getX()
obj.setX(); // Its ok; public member function
{
cout<<x; obj.getX(); // Its ok; public member function
}
// Constructor…
Test() getch();
{
cout<<“I am constructor : Object has created ”<<endl;
cout<<“ Memory has been allocated for object with x=”<<x<<endl; };
x=10;
cout<<“ Allocated memory is initialized with x= ”<<x<<endl;
}
};
Types of Constructors?
1. Do nothing Constructor
• Do nothing constructors are that type of constructor which does not contain any
statements. Do nothing constructor is the one which has no argument in it and no
return type.
2. Default Constructor
• The default constructor is the constructor which doesn't take any argument. It has no
parameter but a programmer can write some initialization statement there.
3. Parameterized Constructor
• A default constructor does not have any parameter, but programmers can add and
use parameters within a constructor if required. This helps programmers to assign
initial values to an object at the time of creation.
4. Copy Constructor
• 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.
What is Constructor Overloading?
• Process of defining more than one constructor in a class with same
name but with different set of parameters is called Constructor
Overloading.
• Constructor can be overloaded
• Multiple constructors for the same class can be defined with different
parameters
• These (Constructor’s) parameters are used to initialize attributes
• as many parameters as you want / require
#include <iostream.h>
#include <conio.h>
void main ()
class Test {
{ clrscr();
private:
int x; Test O1; // object is created;
// object will occupy memory
public: // default constructor will be called
// default Constructor… // allocated memory will be initialized
Test()
{
cout<<“I am constructor : Object has created ”<<endl; Test O2(20); // object with argument is created;
cout<<“ Memory has been allocated for object with x=”<<x<<endl; // object will occupy memory
// constructor with parameter will be called
x=10; // allocated memory will be initialized
cout<<“ Allocated memory is initialized with x= ”<<x<<endl;
}
// Constructor… Overloading
Test O3; // object is created;
// object will occupy memory
Test( int a) // default constructor will be called
{ // allocated memory will be initialized
cout<<“I am overloaded constructor, Object with argument has been created ”<<endl;
cout<<“ Memory has been allocated for object with x=”<<x<<endl;
x=a;
cout<<“ Allocated memory is initialized with x= ”<<x<<endl; getch();
}
}; }
• What is Encapsulation?
• The meaning of Encapsulation, is to make sure that "sensitive" data is
hidden from users. To achieve this, you must:
• declare class variables/attributes as private
• provide public get and set methods to access and update the value of
a private variable
• Why Encapsulation?
• Better control of class attributes and methods
• Class attributes can be made read-only (if you only use
the get method), or write-only (if you only use the set method)
• Flexible: the programmer can change one part of the code without
affecting other parts
• Increased security of data
#include <iostream.h> #include <iostream.h>
#include <conio.h>
#include <conio.h>
class Test class Test // member functions can be defined outside the class
{ {
private: private:
int x;
int x;
public:
public:
void setX() void setX(); // only declaration
{ void getX(); // only declaration
cin>>x; };
}
void Test :: seX() // definition of the member function
{ // outside the class
void getX()
cin>>x; // with scope resolution operator
{ }
cout<<x;
} void Test :: getX()
}; {
cout<<x;
}
Summary:- What are constructors?
• The process of creating and deleting objects in C++ is a vital task. Each
time an instance of a class is created the constructor method is called.
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 class. Initial values can be passed as arguments
to the constructor function when the object is declared…
What are 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. Here's the basic declaration procedure of a
destructor:
• 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.
• ~Class_Name() { } …