C++

C++ Constructors – Detailed Instructions From [A-Z]

A class’s constructor is a unique member function that initializes the class’s object. The name of the constructor is the same as the class name, and it lacks a return type. To further understand how the constructor works, let’s look at a straightforward example.

Simple Example: Using C++’s constructor

To comprehend each section of the following program, read the comments.

#include <iostream>
using namespace std;
class constructorDemo{
public:
   int num;
   char ch;
   /* This is a default constructor of the
    * class, do note that it's name is same as
    * class name and it doesn't have return type.
    */
   constructorDemo() {
      num = 100; ch = 'A';
   }
};
int main(){
   /* This is how we create the object of class,
    * I have given the object name as obj, you can
    * give any name, just remember the syntax:
    * class_name object_name;
    */
   constructorDemo obj;

   /* This is how we access data members using object
    * we are just checking that the value we have
    * initialized in constructor are reflecting or not.
    */
   cout<<"num: "<<obj.num<<endl;
   cout<<"ch: "<<obj.ch;
   return 0;
}

Output:

num: 100
ch: A

Member function versus constructor

Now that we are aware of what a constructor is, let’s talk about how it differs from a member function of the class.

1) The constructor lacks a return type. The return type of a member function is.

2) The constructor is automatically invoked when a class object is created. The class object must be used to explicitly invoke the member function.

(3) The C++ compiler constructs a default constructor and inserts it into our code if we don’t define any constructors for our class. Member functions do not fall under the same umbrella.

This is how a default constructor produced by a compiler appears:

class XYZ
{ 
    ....
    XYZ()
    {
        //Empty no code
    }
};

Types of Constructor in C++

There are two types of constructor in C++. 1) Default constructor 2) Parameterized constructor

1) Default Constructor

An empty default constructor has no arguments (or parameters)

#include <iostream>
using namespace std;
class Website{
public:
   //Default constructor
   Website() {
      cout<<"Welcome to BeginnersBook"<<endl;
   }
};
int main(void){
   /*creating two objects of class Website.
    * This means that the default constructor
    * should have been invoked twice.
    */
   Website obj1;
   Website obj2;
   return 0;
}

Output:

Welcome to BeginnersBook
Welcome to BeginnersBook

If you don’t define a constructor for the class, the compiler will add a default constructor with an empty body to your code.

2) Parameterized Constructor

The term “parameterized constructors” refers to constructors that take parameters. These constructors enable us to pass arguments while creating objects. See how they appear:

Say the class is called XYZ.

Default constructor:

XYZ() {

}
....
XYZ obj;
....

Parameterized Constructor:

XYZ(int a, int b) {

}
...
XYZ obj(10, 20);

Example:

#include <iostream>
using namespace std;
class Add{
public:
   //Parameterized constructor
   Add(int num1, int num2) {
     cout<<(num1+num2)<<endl;
   }
};
int main(void){
   /* One way of creating object. Also
    * known as implicit call to the
    * constructor
    */
   Add obj1(10, 20);
   /* Another way of creating object. This
    * is known as explicit calling the
    * constructor.
    */
   Add obj2 = Add(50, 60);
   return 0;
}

Output:

30
110

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button