# a few questions about inheritance

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

## Recommended Posts

I've never messed with inheritance before,so I only have a basic knowledge of how it works. All of this is C++. What I'm trying to accomplish is a sort of factory-class design, where one class can store multiple types in one vector. So say I have a base class, would everything in it need to be virtual? and can I assign arguments to a virtual function that will automaticall be assumed amonst all the derived classes but still allow me to change the function it's self. here's an example of a GUI setup:
class CControl
{
public:
virtual std::string Init(ptr*, std::string, std::string);
virtual std::string Do_Something();
};

class CButton : public CControl
{
public:
std::string Init(ptr* foo, std::string foo, std::string foo);
std::string Do_Something();
}


Now when I go to store the object would I create the vector just like so?
CButton button;
std::vector<CControl> controls;

controls.push_back(button);

controls[0].Init(&foo, "foo", "foo")
controls[0].Do_Something();


is all that right?

##### Share on other sites
Quote:
 So say I have a base class, would everything in it need to be virtual?

No. "virtual" means that the deriving class can use that function's name for a different function, but doesn't have to. A deriving class gets non-virtual functions from its base class.

If there are no differences between CControl::Init(...) and CButton::Init(...) you needn't include that function in the CButton class declaration. Even if your CButton declaration looked like this:

class CButton : public CControl{};

It would have the Init and Do_Something functions that CControls have.

##### Share on other sites
Quote:
 Original post by xissburgyou should store pointers in the std::vector instead.*** Source Snippet Removed ***

I will it was just easier to right it that way. So I worte something up real quick and I'm getting linking errors.

mcontrol.h
class CMControl{public:    virtual std::string Init(std::string,std::string);    virtual std::string Update(std::string,std::string,std::string);    virtual std::string Display();    virtual std::string Free();};

mcbutton.h
#include "mcontrol.h"class CMCFrame : public CMControl{private:    ... private data ...public:    std::string Init(std::string,std::string);    std::string Update(std::string,std::string,std::string);    std::string Display();    std::string Free();};

mcbutton.cpp
...std::string CMCButton Init(std::string foo ... etc){    ... do something ...}...

Do I need to include a source file for CMControl? I thought if every function was virtual you didn't have to?

*EDIT*
I figured it out, I needed to add "=0" to the end of all my virtual classess. From what I understand this makes the functions abstract therefore, I could never use the clas CMControl on it's own?

##### Share on other sites
Quote:
 Original post by freeworldDo I need to include a source file for CMControl? I thought if every function was virtual you didn't have to?

Only if it's pure virtual:

virtual SomeType foo(SomeType argument) = 0;

without the = 0 it's not pure virtual and needs a method body defined.

-me

##### Share on other sites
Also, to take advantage of polymorphism, you should declare your vector as pointers or references to your CControl, not just CControl objects. I do not think vector of references will work so use pointers (or smart pointers).