1 Replies - 164 Views - Last Post: 15 November 2012 - 09:58 AM Rate Topic: -----

#1 ogrishmania  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-November 12

Multiple class inheritance approach ideea. Bad?

Posted 15 November 2012 - 09:03 AM

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 body(size, 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

I do not ask for code but for a approach or ideea. This program must have more than one class.

Is This A Good Question/Topic? 0
  • +

Replies To: Multiple class inheritance approach ideea. Bad?

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,603
  • Joined: 05-May 12

Re: Multiple class inheritance approach ideea. Bad?

Posted 15 November 2012 - 09:58 AM

One of the major points of modern uses of classes is to let the class tell the reader the kind of "IS A" relationship between a base class and a derived class. In your plan above where you are planning on using classes to keep track of various phone characteristics, you are denoting a "HAS A" relationship rather than an "IS A" relationship.

Some old school C++ programmers didn't really care about the relationship being "IS A" or "HAS A". They simply used multiple inheritance to save from duplicating code. More enlightened programmers now tend to care about the relationships and will prefer composition over inheritance.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1