1,105,380 Community Members

Multiple class inheritance approach. How bad is it?

Member Avatar
ogrishmania
Newbie Poster
12 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm sort of a begginer in classes.
I must implement a simple problem: chosing a phone.

I thought that I should make a program in which a user can select some conditions, like must have wifi or not, android or other, etc. and return a list with the phones that complies to those conditions. But that seemend a bit too booring and simple. So I asked my teacher how to make it better. He suggested to use a base class that inherits: a class for android smartphones, a class for iPhones, etc. 
Now this is a good idea but It's not mine so I thought if this kind of mechanism exists in c++ then why not make a class for each characteristic: a class for display -type, size, etc., a class for bodysize, weight, feature keys, etc. and a basic class Phone that inherits every one of the characteristic classes. 
So I starded typing code and then stopped and googled class inherintance. To my dissapointment it's not reccomended more than one inheritance and each base class must be used with a pointer in the derivate class. 

How can I implement this ideea of mine in a good form of code?

In the first stage I will make just a simple console program, with a menu: 
a. Show all phones (from txt files at first)
b. Compare 2 terminals
c. Insert characteristics 
d. Administrative 
        a.Add phone
        b.Remove phone 
        c.View files
etc.

sorry for the code tag

Member Avatar
ravenous
Practically a Master Poster
682 posts since Jul 2005
Reputation Points: 251 [?]
Q&As Helped to Solve: 112 [?]
Skill Endorsements: 13 [?]
 
0
 

You can think of two main kinds of relationships between classes in C++

  • A class is a kind of another class
  • A class has other classes inside it

The classic "is a" example uses animals. A dog is an animal. A cat also is an animal. So, for this example, Animal would be your base class and Dog and Cat would inherit from it:

class Animal{};

class Dog : public Animal {};
class Cat : puclic Animal {};

Great. Now, animals have organs. They're not kind of organs, but they include them inside themselves. Maybe the Animal class contains a std::vector of organs:

class Organ {};

class Animal
{
    std::vector< Organ* > M_organs;
};

Obviously, there are different kinds of organ: heart, lungs etc:

class Heart : public Organ {};
class Lung : public Organ {};

Since Heart and Lung inherit from Organ, they can both be accessed by a pointer to an Organ (i.e. they can both be stored in the M_organs vector.

So, the question that you could ask yourself, is "Is a phone a kind of operating system?". If the answer is "no", then you probably shouldn't inherit Phone from Android. However, you could have an OperatingSystem class, which AndroidOS, IOS and WindowOS could all inherit from. Your Phone class could then hold a pointer to an OperatingSystem.

Member Avatar
ogrishmania
Newbie Poster
12 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

First of all thank you for your response, I started to suspect my question didn't made sense to other people.
Even though I understood your operating system example, I woudn't go that deep. I want just an operatingSystem class and some other but like the os class. Not android->os->phone just simple os->phone, display->phone, body->phone, etc. Also like I stated in the first post, I didn't really study class inheritance just what I read in ~10 min. But if what I wrote in this post makes any sense please let me know. So I need pointers for every class.

Member Avatar
firstPerson
Industrious Poster
4,052 posts since Dec 2008
Reputation Points: 761 [?]
Q&As Helped to Solve: 634 [?]
Skill Endorsements: 24 [?]
 
0
 

So first you should know that although multiple inheritance is frowned upon by some people, that doesn't necessairly make it wrong. Second, it is an accepted design if you inherit from multiple interface.

So simple example( the size, display ) you can characeterize each property into some catagory and make it an interface like so

struct IRender{
  virtual void render() const = 0; 
};

struct IBodyFeature{
  virtual int mass() const = 0;
  virtual int weight() = const = 0;
};

class Object: public IRender , IBodyFeature{
 //...
};

Now for your example

In the first stage I will make just a simple console program, with a menu: 
a. Show all phones (from txt files at first)
b. Compare 2 terminals
c. Insert characteristics 
d. Administrative 
        a.Add phone
        b.Remove phone 
        c.View files
etc.

Have a base phone class

class PhoneInterface{
     public:
       enum Type{ IPHONE, ANDROID };
     public:
     virtual bool hasWireless() const = 0;
     //...
};

class IPhone : public PhoneInterface{ ... }
class AndroidPhone: public PhoneInterface  { ...}

Implement that then you can go on from there.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: