Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Things to try with inheritance

Here are some projects of actual programs you can practice making.

Things to try with inheritance

Postby noobgrammer on Fri Oct 02, 2009 3:50 pm

Things to try

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
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando

Return to Projects

Who is online

Users browsing this forum: No registered users and 0 guests

cron