2 Replies - 888 Views - Last Post: 29 April 2011 - 06:59 AM Rate Topic: -----

#1 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

inheritance: how to use son's method from the father's pointer

Posted 29 April 2011 - 03:15 AM

Hi folks,

I have the following hierarchic connection :

Father : Product
Son : Music
inner Class within Music called Node

Now,in main , when I declare the following :

Product *album = new Music(.....);


I have a method called "addSongsToAlbum(.........)" .

However ,I cannot call 'addSongsToAlbum' with the pointer 'album' , i.e. :

album->addSongsToAlbum(.....);


because 'album' is a Product pointer , and I must do that using downcasting , meaning :
((Music*)album)->addSongsToAlbum(.....);


The problem is ,my lecturer doesn't allow me using that concept (he says it's bad programming) ,
so , is there something else that I can do except putting 'addSongsToAlbum' in the Father 'Product' and inherit
the method from that Father ?

thanks in advance,Ron

This post has been edited by passer_by: 29 April 2011 - 03:18 AM


Is This A Good Question/Topic? 0
  • +

Replies To: inheritance: how to use son's method from the father's pointer

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: inheritance: how to use son's method from the father's pointer

Posted 29 April 2011 - 04:30 AM

Using the terms father and son may not be the best words to describe a hierarchical structure in C++, generally, the terms base and derived are used, just to clarify. Maybe this is just a preference but either way, I'm going to use those terms.

If I understand you correctly, you're trying to call a method of an inherited class using an object of type "product" (your base class). This is a perfect example where using polymorphism is applicable, which involves the use of the virtual keyword.

If you declare addSongsToAlbum in your base class as virtual, then you can overide it(addSongsToAlbum) in an inherited class, which can then be called by your object of type "product" (assuming it points to an instance of an inherited class which overides that method).

Here's a good tutorial on polymorphism, mcu better than I good possible do.
Was This Post Helpful? 2
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: inheritance: how to use son's method from the father's pointer

Posted 29 April 2011 - 06:59 AM

Over-generalisation is a bad thing. If deriving your class from an abstract base class is too limiting and prevents you from being able to do what you actually want to do, then you've defeated the point of OO entirely and your design is wrong.


You might would be better off treating album as a Music object rather than a Product, unless all your other classes which derive from Product also support the concept of addSongsToAlbum then you don't gain anything at all by trying to access the function via a Product* interface.

This post has been edited by Bench: 29 April 2011 - 07:04 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1