假设有如下三个基类的定义: class Base1{ int a,b,c; public: void initBase1(int a,int b,int c){ this->a=a; this->b=b; this->c=c; } int getA()const{ return a; } int getB()const{ return b; } protected: int getC()const{ return c; } }; class Base2{ int x,y,z; public: void initBase2(int x,int y,int z){ this->x=x; this->y=y; this->z=z; } int getX()const{ return x; } int getY()const{ return y; } int getZ()const{ return z; } }; class Base3{ protected: int i,j,k; void initBase3(int i,int j,int k){ this->i=i; this->j=j; this->k=k; } private: int getI()const{ return i; } int getJ()const{ return j; } int getK()const{ return k; } }; 并且有派生类的定义的第一行: class Derived:public Base2,private Base3,protected Base1 main函数已经写好了,请根据基类的定义和main函数的定义,结合输入输出,完成Derived类的定义: int main(){ int a,b,c; int x,y,z; int i,j,k; cin>>a>>b>>c; cin>>x>>y>>z; cin>>i>>j>>k; Derived d; d.initBase1(a,b,c); d.initBase2(x,y,z); d.initBase3(i,j,k); cout<<d.getA()<<" "<<d.getB()<<" "<<d.getC()<<endl; cout<<d.getX()<<" "<<d.getY()<<" "<<d.getZ()<<endl; cout<<d.getI()<<" "<<d.getJ()<<" "<<d.getK()<<endl; return 0; }
时间: 2023-12-20 10:04:02 浏览: 28
根据输入输出和三个基类的定义,我们可以完成Derived类的定义:
```
class Derived : public Base2, private Base3, protected Base1 {
public:
void initDerived(int a, int b, int c, int x, int y, int z, int i, int j, int k) {
initBase1(a, b, c);
initBase2(x, y, z);
initBase3(i, j, k);
}
int getA() const { return Base1::getA(); }
int getB() const { return Base1::getB(); }
int getC() const { return Base1::getC(); }
int getX() const { return Base2::getX(); }
int getY() const { return Base2::getY(); }
int getZ() const { return Base2::getZ(); }
// Base3中的成员函数是private的,无法在Derived中访问
};
```
在main函数中,我们可以这样使用Derived类:
```
int main() {
int a, b, c;
int x, y, z;
int i, j, k;
cin >> a >> b >> c;
cin >> x >> y >> z;
cin >> i >> j >> k;
Derived d;
d.initDerived(a, b, c, x, y, z, i, j, k);
cout << d.getA() << " " << d.getB() << " " << d.getC() << endl;
cout << d.getX() << " " << d.getY() << " " << d.getZ() << endl;
// Base3中的成员函数是private的,无法在Derived中访问
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)