C++ Inheritance – 3 – Constructors and Destructors

When a derived-class object is instantiated, a chain of constructor calls begins. First, the derived class constructor invokes the base class constructor, before performing its own tasks. If the base class of the derived class is also a derived class from another class, then that class invokes its base class constructor before performing its tasks, and this continues until the class at the base of the hierarchy is found, whose body will be executed first. The derived class that you instantiate will actually execute its constructor last.

Similarly, when a derived-class object is destroyed, a chain of destructor calls begins. But the destructors are called in the reversed order, where the original derived-class object performs its tasks first, then it invokes the destructor of the base class and so on.

The code below is from C++ Inheritance – 2 – Protected Members with some modifications. I added the class TwoDimensionalShape which is derived from Shape, and derives Rectangle and Triangle. It’s quite useless, I just added it to make the chain of constructor and destructor calls clearer. Each constructor and destructor prints out “Constructor of class” and “Destructor of class”, respectively, so it should be easy to follow and understand the output.

#include <iostream>

// Base class
class Shape {
public:
	Shape(double width, double height) : width(width), height(height) {
		std::cout << "constructor of Shape" << "\n";
	}
	~Shape() {
		std::cout << "destructor of Shape" << "\n";
	}
	void setWidth(int width) {
		this->width = width;
	}

	void setHeight(int height) {
		this->height = height;
	}

	double getWidth() {
		return width;
	}

	double getHeight() {
		return height;
	}

protected:
	double width;
	double height;
};

class TwoDimensionalShape : public Shape {
public:
	TwoDimensionalShape(double width, double height) : Shape(width, height) {
		std::cout << "constructor of TwoDimensionalShape" << "\n";
	}
	~TwoDimensionalShape() {
		std::cout << "destructor of TwoDimensionalShape" << "\n";
	}
};

// Derived class
class Rectangle : public TwoDimensionalShape {
public:
	Rectangle(double width, double height) : TwoDimensionalShape(width, height) {
		std::cout << "constructor of Rectangle" << "\n";
	}
	~Rectangle() {
		std::cout << "destructor of Rectangle" << "\n";
	}

	double getArea() {
		return width * height;
	}
};

// Another derived class
class Triangle : public TwoDimensionalShape {
public:
	Triangle(double width, double height) : TwoDimensionalShape(width, height) {
		std::cout << "constructor of Triangle" << "\n";
	}

	~Triangle() {
		std::cout << "destructor of Triangle" << "\n";
	}
	double getArea() {
		return (width * height) / 2;
	}
};

int main() {
	Rectangle rect(5, 5);

	std::cout << "\n";

	Triangle tri(10, 10);

	std::cout << "\n";

	std::cout << "The rectangle's area is " << rect.getArea() << "\n";
	std::cout << "The triangle's area is " << tri.getArea() << "\n";

	std::cout << "\n";
}

Output:

constructor of Shape
constructor of TwoDimensionalShape
constructor of Rectangle
constructor of Shape
constructor of TwoDimensionalShape
constructor of Triangle

The rectangle's area is 25
The triangle's area is 50

destructor of Triangle
destructor of TwoDimensionalShape
destructor of Shape
destructor of Rectangle
destructor of TwoDimensionalShape
destructor of Shape

 

Author

authors profile photo

Articles with similar tags

thumbnail
C++ Overloading Relational Operators

This article goes through how to overload relational operators in C++. Create your own user-defined types and use overloaded operators with them.

By Frogitecture

thumbnail
C++ Inheritance - 1 - Base and Derived Classes

Inheritance is a form of software reuse in which a class can use another class’s methods and members and modify them.

By Frogitecture

thumbnail
C++ Inheritance – 2 – Protected Members

Two very common access specifier in C++ are public and private. Here I introduce the access specifier protected and give you an example of how you can use it.

By Frogitecture