修改程序#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
提交
取消