C++ 如何从子类对象访问超类函数
有没有办法从子类访问超类方法? 我将Apple类作为Fruit类的子类,但我无法从Apple类的对象访问Fruit类中的setName函数。 你能给我一些建议吗C++ 如何从子类对象访问超类函数,c++,C++,有没有办法从子类访问超类方法? 我将Apple类作为Fruit类的子类,但我无法从Apple类的对象访问Fruit类中的setName函数。 你能给我一些建议吗 #include <iostream> #include <string> using namespace std; class Fruit { public: Fruit(string str) { cout << "Fruit class" << en
#include <iostream>
#include <string>
using namespace std;
class Fruit {
public:
Fruit(string str)
{
cout << "Fruit class" << endl;
name = str;
cout << name << endl;
}
void setName(string str) {
name = str;
}
private:
string name;
};
class Apple:Fruit {
public:
Apple() :Fruit("apple"){
cout << "Apple class" << endl;
}
void setName(string str) {
name = str;
}
};
int main()
{
Apple apple;
apple.setName("Orange"); //I can not access to setName function from apple
return 0;
}
#包括
#包括
使用名称空间std;
等级水果{
公众:
水果(串str)
{
不能像这样使用public
继承:
class Apple : public Fruit
如果未指定,类
的默认可见性为私有
。这就是为什么您无法访问基类的公共
成员的原因,因为它们现在由于其私有
可见性而私有
与类
相反,结构
的默认可见性为公共
,即:
struct Base{};
派生结构:基{};//公共继承
在代码中,重写的setName()派生类中的
方法是冗余的,因为它不能直接操作私有数据成员名称
。您必须使用基类方法在重写的方法中设置名称
。到目前为止,您没有在该方法中执行任何其他操作,因此不需要它
这是您的工作代码():
#包括
#包括
使用名称空间std;
等级水果{
公众:
水果(串str)
{
不能像这样使用public
继承:
class Apple : public Fruit
如果未指定,类
的默认可见性为私有
。这就是为什么您无法访问基类的公共
成员的原因,因为它们现在由于其私有
可见性而私有
与类
相反,结构
的默认可见性为公共
,即:
struct Base{};
派生结构:基{};//公共继承
在代码中,重写的setName()派生类中的
方法是冗余的,因为它不能直接操作私有数据成员名称
。您必须使用基类方法在重写的方法中设置名称
。到目前为止,您没有在该方法中执行任何其他操作,因此不需要它
这是您的工作代码():
#包括
#包括
使用名称空间std;
等级水果{
公众:
水果(串str)
{
cout您的Apple
类正在使用private继承(未指定继承类型时的默认值),因此foult
的所有public
方法都是Apple
中的private
,因此main()无法访问
调用。您需要使用public继承来解决此问题
此外,无需在Apple
中重新实现setName()
。从four
继承的setName()
在Apple
中作为公共方法继承后就足够了
苹果类:公共水果{
公众:
苹果:水果(“苹果”){
cout您的Apple
类正在使用private继承(未指定继承类型时的默认值),因此foult
的所有public
方法都是Apple
中的private
,因此main()无法访问
调用。您需要使用public继承来解决此问题
此外,无需在Apple
中重新实现setName()
。从four
继承的setName()
在Apple
中作为公共方法继承后就足够了
苹果类:公共水果{
公众:
苹果:水果(“苹果”){
不能使用公共继承吗
class Apple:public Fruit
类内默认值为Private
class Apple:Fruit
与
class Apple:private Fruit
您在此处设置为私有,所以即使是公共的,也不能通过Apple对象访问水果成员
class Apple:public Fruit
{
public:
Apple() :Fruit("apple")
{
cout << "Apple class" << endl;
}
};
苹果类:公共水果
{
公众:
苹果:水果(“苹果”)
{
不能使用公共继承吗
class Apple:public Fruit
类内默认值为Private
class Apple:Fruit
与
class Apple:private Fruit
您在此处设置为私有,所以即使是公共的,也不能通过Apple对象访问水果成员
class Apple:public Fruit
{
public:
Apple() :Fruit("apple")
{
cout << "Apple class" << endl;
}
};
苹果类:公共水果
{
公众:
苹果:水果(“苹果”)
{
不能首先,把班级改成苹果
class Apple: public Fruit
然后,使用这个方法
apple.Fruit::setname()
首先,把班级改为苹果
class Apple: public Fruit
然后,使用这个方法
apple.Fruit::setname()
小挑剔:私有继承默认仅用于class
es,因为private
是class
成员的默认可见性;
Fruit将成为Apple的公共父级。@bitmask:谢谢您的输入!我将它集中在类上,因为OP的问题没有任何对结构的引用。@Miria:不客气!我在我的回答中添加了一个关于继承的相关链接。您可以参考它。如果有助于关闭此线程,请接受答案。@bitmask:我也更新了关于struct
的答案。再次感谢!小挑剔:私有继承默认只用于class
es,因为private
是class
成员的默认可见性。在struct Apple:Fruit{};
Fruit将成为Apple的公共父级。@bitmask:谢谢您的输入!我将它集中在类上,因为OP的问题没有任何对结构的引用。@Miria:不客气!我在我的回答中添加了一个关于继承的相关链接。您可以参考它。如果有助于关闭此线程,请接受答案。@bitmask:我也更新了关于struct
的答案。再次感谢!他们不需要使用Apple.Fruit::setname()
@RemyLebeau是的,没错,我只想展示通过子对象访问超类方法的方法..但键入了错误的字母..Th