• Advertisement
Sign in to follow this  

General Question on Class Inheritance

This topic is 4708 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have some VERY general questions about class inheritance that I want to ask. 1) How exactly does it work syntatically? I mean I could look at code day by day by day (and I have) but I still don't really have a full understanding of the concept. 2) Lets say you have a class inherited from a base class. Does the inherited class still carry all the base class methods and properties? (think a general rendering method from a sprite class, its x position, and y position) Well I think that's it for now. I'm looking at C++ Primer Plus by Steve Prata and I'm getting the shaft. Oh well maybe I'm just missing the obvious. Thank you for your time, toXic1337

Share this post


Link to post
Share on other sites
Advertisement
Here is one example:


#include <string>
#include <iostream>

// This is our base class to which we will derive from
class BaseClass
{
// Any member or function in the private section will not be able to be accessed outside of this class
private:
int Number;

// Any member in the protected section can be accessed only by class derived from this class as well as this class itself
protected:
char Letter;

// Anything in the public section is of fair game for anyone
public:
// This is the default ctor, it's a good idea to make one ;)
BaseClass()
{
Letter = 'A';
}

// This is a overloaded ctor
BaseClass( int num )
{
Letter = num;
}

// This is the default dtor, if we want our classes to be destroyed properly it needs to be virtual(overideable)
virtual ~BaseClass()
{
}

// Some class function that can be accessed freely
void Print()
{
std::cout << Letter << std::endl;
}

// This functions means it MUST be overridden in the derived class, thus making this class abstract
// You cannot now do "BaseClass varname;" but you can do "BaseClass *varname"
virtual void Die() = 0;
};

// Child class derived from base class
class ChildClass : public BaseClass
{

// Same as above, only this class can use this variable
private:
int Number2;

// If we plan to further derive from this class, then we put any members/functions in this section we want them to use
protected:

public:
// Default ctor that initializes itself as well as calls the BaseClass ctor. This is done automatically, but if you need to pass in arguments, then this would be how you would do it
ChildClass()
: BaseClass()
{
}

// As stated above ;)
ChildClass( int var)
: BaseClass( var )
{
}

// This is the dtor for this class, good idea to use 'em
~ChildClass()
{
}

// In this function we modify the base class's variable in the protected area
void SetLetter( char var )
{
Letter = var;
}

void Die()
{
std::cout << "Oh teh noes!!11" << std::endl;
}
};

int main()
{
ChildClass c1;
c1.Print();

ChildClass c2( 'a' );
c2.SetLetter( 'z');
c2.Print();

// Now we can only access those member in the BaseClass class with this ptr
BaseClass* bcptr;
bcptr = &c1;
bcptr->Print();
bcptr->Die();

return 0;
}




I think that's about the basics [smile] I've probabally missed something here or there but someone will point it out. I went ahead and edited to add the main function and to make sure it compiled.

Share this post


Link to post
Share on other sites
If you inherit publicly and the other class's functions are public, you can treat either the base or derived class as a base class.


class BaseClass {
public:
void SomeFunction() { definition goes here }
}

// inherits all of BaseClass's public functions/data
class ChildClass: public BaseClass {
public:
void SomeOtherFunctionOnlyChildCanDo() { definition goes here }
}

//create a ChildClass object
ChildClass cc = ChildClass();

// use parent class's functions:
cc.SomeFunction();

// create a BaseClass object
BaseClass bc = BaseClass();

// can't use child's functions!
bc.SomeOtherFunctionOnlyChildCanDo() // error! can't do this!



Share this post


Link to post
Share on other sites
If you override a base class method, you still have access to it throught the scope resolution operator, aka "::"


class base
{
public:
virtual void foo() {...}
};

class derived : public base
{
public:
void foo() {};
...

derived der_instance;

der_instance.foo();//Call derived::foo()

der_instance.base::foo(); //Call base::foo();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement