0

I have a question (part of my homework) which im unsure of the code's behavior.

I got a class Father, which owns a function "A", function "A" prints "Hello Father".
I got a second class -> class son:public Father, which does not owns function "A".
I got a third class -> class grandson:public son , which owns function "A" which prints "Hello grandson".

Function "A" is not virtual. Please ignore compiling errors, I didnt want to put here a 80 lines of code.

I have another function :

void justPrint(const son& a) {
   a.A;
}

Now, what will be printed in the following call :

grandson jeff;
justPrint(jeff);

I'm a bit confused, son dont have the print function (A), so he suppose to call to Father::A (son is a father..)
but, we send jeff (grandson) to the function which recieves son.. and grandson is a son..

I think that it will print

"Hello Father"

But i'm so confused... Will appericiate any help and explenation..

Second thing, What will happen if i will make the following call :

justPrint(1);
3
  • 1
    Have you tried these to see the real outputs? Commented Jul 1, 2013 at 11:45
  • 1
    Actually justPrint function as described will not compile - or at least it won't call function A. And justPrint(1), assuming there is only the function above also will not compile (as 1 is not a valid class son object). Commented Jul 1, 2013 at 11:47
  • 2
    And to be able to answer the question (ignoring errors mentioned above), we need to know if function A in the class father is virtual or not. Commented Jul 1, 2013 at 11:49

2 Answers 2

1

I try to make a short code to solve your problem, but I don't have it (using "g++ test.cpp -fpermissive" to compile)

#include <iostream>

using namespace std;

class Father
{
    public:
    void A(){
        cout<<"Hello Father"<<endl;
    }
};

class Son : public Father
{
};

class GrandSon : public Son
{
    public:
    void A()
    {
        cout<<"Hello GrandSon"<<endl;
    }
};

void justPrint(const Son& a)
{
    a.A();
}

int main(int argc, char* argv[])
{
    GrandSon jeff;
    justPrint(jeff);
    return 0;
}

Maybe you have put A in private?


Output: Hello Father

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, This is what i think it should print (as you can see in my answer), But I was not sure.
It's because you have use "class Son : public Father" to build Son. So Son have have all public methode that Father have (include A()). Morover, Son don't have a A(), so it use the heritance tree, and take the first one "A" (here in Father::A) with the same signature.
0
void justPrint(const son& a) {
   a.A;
}

justPrint(1); if Class son contains single argument constructor with int as i/p.It will call that .

2 Comments

class son does contains a single argument constructor. Can you please explain me why it will be called ?
It will be called if it takes an int, unless you mark it explicit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.