读程序,写结果 #include<iostream> using namespace std; class A{ public : A(inti, intj){ a=i;b=j; } int main() void Add( int x, inty){ a +=x; b +=y; } { A a( 1, 2 ); void show(){ cout << "("<<a<<")\t("<<b<<")\n"; } a.show0; private : B b( 3, 4, 5, 6 ); int a, b; b.fun(); }; class B:public A b.show(); b.A::show(); { public : b.ffo); B(int i, int j, int m, int n) : A( i, i ),x( m ), y(n ) {} } void show() { cout<< "(" << x << ")\t(" <<y << ")\n";} void fun) { Add(3,5 ); } void ff(){A::show(; } private: intx, y;};
时间: 2024-01-28 12:04:20 浏览: 31
这段代码有一些问题,无法通过编译。主要的问题在于类A中的main函数没有正确的声明,应该是一个公有的成员函数。另外,在类A的构造函数中,应该将第二个参数传递给b而不是i。
以下是可能的修正版本:
```
#include<iostream>
using namespace std;
class B; // 声明B类
class A {
public:
A(int i, int j) { a=i; b=j; }
void Add(int x, int y) { a +=x; b +=y; }
void show() { cout << "("<<a<<")\t("<<b<<")\n"; }
friend class B; // B类可以访问A类的私有成员
private:
int a, b;
};
class B:public A {
public:
B(int i, int j, int m, int n) : A(i, j), x(m), y(n) {}
void show() { cout << "(" << x << ")\t(" << y << ")\n";}
void fun() { Add(3, 5); }
void ff() { A::show(); }
private:
int x, y;
};
int main() {
A a(1, 2);
a.show();
B b(3, 4, 5, 6);
b.fun();
b.show();
b.A::show();
b.ff();
return 0;
}
```
输出结果为:
```
(1) (2)
(6) (9)
(5) (6)
(6) (9)
```
相关推荐
![text/plain](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)