7.26派生类成员函数与基类成员函数同名

本文深入探讨了C++中派生类成员函数与基类成员函数同名的情况,通过具体示例展示了如何正确理解和使用这种特性。详细解释了基类和派生类之间的继承关系,并提供了实际操作中的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*派生类成员函数与基类成员函数同名*/


#include <iostream>
using namespace std;
class Circle    
{public:    
     void Set(int r){  radius = r;  }
    void Show( ){  cout << "Base class Circle: radius = " << radius << endl;  }
private:    
    int radius; 
};
class Cylinder: public Circle   
{public:
        void Set(int r, int h)
{   Base::Set(r);    
  height = h;  }
  void Show( )
    {   Base::Show( );    
cout << "Derived class Cylinder: height = " << height << endl;
}
private:
    int height; 
};
int main( )
{   Cylinder obj;
    obj.Set(10, 20);      
    obj.Show( );        
    obj.Circle::Set(30);       
    obj. Circle::Show( );   
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值