This is more of a thing to try then a real project it's a way to see how the class inheritance works
These are things I've been doing so I thought I would share the idea to see if it helps you also
Basically build a couple classes and do inheritance I'll supply an example of what I mean so you can see
The idea here is to see how classes work together and how memory is used with the classes and a preview of the constructors and destructors at work
- Code: Select all
//Notice no headers will compile
class Base
{
public:
Base(){}//try adding the couts inside the constructor/destructor and not pass a variable in the derived object see if i does what you thought
~Base(){}
private:
int baseVariable;
};
class Derived : public Base
{
public:
Derived(){}
~Derived(){}
private:
int derivedVariable;
};
int main()
{
Derived DerivedObject();
}
of coures that does absolutly nothing just yet. Just a base to start playing with, save you some typing
Heres where you start playing around with the code to see how its interacting with each other
- Code: Select all
#include <iostream> //Notice header for cout only maybe sizeof() not sure
class Base
{
public:
Base(int y)
{
std::cout<< "In my Base Constructorn being created with " << y << "\n";
}
~Base()
{
std::cout<< "In The Base Destructor getting destroyed\n";
}
private:
int baseVariable;
//int dummy //a dummy int to show size of class
};
class Derived : public Base //Derived iheriting Base class
{
public:
Derived(int x, int y) : Base(y) //passing y from Derived constructor to Base class constructor
{
std::cout<< "In my Derived Constructorn being created with " << x << "\n";
}
~Derived()
{
std::cout<< "In The Derived Destructor getting destroyed\n";
}
private:
int derivedVariable;
};
int main()
{
Derived DerivedObject(5,6);
//std::cout << sizeof(DerivedObject) << "\n";
}
as you can see I stuck cout statements in both the constructors and destructors that is to see how they are called and destroyed.
Try and make multiple bases or multiple derived or just mix and match.
Try passing more from a derived to a Base class like Base(y), Base1(z) see what happens
try putting Base1 in front of Base a see what happens
uncomment std::cout << sizeof(DerivedObject) << "\n"; under your Derived declaration see what happens.
uncomment the dummy int in the base class and see if the size changes.
Just get creative and try it in different ways and hopefuly that will help you understand inheritance that much more
give me some feed back if this helped or if was just a waste of your time
