Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What does this program do, and how does it do that?

I am having trouble figuring out why this program works. I wrote it based on my notes (OOPP and classes) but I do not understand how exactly it works? I would appreciate any help!

Here is the code:

#include <iomanip>
#include <iostream>

using namespace std; 

class Base{
    public:
        void f(int) {std::cout<<"i";}
};
class Derived:Base{
    public:
        void f(double){std::cout<<"d";}
};
int main(){
    Derived d;
    int i=0; 
    d.f(i);
}

I have tried making cout statements to show me how everything is passed and runs, but it will not allow me to cout anything.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This program defines a class called Base, which has a member function called f that takes an int parameter. It also defines a class called Derived, which inherits from Base and has a member function called f that takes a double parameter.

In the main function, an object of type Derived is created, and an int variable is initialized to 0. The member function f is then called on the Derived object, passing in the int variable as a parameter.

When the member function f is called on the Derived object, the compiler looks for a matching function signature in the Derived class. Since there is a function with the same name and parameter list in the Derived class, that function is called. The function in the Derived class prints out a "d", indicating that it was called.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading