C++ Constructors | Types & Characteristics of Constructors in OOP

C++ Constructors in OOP | Types & Characteristics of Constructors in C++ | Constructors in C++ | OOP Tutorial in C++

In this tutorial, we are going to learn about the C++ Constructor in OOP, Types of Constructors in C++, Characteristics of Constructors in C++,Constructors in C++, C++ Default Constructor, C++ Parameterized Constructor, C++ Copy Constructor.

What is Constructors in C++?

A constructor in C++ is a special method that is automatically executed when an object of a class is created which initializes objects of a class.

A constructor has the same name as the class and no return value. Constructors initialize values to object members after storage is allocated to the object.

Whereas, Destructor on the other hand is used to destroy the class object.


Example of Constructor in C++ :        

class MyClass {     // The class
  public:           // Access specifier
    MyClass() {     // Constructor
      cout << "Hello World!";
    }
};

int main() {
  MyClass myObj;    // Create an object of MyClass (this will call the constructor)
  return 0;

}


Note: The constructor has the same name as the class, it is always public, and it does not have any return value.

Constructors can be defined either inside the class definition or outside class definition using class name and scope resolution :: operator.


Example of Constructor using Scope Resolution Operator in C++ :        

class A
{

    public:

    int i;

    A(); // constructor declared

};

// constructor definition

A::A()   

{

    i = 1;

}


What are Characteristics Of Constructors in C++ ?

Characteristics of constructors are below:-

  1. These are called automatically when the objects are created.
  2. All objects of the class having a constructor are initialized before some use.
  3. These should be declared in the public section for availability to all the functions.
  4.  Return type (not even void) cannot be specified for constructors.
  5. These cannot be inherited, but a derived class can call the base class constructor.
  6. These cannot be static.
  7. Default and copy constructors are generated by the compiler wherever required.
  8. Generated constructors are public.
  9. These can have default arguments as other C++ functions.
  10. A constructor can call member functions of its class.
  11. An object of a class with a constructor cannot be used as a member of a union.
  12. A constructor can call member functions of its class.
  13. We can use a constructor to create new objects of its class and constructors cannot be virtual.
C++ Types of Constructors in OOP:

There are  three types of constructors in C++ :

1. Default Constructor

2. Parameterized Constructor

3. Copy Constructor


C++ Default Constructor in OOP :

A constructor which has no argument is known as default constructor. A constructor with no parameters is known as a default constructor. It is invoked at the time of creating object.

 

Let's see the simple example of C++ default Constructor.

Example of Default Constructor in C++ :        

class Cube {

    public:

    int side;

    Cube()

    {

        side = 10;

    }
};
 

int main(){

    Cube codeshikhi;

    cout << codeshikhi.side;

}

Output:

10


Note: If we have not defined a constructor in our class, then the C++ compiler will automatically create a default constructor with an empty code and no parameters.

C++ Parameterized Constructor in OOP :

A constructor with parameters is known as a parameterized constructor.  It is used to provide different values to distinct objects. 

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 of Parameterized Constructor in C++ :        

class Cube

{

    public:

    int side;

    Cube(int x)

    {

        side=x;

    }

};

 

int main(){

    Cube codeshikhi1(10);

    Cube codeshikhi2(20);

    Cube codeshikhi3(30);

    cout << codeshikhi1.side;

    cout << codeshikhi2.side;

    cout << codeshikhi3.side;

}

Output:

10

20

30


Uses of Parameterized Constructor in Classes : 

It is used to initialize the various data elements of different objects with different values when they are created.

It is used to overload constructors.


Can we have more than one constructor in a class?

       Answer: Yes, It is called Constructor Overloading.


C++ Copy Constructor in OOP :

A copy constructor is a method or member function which is used to copy data of one object to another. It is usually of the form X (X&), where X is the class name.

Copy Constructors in C++

Syntax of Copy Constructor in C++ :        

Classname(const classname & objectname) {

}

Example of Copy Constructor in C++ :

 class A  {  
     A(A &x) //  copy constructor.  
    {  

       // copyconstructor.  

   }  

};


In the above case, copy constructor can be called in the following ways:


Uses of Copy Constructor in OOP :

  • When we initialize an object by another object of the same class. 
  • When we return an object as a function value. 
  • When the object is passed to a function as a non-reference parameter. 

Difference between Copy Constructor and Assignment Operator:

Copy ConstructorAssignment Operator
It is an overloaded constructor.It is an operator.
The new object is initialized with an object already existing.Value of one object is assigned to another object both of which exists already.
Here, both the objects use different or separate memory locations.Here, different variables points to the same location but only one memory location is used.
The compiler provides a copy constructor if there is no copy constructor defined in the class.Bitwise copy will be made if the assignment operator is not overloaded.
Copy Constructor is used when a new object is being created with the help of the already existing element.Assignment operator is used when we need to assign an existing object to a new object





 

0 Response to C++ Constructors | Types & Characteristics of Constructors in OOP

Post a Comment