C++ classes, objects & methods
A class is an object-oriented paradigm or model used to group both data & functionality together. Classes provide additional features...

Constructors are the special type of methods that get called when we create an instance for a class i.e. an object.

Constructors are used to initializing the member variables of a class with their default values. Without initializing, we cannot access them from outside of a class.

For example, the following program gives you a compilation error because we accessed a class variable without initializing it.

#include <iostream>

class IntrestCalculator {

public:
	int intrestValue;
};

int main() {
	IntrestCalculator obj;
	std::cout << obj.intrestValue;
}

In the below example, interestValue contains a garbage value because it has not been initialized. Inside a class, it is allowed to use class variables without initializing them but it may provide an unexpected result as shown in the below example.

#include <iostream>

class IntrestCalculator {

public:
	// Not initialized & contains a garbage value
	int intrestValue;

	int calculateIntrest(int amount) {
		return (amount * intrestValue) / 100;
	}
};

int main() {

	IntrestCalculator obj;

	// The following line prints a garbage or incorrect value.
	// Because the intrestValue is not defined
	std::cout << obj.calculateIntrest(10000);
}

How to create a constructor?

Constructors can be created in the same way as how we create methods. But we must follow the following rules:

  • The constructor name must be the same as a class name.
  • The constructor should not have a return type.
  • Must be declared under the public access modifier.

Example:

#include <iostream>

class IntrestCalculator {

public:
	int intrestValue;

	// Constructor with no parameters
	IntrestCalculator()
	{
		intrestValue = 2;
	}
	
	int calculateIntrest(int amount) {
		return (amount * intrestValue) / 100;
	}
};

int main() {

	IntrestCalculator obj;
	std::cout << obj.calculateIntrest(10000);
}

What is a default constructor?

In the above example, we declared a constructor and we initialized a class variable inside the constructor. We call it a no-param constructor.

If we don't create any constructor then by default C compiler provides a default one which is known as a default constructor. It looks like as shown in the below example.

#include <iostream>

class IntrestCalculator {

public:
	int intrestValue;

	// Default constructor
	IntrestCalculator()
	{
		// Does not contain anything inside it.
	}
	
};

int main() {
	IntrestCalculator obj;
}

What is a parameterized constructor?

Using parameterized constructor, we can pass parameters to the constructor as shown in the below example.

#include <iostream>

class InterestCalculator {

public:
	int interestValue;

	// Constructor with one parameter
	InterestCalculator(int interest)
	{
		interestValue = interest;
	}

	int calculateInterest(int amount) {
		return (amount * interestValue) / 100;
	}
};

int main() {

	InterestCalculator obj(2);
	std::cout << obj.calculateInterest(10000);
}