【发布时间】:2021-11-27 07:02:51
【问题描述】:
我创建了一个程序,其中有两个班级,即母亲和女儿。女儿班继承自母亲班。这两个类具有相同的方法名称,但它们打印不同的数据。现在在 main 方法中,我创建了一个子类的对象,并通过女儿的对象调用了 display(),它覆盖了母亲的 display() 方法。
现在我想通过女儿的对象在main方法中调用妈妈的display()方法,那怎么做呢?可以这样做吗?
我尝试过这样做,但它显示错误(请参阅最后第 3 行代码)
#include <iostream>
using namespace std;
class mother{
public:
void display(){
cout<<"Mother"<<endl;
}
};
class daughter : public mother{
public:
void display(){
cout<<"Daughter"<<endl;
}
};
int main(){
daughter rita;
mother mother;
rita.display();
mother::rita.display(); //error
return 0;
}
【问题讨论】:
标签: c++