Like Tree1Likes
  • 1 Post By laserlight

Question about inheritance

This is a discussion on Question about inheritance within the C++ Programming forums, part of the General Programming Boards category; Suppose I have a base class with a virtual function that simply returns 140. Now suppose I have a derived ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    245

    Question about inheritance

    Suppose I have a base class with a virtual function that simply returns 140.

    Now suppose I have a derived class, that extends the base class. Here, I make the virtual function return 230.

    Suppose this fucntion is named ret_int().

    Now, if I have a function that accepts a pointer to the Base class

    Code:
    function(Base * b)
    that calls the ret_int() function.

    if I pass in a pointer to derived, will it return 140, or 230?
    Last edited by dayalsoap; 10-05-2011 at 10:22 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    Write a program to test and report your results.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    245
    very complicated position where I can't do that.. no compiler (government machine)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    Quote Originally Posted by dayalsoap
    very complicated position where I can't do that.. no compiler (government machine)
    Wait until you have access to a computer with which you can program then.

    By the way, what do you think the answer is, and why?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    245
    I mean, if you know about inheritance, you can answer the question. if you don't then what's the point of answering with "test it!" Clearly, there's a reason I can't test it right now and need to know sooner than later.

    My guess is, even though it accepts a ptr to the base class, since we've instantiated an instance of the sub-class, it will use the sub-classes functionality.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    Quote Originally Posted by dayalsoap
    I mean, if you know about inheritance, you can answer the question.
    Yes, inheritance is involved, but here we're talking about polymorphism

    Quote Originally Posted by dayalsoap
    if you don't then what's the point of answering with "test it!" Clearly, there's a reason I can't test it right now and need to know sooner than later.
    There is no point knowing if you cannot use the knowledge, and one powerful way to acquire the knowledge is to see the behaviour happen, first hand.

    Quote Originally Posted by dayalsoap
    My guess is, even though it accepts a ptr to the base class, since we've instantiated an instance of the sub-class, it will use the sub-classes functionality.
    Good guess
    In fact, I would change "even though" to "because": if the parameter was changed to be a Base instead of a Base*, there would be slicing, so the base class' implementation would be invoked.
    Last edited by laserlight; 10-05-2011 at 10:47 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by dayalsoap View Post
    very complicated position where I can't do that.. no compiler (government machine)
    Hmm.....Comeau Compiler.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    245
    Egad , it's quite typical of laymen programmers to be pedantic. Anyways, thanks for your answer, finally, pedantic layman.

    Quote Originally Posted by laserlight View Post
    Yes, inheritance is involved, but here we're talking about polymorphism


    There is no point knowing if you cannot use the knowledge, and one powerful way to acquire the knowledge is to see the behaviour happen, first hand.


    Good guess
    In fact, I would change "even though" to "because": if the parameter was changed to be a Base instead of a Base*, there would be slicing, so the base class' implementation would be invoked.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    245
    That would have been perfect! Thank you!

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    2,499
    it's interesting that you insult those from whom you seek help, and refuse to even try to find your own answer. a simple google search would have provided the answer you seek.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    Quote Originally Posted by AndrewHunter View Post
    Oh, that compiles the code, but you won't be able to run it afterwards.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    Quote Originally Posted by dayalsoap
    Egad , it's quite typical of laymen programmers to be pedantic. Anyways, thanks for your answer, finally, pedantic layman.
    You're welcome. Yeah, my degree is in computer science, not theology, and software development is my current profession, hence I am a layman. I do not know any ordained programmers though, so I cannot say if they are actually more pedantic than me
    stahta01 likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Question
    By threahdead in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2011, 02:42 PM
  2. Inheritance question
    By alexy in forum C++ Programming
    Replies: 16
    Last Post: 11-06-2008, 02:46 PM
  3. Inheritance question.
    By antex in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2006, 09:59 AM
  4. Different question about inheritance
    By Marcos in forum C++ Programming
    Replies: 5
    Last Post: 03-02-2006, 02:52 PM
  5. Inheritance question
    By CompiledMonkey in forum C++ Programming
    Replies: 5
    Last Post: 12-19-2003, 12:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21