0

I have a problem in understanding one important aspect of the c++. see the example below:

class human
{
protected:
    int age;
public:
    human(int a){age=a;}
    void f(){cout<<"\n the age of human ="<<age;}
};

and the class below is derived from above class:

class student: public human
{
private:
    int num;
public:
    student(int b, int c):human(b){num=c;}
    void g(){cout<<"\n the student with age "<<age<<" has " <<num;}
};

now I want to use these classes in the code below:

int main()
{
    human h1(10);
    student s1(11,4);
    human* p=&h1;
    p->f();
    p=&s1;
    p->g();
}

but the compiler gives me error on the p->g(). what is the problem?

2
  • 1
    You are trying to access a child method from a parent class. Read up on polymorphism, class pointers compatibility and virtual functions. Commented Jan 3, 2017 at 7:31
  • @RawN I know them. in fact the child method was void fand I changed that to see the effects. we have a pointer that points to a child class, why it can't find a method called g. Commented Jan 3, 2017 at 7:33

5 Answers 5

1

Your base class pointer still points to a base class object human* p=&h1;. Try something like this:

#include <iostream>

class human
{
protected:
    int age;
public:
    human(int a){ age = a; }
    void f(){ std::cout << "\n the age of human =" << age; }
    virtual void g(){}
};

// class student code here...

int main()
{
    human* p = new student(11, 4);
    p->f();
    p->g();
    delete p;
    return 0;
}

or instead use the s1 object and not the h1:

student s1(11, 4);
human* p = &s1;
Sign up to request clarification or add additional context in comments.

Comments

0

The function g() is not part of the interface of the class human. p is a human*. If you upcast p to a student*, it will work.

1 Comment

But the compiler doesn't know that. It only knows that the declaration of your pointer is human and that's what matters, not the inheritance, so for this to work you have to cast to student
0

The compiler doesn't know that your pointer is of student, it only sees the declaration as human. To fix this you need to cast it to a student pointer.

((student*)p)->g();

Comments

0

You can announce a virtual g in the Human.

class human {
    public:
        virtual void g() {}
    ... ...
};

1 Comment

I have put the address of student in the p. why it can't access g. I know that having g in the base class can solve the problem. But why this is the case?
0

In your example main() has two objects: human h1 and student s1.

int main()
{
    human h1(10);
    student s1(11,4);
    human* p=&h1;
    p->f();
    p=&s1;
    p->g();
}

Aside from the constructor, the declaration of the parent class human has only the function f() and the child class student, only adds the one function g().

The human pointer can look up only the function contained in it's class. Any information in the child's look up table is unknown to the parent because human was never told anything about the child class.

Passing the child object's reference &s1 to the parent pointer means that the parent can only look up the function it already knows. This is sometimes called object slicing. The child function g() has no place keeper in the parent's class. But, the child has both functions in it's look up table.

In other words, the child knows both itself and it's parent because it is declared class student: public human but the parent class knows only itself because it is merely declared "class human"

As Bauss says, it is possible to "upcast" the human pointer but I would not recommended that style. It will only confuse what type of pointer is being actually used.

This is the way I would rewrite main:

int main()
{
    human h1(10);
    human* p=&h1;
    p->f();  
    /* prints " the age of human = 10" */

    student s1(11,4);
    student* q=&s1;
    q->f(); 
    /* prints " the age of human = 11" */
    q->g(); 
    /* prints " the student with age 10 has 4" */
} 

Comments

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.