Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inheritance Base Classes

Status
Not open for further replies.

Miquella

Programmer
Mar 11, 2005
41
US
I am writing a set of classes that inherit from one-another as follows:

Code:
        <BaseClass>
          /     \
    <Class1>  <Class2>

I have a function in Class2 that needs to accept any of the classes as a parameter. Can I just have it accept the BaseClass as the parameter (I only need access to the BaseClass variables), like this:
Code:
class Class2 : public BaseClass
{
public:
    Func(BaseClass&);
private:
    int C2;
};

-or- do I need to override the function for each class (ick):

Code:
class Class2 : public BaseClass
{
public:
    Func(BaseClass&);
    Func(Class1&);
    Func(Class2&);
private:
    int C2;
};

Please tell me that I can just do a parameter of the BaseClass, it would make things so much simpler!

-Miq
 
Yes, if you only need access to the Base class elements, then just pass a reference or pointer to the base class. That's the whole idea behind polymorphism.
 
Thank you!
I thought that this was how it worked (I've never had to do inheritance before) but I was having problems getting it to work, so I'll give it a try again.

If something goes wrong, I'll be back!

-Miq
 
1. No polymorphism in C++ without virtual functions. So it's inheritance only case, not polymorphism illustration.

Apropos: as usually, such pattern (as above) without true polymorphism is (semantically) dangerous...

2. int main(), not void main() in C++ Standard...

3. Old fashion declarations like Func() without explicit type are dangerous too...
 
Well, I didn't define my BaseClass, so you cannot assume that it is not an abstract class. The BaseClass I'm inheriting from is an abstract class (includes Virtual functions). So it is polymorphism.
Code:
class BaseClass
{
public:
    virtual bool Func( BaseClass& ) const = 0;
private:
    int C;
};

class Class1 : public BaseClass
{
public:
    bool Func( BaseClass& ) const;
private:
    int C1;
};

class Class2 : public BaseClass
{
public:
    bool Func( BaseClass& ) const;
private:
    int C2;
};

But I do apologize, I forgot to include the return types for the Func definitions: bool


-Miq
 
BTW, even though all the derived functions of a virtual function are implicitly virtual, I like to explicitly type the virtual keyword on all my derived class functions so people know it's virtual without having to look at all the base versions.
 
...and since the BaseClass (even though it is abstract) has members I'd recommend you define its destructor as virtual. But now it is perhaps getting a bit off topic, sorry for that.

/Per
[sub]
www.perfnurt.se[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top