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;
}
virtualfloat 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):
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.