修改程序 #include<iostream> using namespace std; class base { private: int m; public: base(i
修改程序#include<iostream>usingnamespacestd;classbase{private:intm;public:base(inta):m(a)...
修改程序
#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();
}
问题补充:求详解 展开
#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();
}
问题补充:求详解 展开
展开全部
我说一下我的看法:
一、在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();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询