代码改错:#include<iostream> using namespace std; class base { private: int x; public: void setx(int n) { x = n; } void showx() { cout<<x<<endl; } }; class derived:private base { private: int y; public: void setxy(int n, int m) { setx(n); y = m; } void showxy() { cout<<x<<endl; cout<<y<<endl; } } int main() { derived obj; obj.setx(10); obj.showx(); obj.setxy(20, 30); obj.showxy(); return 0; }
时间: 2024-02-02 21:04:43 浏览: 57
#include <iostream> #include<iomanip> using namespace std;九九乘法表
代码有一个语法错误,需要在 derived 类的最后加上一个分号。
正确代码如下:
```
#include<iostream>
using namespace std;
class base {
private:
int x;
public:
void setx(int n)
{
x = n;
}
void showx()
{
cout<<x<<endl;
}
};
class derived:private base {
private:
int y;
public:
void setxy(int n, int m)
{
setx(n);
y = m;
}
void showxy()
{
cout<<x<<endl;
cout<<y<<endl;
}
}; // 添加分号
int main() {
derived obj;
obj.setx(10);
obj.showx();
obj.setxy(20, 30);
obj.showxy();
return 0;
}
```
阅读全文