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

Recommended Answers

All 3 Replies

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.