Constructors in C++

C++ProgrammingServer Side Programming

Constructors are functions of a class that are executed when new objects of the class are created. The constructors have the same name as the class and no return type, not even void. They are primarily useful for providing initial values for variables of the class.

The two main types of constructors are default constructors and parameterized constructors. Details about these are given as follows.

Default Constructors

Default constructors do not take any parameters. If a default constructor is not provided by the programmer explicitly, then the compiler provides a implicit default constructor. In that case, the default values of the variables are 0.

A program that demonstrates default constructors is given as follows.

Example

 Live Demo

#include <iostream>
using namespace std;
class A {
   private:
   int num1, num2 ;
   public:
   A() {
      num1 = 5;
      num2 = 7;
   }
   void display() {
      cout<<"num1 = "<< num1 <<endl;
      cout<<"num2 = "<< num2 <<endl;
   }
};
int main() {
   A obj;
   obj.display();
   return 0;
}

Output

num1 = 5
num2 = 7

In the above program, the class A contains a default constructor that initialises num1 and num2 as 5 and 7. It also contains a function display() that prints the value of num1 and num2. The code snippet for this is given as follows.

class A {
   private:
   int num1, num2 ;
   public:
   A() {
      num1 = 5;
      num2 = 7;
   }
   void display() {
      cout<<"num1 = "<< num1 <<endl;
      cout<<"num2 = "<< num2 <<endl;
   }
};

The function main() contains the object definition for an object of class type A. Then the function display() is called. This is shown below.

A obj;
obj.display();

Parameterized Constructors

The parameterized constructors can take arguments to initialize an object when it is created. Parameters are added to a parameterized constructor just like they are added to a normal function. The parameterized constructors can be called implicitly or explicitly.

A program that demonstrates parameterized constructors is given as follows.

Example

 Live Demo

#include <iostream>
using namespace std;
class A {
   private:
   int num1, num2 ;
   public:
   A(int n1, int n2) {
      num1 = n1;
      num2 = n2;
   }
   void display() {
      cout<<"num1 = "<< num1 <<endl;
      cout<<"num2 = "<< num2 <<endl;
   }
};
int main() {
   A obj(3,8);
   obj.display();
   return 0;
}

Output

num1 = 3
num2 = 8

In the above program, the class A contains a parameterized constructor that initialises num1 and num2 with the values provided by n1 and n2. It also contains a function display() that prints the value of num1 and num2. The code snippet for this is given as follows.

class A {
   private:
   int num1, num2 ;
   public:
   A(int n1, int n2) {
      num1 = n1;
      num2 = n2;
   }
   void display() {
      cout<<"num1 = "<< num1 <<endl;
      cout<<"num2 = "<< num2 <<endl;
   }
};

The function main() contains the object definition for an object of class type A. Then the function display() is called. This is shown below.

A obj(3,8);
obj.display();
raja
Published on 09-Oct-2018 06:29:27
Advertisements