C++ Class Constructors

This lesson will use examples to study the C++ Class Constructors, their type, and usage.


What is a Constructor?

A particular member function is called automatically when an object is created. In C++, a constructor lacks a return type and shares the same name as the class, followed by parentheses (). For illustration.


Basic Syntax of Class Constructor

class Student {
  public:
    // create a constructor
    Student() {}
};
Tip
The constructor is primarily used to initialize objects. In addition, they are utilized to execute a default code when an object is created.

You can create three types of constructors. Here, the constructor for the “Student” class is a Student (). Take note that the constructor with the same name as the class is public and lacks a return type.

  • C++ Default Constructor
  • C++ Parameterized Constructor
  • C++ Copy Constructor

C++ Default Constructor

A constructor accepts no parameters. Student() in the preceding example is a default constructor.

C++ Default Constructor Example:

//Program in C++ to show how to utilize the default constructor
#include <iostream>

using namespace std;
class Student { // The class: create a student class
  public: // Access specifier
    double studentMathMarks;
  double studentEnglishMarks;
  Student() { //Constrcutor
    studentMathMarks = 100;
    studentEnglishMarks = 99;
    cout << "Total marks of Student is " << studentEnglishMarks + studentMathMarks << endl;
  }
};
int main() {
  Student myStudent;
  return 0;
}

Output

Total marks of Student is 199

The Student() constructor is created when the myStudent object is called, which changes the object’s variables studentMathMarks to 100 and studentEnglishMarks to 99.

Note
The C++ compiler will automatically produce a default constructor with empty code and no parameters if we don’t define one in class.

C++ Parameterized Constructor

A constructor that has parameters is referred to as a parameterized constructor in C++. The method used to initialize member data is this one.

C++ Parameterized Constructor Example:

//Program in C++ to show how to utilize the parameterized constructor
#include <iostream>

using namespace std;
class Student {
  public:
    double studentMathMarks;
  double studentEnglishMarks;
  Student(double MathMarks, double EnglishMarks) { //parameterized Constrcutor

    studentMathMarks = MathMarks;

    studentEnglishMarks = EnglishMarks;
  }
  double calculateTotalMarks() {
    return studentEnglishMarks + studentMathMarks;
  }
};
int main() {
  Student myStudent(100, 99);
  cout << "Total marks of Student is " << myStudent.calculateTotalMarks() << endl;
  return 0;
}

Here, we have created a parameterized constructor Student() that has 2 parameters: double EnglishMarks and double MathMarks. The values in these parameters are used to initialize the member variables studentEnglishMarks and studentMathMarks. When we create an object of the Student class, we pass the values for the member variables as arguments. The code for this is:

Student myStudent(100,99);

C++ Copy Constructor

In C++ programming, the copy constructor is used to copy data from one object to another.


C++ Copy Constructor Example:

//Program in C++ to show how to utilize the parameterized constructor
#include <iostream>

using namespace std;
class Student {
  public:
    double studentMathMarks;
  double studentEnglishMarks;
  Student(double MathMarks, double EnglishMarks) { //parameterized Constrcutor

    studentMathMarks = MathMarks;

    studentEnglishMarks = EnglishMarks;
  }
  // copy constructor with a Student object as a parameter
  // copies data of the obj parameter
  Student(Student & obj) { //parameterized Constructor

    studentMathMarks = obj.studentMathMarks;

    studentEnglishMarks = obj.studentEnglishMarks;
  }
  double calculateTotalMarks() {
    return studentEnglishMarks + studentMathMarks;
  }
};
int main() {
  // copy contents of Peter to John
  Student myStudent1(100, 99);
  Student myStudent2 = myStudent1;
  cout << "Total marks of Student is " << myStudent2.calculateTotalMarks() << endl;
  return 0;
}

In this program, we used a copy constructor to copy the contents of the Student() class object myStudent1 to myStudent2. The code of the copy constructor is:

Student(Student & obj) { //parameterized Constructor
  studentMathMarks = obj.studentMathMarks;
  studentEnglishMarks = obj.studentEnglishMarks;
}

Note that the Student’s class one object’s address is given in the constructor’s parameter. We assign the values of the variables of the obj object obj.studentMathMarks and obj.studentEnglishMarks to the corresponding variables of the object studentMathMarks and studentEnglishMarks, calling the copy constructor, which shows how the objects’ contents are copied.

In main(), we then create myStudent1 and myStudent2 as two objects and then copy the contents of myStudent1 to myStudent2:

// copy contents of myStudent1 to myStudent2
Student myStudent1(100, 99);
Student myStudent2 = myStudent1;

Here, the myStudent2 object calls its copy constructor by passing the address of the myStudent1 object as its argument, i.e., &obj = &myStudent1.

This concludes the C++ Class Constructors lesson. In The next lesson, you will learn about Inheritance in C++ and its usage.