BIT153 ยท 2079 ยท 10 marks
Explain the chain of constructor and destructor between sub classes and super classes during inheritance with example.Create a class named 'Point' with data members x and y. Overload the + operator to add the value of two objects of this class.[5+5]
Explain the chain of constructor and destructor between sub classes and super classes during inheritance with example.Create a class named 'Point' with data members x and y. Overload the + operator to add the value of two objects of this class.[5+5]
Quick answer
In C++ inheritance, when an object of a derived (sub) class is created or destroyed, constructors and destructors are called in a specific order involving both the base (super) class and the derived class.
Chain of Constructor and Destructor in Inheritance + Operator Overloading
(a) Chain of Constructor and Destructor in Inheritance
Concept
Constructor Chaining (Order of Execution)
When a derived class object is created:
- Base class constructor is called FIRST
- Derived class constructor is called SECOND
This ensures that the base part of the object is fully initialized before the derived part uses it.
Destructor Chaining (Order of Execution)
When a derived class object is destroyed:
- Derived class destructor is called FIRST
- Base class destructor is called SECOND
The destructor order is the exact reverse of constructor order.
Constructor Order: Base --> Derived
Destructor Order: Derived --> Base
Example
#include <iostream>
using namespace std;
// Super class (Base class)
class Animal {
public:
Animal() {
cout << "Animal Constructor called" << endl;
}
~Animal() {
cout << "Animal Destructor called" << endl;
}
};
// Sub class (Derived class)
class Dog : public Animal {
public:
Dog() {
cout << "Dog Constructor called" << endl;
}
~Dog() {
cout << "Dog Destructor called" << endl;
}
};
// Further derived class
class Puppy : public Dog {
public:
Puppy() {
cout << "Puppy Constructor called" << endl;
}
~Puppy() {
cout << "Puppy Destructor called" << endl;
}
};
int main() {
cout << "Creating Puppy object:" << endl;
Puppy p;
cout << "\nObject going out of scope:" << endl;
return 0;
}
Output
Creating Puppy object:
Animal Constructor called
Dog Constructor called
Puppy Constructor called
Object going out of scope:
Puppy Destructor called
Dog Destructor called
Animal Constructor called
Summary Table
| Event | Order |
|---|---|
| Object Creation | Base --> Intermediate --> Derived |
| Object Destruction | Derived --> Intermediate --> Base |
Key Rule: Constructors run from top (most base) to bottom (most derived). Destructors run from bottom (most derived) to top (most base).
(b) Class Point with Overloaded + Operator
Concept of Operator Overloading
Operator overloading allows C++ operators (like +, -, *) to work with user-defined objects. The keyword operator is used to define the overloaded function.
Program
#include <iostream>
using namespace std;
class Point {
private:
int x;
int y;
public:
// Constructor
Point(int x = 0, int y = 0) {
this->x = x;
this->y = y;
}
// Overload + operator
Point operator+(Point p) {
Point temp;
temp.x = this->x + p.x;
temp.y = this->y + p.y;
return temp;
}
// Display function
void display() {
cout << "Point(" << x << ", " << y << ")" << endl;
}
};
int main() {
Point p1(3, 4);
Point p2(5, 6);
Point p3;
cout << "Point 1: ";
p1.display();
cout << "Point 2: ";
p2.display();
// Using overloaded + operator
p3 = p1 + p2;
cout << "Sum of Points: ";
p3.display();
return 0;
}
Output
Point 1: Point(3, 4)
Point 2: Point(5, 6)
Sum of Points: Point(8, 10)
Explanation
| Step | Description |
|---|---|
Point p1(3, 4) | Creates object p1 with x=3, y=4 |
Point p2(5, 6) | Creates object p2 with x=5, y=6 |
p1 + p2 | Calls p1.operator+(p2) |
| Inside operator+ | temp.x = 3+5 = 8, temp.y = 4+6 = 10 |
Returns temp | Result stored in p3 |
Note: The
operator+function takes one argument (the right-hand operand) because the left-hand operand is the calling object (this). The function returns a newPointobject containing the sum.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.