Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Constructor and Destructor

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Constructor

• A constructor is a member function of a class


which initializes objects of a class.

• In C++, Constructor is automatically called


when object(instance of class) create. It is
special member function of the class.
How constructors are different from a
normal member function?
• Constructor has same name as the class.
• Constructors don’t have return type
• A constructor is automatically called when an object
is created.
• If we do not specify a constructor, C++ compiler
generates a default
constructor for us (expects no parameters and has an
empty body).
• constructors can be overloaded
Types of Constructor
1. Default Constructor
A default constructor does not have any parameter
2. Parametrized Constructor
A Parametrized constructor have any parameter. It’s called
“Parametrized constructor”. Hence constructor can be
overloaded.
3. Copy Constructor
• Constructor can accept a reference to its own class as a
parameter. It’s called “Copy constructor”.
• All member values of one object can be assigned to the
other object using copy constructor.
• For copying the object values, both objects must belong to
same class.
Syntax of Constructors
class A
{
public:
int i;
A(); // constructor declared
};
// constructor definition
A::A()
{
i = 1;
}
Default Constructor
Default constructor is the constructor which doesn’t take any
argument. It has no parameters.
Example
#include <iostream>
using namespace std;
class construct {
public:
    int a, b;
      // Default Constructor
    construct()
    {
        a = 10;
        b = 20;
    }
};
  
Default Constructor

int main()
{
    // Default constructor called automatically
    // when the object is created
    construct c;
    cout << "a: " << c.a << endl
         << "b: " << c.b;
    return 1;
}
Output:
a: 10
b: 20
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.
Example:
#include <iostream>
using namespace std;
  class Point {
private:
    int x, y;
Parameterized Constructors

public:
    // Parameterized Constructor
    Point(int x1, int y1)
    {
        x = x1;
        y = y1;
    }
     int getX()
    {
        return x;
    }
    int getY()
    {
        return y;
    }
};
  
Parameterized Constructors

int main()
{
    // Constructor called
    Point p1(10, 15);
  
    // Access values assigned by constructor
    cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
  
    return 0;
}
Output
p1.x = 10, p1.y = 15
Copy Constructor

Syntax:

class A  
{  
    A(A &x) //  copy constructor.  
   {  
       // copyconstructor.  
   }  
}   
Copy Constructor
Example:

#include <iostream>  
using namespace std;  
class A  
{  
   public:  
    int x;  
    A(int a)                // parameterized constructor.  
    {  
      x=a;  
    }  
    A(A &i)               // copy constructor  
    {  
        x = i.x;  
    }  
};  
Copy Constructor

int main()  
{  
  A a1(20);               // Calling the parameterized constructor.  
 A a2(a1);                //  Calling the copy constructor.  
 cout<<a2.x;  
  return 0;  
}  

Output
20
Destructors

Syntax
class A
{
public:
// defining destructor for class
~A()
{
// statement
}
};
Destructors

Example Program
class A
{
// constructor
A()
{
cout << "Constructor called";
}
// destructor
~A()
{
cout << "Destructor called";
}
};
Destructors

int main()
{
A obj1; // Constructor Called
int x = 1
if(x)
{
A obj2; // Constructor Called
} // Destructor Called for obj2
} // Destructor called for obj1
Output
Constructor called
Constructor called
Destructor called
Destructor called
Destructors

Example Program
#include <iostream>
using namespace std;
class HelloWorld
{
public:
//Constructor
HelloWorld()
{ cout<<"Constructor is called"<<endl; }
//Destructor
~HelloWorld()
{ cout<<"Destructor is called"<<endl; }
//Member function
void display()
{ cout<<"Hello World!"<<endl; } };
Destructors

int main()
{
//Object created
HelloWorld obj;
//Member function called
obj.display();
return 0;
}

Output
Constructor is called
Hello World!
Destructor is called

You might also like