猿问

如下,#include<iostream> using namespace std;

修改程序
#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();
}
问题补充:求详解

九州编程
浏览 173回答 1
1回答

绝地无双

我说一下我的看法:一、在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&nbsp;namespace&nbsp;std;class&nbsp;base{private:&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;m;public:&nbsp;&nbsp;&nbsp;&nbsp;base(int&nbsp;a)&nbsp;:m(a)&nbsp;{}&nbsp;&nbsp;&nbsp;&nbsp;void&nbsp;show()&nbsp;const&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;<<&nbsp;"m:"&nbsp;<<&nbsp;m&nbsp;<<&nbsp;endl;&nbsp;&nbsp;&nbsp;&nbsp;}};class&nbsp;derived&nbsp;:public&nbsp;base{&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;n;public:&nbsp;&nbsp;&nbsp;&nbsp;derived(int&nbsp;a,&nbsp;int&nbsp;b)&nbsp;:base(a)&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n&nbsp;=&nbsp;b;&nbsp;&nbsp;&nbsp;&nbsp;}};void&nbsp;main(){&nbsp;&nbsp;&nbsp;&nbsp;derived&nbsp;obj(10,&nbsp;18);&nbsp;&nbsp;&nbsp;&nbsp;obj.base::show();}
随时随地看视频慕课网APP
我要回答