How can i call a derived class method using a base class object ?

I understand how this goes in the opposite direction. But for various reasons i want to use a base class object to call a derived class method

Let's say we have 2 classes, together representing a person's data (x= age and y=height) :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    class Person{

protected:
    int x;

public:

    Person()
    {
        x=0;
    }

    Person(int x1)
    {
        x=x1;
    }

    Person(const Person &o1)
    {
        x=o1.x;
    }

    Person &operator=(const Person &o1)
    {
      x=o1.x;
      return *this;
    }

    int get_x() const
    {
        return x;
    }

    virtual float get_y() const = 0;

    virtual ~Person() = default;

};


class Info: public Person{

protected:
    float y=10;

public:

    Info(): Person()
    {
        y=0.0;
    }

    Info(int x1, float y1): Person(x1)
    {
        y=y1;
    }

    Info(const Info &o2): Person(o2)
    {
        y=o2.y;
    }

    Info &operator=(const Info &o2)
    {
        y=o2.y;
        Person::operator=(o2);
        return *this;
    }

    float get_y() const override
    {
        return y;
    }
};

Since those 2 classes are about a person's data, and i have a Person object, i want to use this object to find out his height as well ( likely calling the method get_y() from it's derived class, Info)

From what i've readed, it has to do something with virtual methods ( pure virtual methods). So i tried to use a pure virtual method but now the problem is that i can't call that method in main properly (im too stupid to figure it out how to):
1
2
3
4

    Person* o1;
    o1->get_y(); /// didn't work..
    return 0;



How can i do it ? (i will apreciate if you can show me the main of the program too).
Last edited on
First, you should avoid using variable names like 'x' and 'y' (outside limited cases, like actual coordinate system functions). Why not call them what they are -- "age" and "height"?

Second, in my opinion you are constructing the wrong class hierarchy, and it's leading you down the wrong path. Inheritance is supposed to represent an "is-a" relationship. A Dog "is an" Animal. But an Info is not a Person. More properly, a Person has Info associated with him or her. This is a has-a relationship, or aggregation.

What I would do instead: Start with a Person class. Let a Person have Age and Height properties (whether you simply make these be ints/doubles, or whole classes onto themselves, is up to you). Or, make an Info class that has age and height properties.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Example program
#include <iostream>
#include <string>

struct Info
{
    int age;
    double height;
};

class Person
{
  public:
    Person(int age, double height)
     : info {age, height} { }
    
    const Info& getInfo()
    {
        return info;   
    }
    
  private:
    Info info;
    
};

int main()
{
    Person bobby(13, 165.0);
    std::cout << bobby.getInfo().age << '\n';
}
Last edited on
Topic archived. No new replies allowed.