Я написал следующий код:
// constructors and derived classes
#include <iostream>
using namespace std;
class Mother
{
public:
int age;
Mother()
{
cout << "Mother: no parameters: \n"
<< this->age << endl;
}
Mother(int a)
{
this->age = a;
}
void sayhello()
{
cout << "hello my name is clair";
}
};
class Daughter : public Mother
{
public:
int age;
Daughter(int a)
{
this->age = a * 2;
};
void sayhello()
{
cout << "hello my name is terry";
}
};
int greet(Mother m)
{
m.sayhello();
}
int main()
{
Daughter kelly(1);
Son bud(2);
greet(kelly);
}
и мой вопрос таков:
Поскольку Келли является экземпляром класса, производного от Mother, для меня имеет смысл, что я могу передать его в функцию, которая требует объект типа mother, т.е. здороваться. Мой вопрос заключается в следующем: можно ли вызвать функцию sayhello изнутри greet, чтобы она
он будет говорить «привет, меня зовут Терри» вместо «привет, меня зовут Клэр».