-1

I am learning about inheritance and I am a bit confused by one of the problems I got. In this problem, why is the output The color is orange and not The color is orangeThecolor is red?

using namespace std;

class Red{
    public: void print(){
        cout<<"The color is red";
    }
};

class Orange: public Red{
    public: void print(){
        cout<<"The color is orange";
    }
};

class Yellow: public Orange{};

int main()
{
    Orange colorOrange;
    colorOrange.print();
    
    return 0;
}
Jason
  • 36,170
  • 5
  • 26
  • 60
  • Inheritance doesn't mean that all functions of the base classes are invoked automatically, if the derived classes functions are called. You must have misunderstood something. – πάντα ῥεῖ Sep 17 '22 at 06:40
  • So if I called an instance of the Yellow class and I tried the print function, would it not print anything at all? Or would it still print "The color is orange" – ahahahahana Sep 17 '22 at 06:41
  • Each of the functions overrides the ancestor. So if you want it to call the ancestor you must do so. For example, in the orange print function , add a Red::print() call at the bottom to call the base class. – Andrew Truckle Sep 17 '22 at 06:52
  • As for your question in your last comment - just try it! – Andrew Truckle Sep 17 '22 at 06:53
  • 1
    @ahahahahana The `print` function in the derived class **hides** the version from the base class. Refer to a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and [how to ask](https://stackoverflow.com/help/how-to-ask) where the first step is to *"search and then research"* and you'll find plenty of dupes for this. The books listed above are also available as PDFs. – Jason Sep 17 '22 at 07:08
  • 1
    @AndrewTruckle only virtual functions can be overriden. – n. m. could be an AI Sep 17 '22 at 14:35
  • @n.1.8e9-where's-my-sharem.virtual I get you. But I wasn’t far off in saying to call base. Much like the answer. – Andrew Truckle Sep 17 '22 at 14:58
  • @n.1.8e9-where's-my-sharem. Polymorphism Isn’t it? Use base class pointer which calls the derived class overridden virtual function. – Andrew Truckle Sep 17 '22 at 15:09

1 Answers1

0

why is the output "The color is orange" and not "The color is orangeThecolor is red"?

Because when you wrote colorOrange.print(); lookup starts from the derived class and then it finds the Yellow::print() and so the search stops and this already found print is called/used. Thus you get the output The color is orange and nothing more.

In other words, the print inside the derived class hides the version from the base class.

If you want to call the Red::print() in addition to the Yellow::print() then you could add a call to base' print version by adding Red::print() as shown below:

class Orange: public Red{
    public: void print(){
        cout<<"The color is orange";
        //added this to call Red::print() 
        Red::print();
    }
};

Demo

Jason
  • 36,170
  • 5
  • 26
  • 60