Tags:
I hope you read my previous post before looking at this one if you don't know much about OOP in C++. The previous post explains various things like naming of stuff in C++ which is different from naming conventions in other languages.
This one is about explaining scope in C++ when we have inheritance and non-default constructors as well as compositions.
The piont of this post is to establish:
Anyway let's get to it.
#include <iostream>
using namespace std;
class parent {
public:
parent() {
cout << "Parent object created." << endl;
}
~parent() {
cout << "Parent object destoyed." << endl;
}
};
class child: public parent {
public:
child() {
cout << "Child object created." << endl;
}
~child() {
cout << "Child object destoyed." << endl;
}
};
class composition {
child* childObj;
public:
composition() {
cout << "Composing object created" << endl;
childObj = new child;
}
~composition() {
delete childObj;
cout << "Composed oject destroyed." << endl;
}
};
int main () {
composition compObject;
return 0;
}
Output:
Composing object created
Parent object created.
Child object created.
Child object destoyed.
Parent object destoyed.
Composed oject destroyed.
So what do we see here:
It's really that simple.
Hail Stallman and may the FOSS be with you.