单继承函数调用问题

 A <- B <- C       C类继承B类继承A类

#include<iostream>
using namespace std;

class A                                           //A类 包含fun()和add()函数
{
private :
	int aa;
public :
	void fun();
	void add();
};

class B  : public A				//B类 包含fun()函数
{
private :
	int bb;
public :
	void fun();
};

class C  : public B				 //C类 无
{
private :
	int cc;
};


void A::fun()
{
	cout<< "调用A.fun()函数"<<endl;

}

void A::add()
{
	cout<< "调用A.add()函数"<<endl;

}


void B::fun()
{
	cout<< "调用B.fun()函数"<<endl;

}



int main()
{
   A a;
   B b;
   C c;

   a.fun();                   //调用A.fun()函数
   b.fun();                   //调用B.fun()函数
   b.add();                   //调用A.add()函数
   c.fun();                   //调用B.fun()函数
   c.add();                   //调用A.add()函数

    return 0;
 }

派生类中的名称优先于直接或间接祖先类中的相同名称。

如果C优先调用B.fun()函数而不是A.fun()函数。

猜你喜欢

转载自blog.csdn.net/weixin_40539125/article/details/82899476