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.
ravenous
Practically a Master Poster
681 posts since Jul 2005
Reputation Points: 286
Solved Threads: 111
Skill Endorsements: 9
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.
firstPerson
Industrious Poster
4,046 posts since Dec 2008
Reputation Points: 851
Solved Threads: 626
Skill Endorsements: 15