修改程序#include<iostream>using namespace std;class base{private:int m;public:base(int a):m(a){}void show() const{cout<<"m:"<<m<<endl;}};class derived:private base{int n;public:derived(int a,int b):b(a){n=b;}};void main(){derived obj(10,18);obj.show();}问题补充:求详解
1 回答
绝地无双
TA贡献1946条经验 获得超4个赞
我说一下我的看法:
一、在derived(int a,int b):b(a)这一构造函数应该改为derived(int a,int b):base(a);
二、在class derived:private base这一句中,将base定义为了私有成员,所以派生类derived的对象无法访问base的成员函数。
所以我将这一句改为class derived :public base
然后下边的调用改成了obj.base::show();
附代码
#include<iostream> using namespace std; class base { private : int m; public : base( int a) :m(a) {} void show() const { cout << "m:" << m << endl; } }; class derived : public base { int n; public : derived( int a, int b) :base(a) { n = b; } }; void main() { derived obj(10, 18); obj.base::show(); } |
添加回答
举报
0/150
提交
取消