Results 1 to 6 of 6

Thread: Inheritance

  1. #1
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Inheritance

    Hello, nooby question but anyway! Trying to understand the point of inheritance. If I have a class fruit, I can always create:

    Fruit Banana(various parameters);

    So why create a banana class if I can simply put everything into the fruit class? If so just keep breaking classes down more and more, the way I see it, we'll just end up with a bunch of unique classes only good for one thing, so what's the point?

  2. The following user says thank you to Atomic_Sheep for this useful post:


  3. #2
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Inheritance

    Hey,

    There are so many varieties of fruits available, for example mango, apple, banana, papaya etc. But all these fruits are having different texture, properties etc. Eventually, they all comes under fruits category. So fruit is the base class. See the relations:-

    Fruit is the superclass of Banana class.
    Fruit is the superclass of Apple class.
    Banana and Apple are subclasses of Fruit class.

    Now, if we consider the IS-A relationship, we can say:
    Banana IS-A Fruit
    Apple IS-A Fruit

    It will works like a tree

    |->Sweet->Mango,apple
    Fruit->Sour->Lemon
    |->Bitter-> ...
    Last edited by sonulohani; 13th December 2013 at 05:22.
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

  4. The following user says thank you to sonulohani for this useful post:


  5. #3
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Inheritance

    Quote Originally Posted by Atomic_Sheep View Post
    So why create a banana class if I can simply put everything into the fruit class?
    why go through the trouble of creating a class? just put everything in one function then. Logically separating things is an everyday practice.

    If so just keep breaking classes down more and more, the way I see it, we'll just end up with a bunch of unique classes only good for one thing, so what's the point?
    The point is exactly what you stated. Once class, one responsibility.

    And of-course there are numerous other OOP reasons, google will help.

  6. The following user says thank you to nish for this useful post:


  7. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Inheritance

    Quote Originally Posted by Atomic_Sheep View Post
    So why create a banana class if I can simply put everything into the fruit class? If so just keep breaking classes down more and more, the way I see it, we'll just end up with a bunch of unique classes only good for one thing, so what's the point?
    This always depends on your needs.

    For example, if your program only need to work with data that is common to all fruit, says name, weight and calories, then you will only need a Fruit class and just fill the instances with appropriate data.

    Separation becomes interesting when you have data specific to each type of fruit that does not apply to other types. It becomes really interesting when you have code that needs to treat different types differently.

    Lets look at a different example: say you have two types of areas, circles and squares.
    For both you only need one length. In the case of the circle that would be the radius, for the square the side length.

    At this point you have two options:
    1) store some kind of type information
    2) create two distinct subclasses

    1) type

    Qt Code:
    1. class Area
    2. {
    3. public:
    4. enum Type {
    5. Circle,
    6. Square
    7. };
    8. Area(Type type, int length)
    9. : m_type(type), m_length(length)
    10. {
    11. switch (type) {
    12. case Circle:
    13. m_area = m_length * m_length * M_PI;
    14. break;
    15.  
    16. case Square:
    17. m_area = m_length * m_length;
    18. break;
    19. }
    20. }
    21.  
    22. private:
    23. Type m_type;
    24.  
    25. int m_length;
    26. int m_area;
    27. };
    To copy to clipboard, switch view to plain text mode 

    2)
    Qt Code:
    1. class Area
    2. {
    3. public:
    4. Area(int length)
    5. : m_length(length), m_area(0)
    6. {
    7. }
    8.  
    9. protected:
    10. int m_length;
    11. int m_area;
    12. };
    13.  
    14. class Circle : public Area
    15. {
    16. public:
    17. Circle(int length) : Area(length)
    18. {
    19. m_area = m_length * m_length * M_PI;
    20. }
    21. };
    22.  
    23. // and similar for Square
    To copy to clipboard, switch view to plain text mode 

    Now consider you want to add non-square rectangles.

    For (1) you could add a second length and ignore it for circle and square.
    For (2) you would just add it to the rectangle class. For (2) you can also add rectangle without having to change Area.

    Using base class and subclasses also allows us to name data members more appropriately

    Qt Code:
    1. class Area
    2. {
    3. protected:
    4. Area()
    5. : m_area(0)
    6. {
    7. }
    8.  
    9. protected:
    10. int m_area;
    11. };
    12.  
    13. class Circle : public Area
    14. {
    15. public:
    16. Circle(int radius : Area(), m_radius(radius)
    17. {
    18. m_area = m_radius * m_radius * M_PI;
    19. }
    20.  
    21. protected:
    22. int m_radius;
    23. };
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:


  9. #5
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Inheritance

    Thanks makes sense, overcomplicating things as per usual

  10. The following 2 users say thank you to Atomic_Sheep for this useful post:


  11. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Inheritance

    You should also consider that multiple inheritance is also a possibility. This is something you cannot mimic without classes (e.g. your banana could also be a ChildToy instance as well as a Fruit).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. The following user says thank you to wysota for this useful post:


Similar Threads

  1. C++ inheritance
    By yyiu002 in forum Newbie
    Replies: 3
    Last Post: 29th June 2010, 11:38
  2. inheritance
    By steiner in forum Qt Programming
    Replies: 4
    Last Post: 30th October 2007, 20:17
  3. inheritance
    By mickey in forum General Programming
    Replies: 11
    Last Post: 28th September 2007, 21:54
  4. How much inheritance do you use?
    By Michiel in forum General Programming
    Replies: 8
    Last Post: 1st August 2006, 22:29
  5. QSA and inheritance
    By jwintz in forum Qt Programming
    Replies: 1
    Last Post: 13th June 2006, 14:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.