Last active
March 29, 2019 11:22
-
-
Save kateolenya/c5edacee13fd544ad834e82fb00084d9 to your computer and use it in GitHub Desktop.
Process of calling inherited constructors and destructors in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Device { | |
public: | |
// constructor | |
Device() { | |
cout << "Device constructor called" << endl; | |
} | |
// destructor | |
~Device() { | |
cout << "Device destructor called" << endl; | |
} | |
}; | |
class Computer: public Device { | |
public: | |
Computer() { | |
cout << "Computer constructor called" << endl; | |
} | |
~Computer() { | |
cout << "Computer destructor called" << endl; | |
} | |
}; | |
class Laptop: public Computer { | |
public: | |
Laptop() { | |
cout << "Laptop constructor called" << endl; | |
} | |
~Laptop() { | |
cout << "Laptop destructor called" << endl; | |
} | |
}; | |
int main() { | |
cout << "\tConstructors" << endl; | |
Laptop Laptop_instance; | |
cout << "\tDestructors" << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment