/给出下面程序输出结果。 #include <iostream> using namespace std; class Base { private: int Y; public: Base(int y = 0) { Y = y; cout << "Base(" << y << ")\n"; } ~Base() { cout << "~Base()\n"; } void print() { cout << Y << ""; } }; class Derived :public Base { private: int Z; public: Derived(int y, int z) :Base(y) { Z = z; cout << "Derived(" << y << "," << z << ")\n"; } ~Derived() { cout << "~Derived()\n"; } void print() { Base::print(); cout << Z << endl; } }; void main() { Derived d(10, 20); d.print(); }
时间: 2023-06-23 17:08:49 浏览: 95
程序输出结果为:
Base(10)
Derived(10,20)
10 20
其中,首先会调用 Derived 类的构造函数,该构造函数调用了 Base 类的构造函数,因此会输出 "Base(10)" 和 "Derived(10,20)"。接着调用 d.print() 函数,该函数先调用 Base 类的 print() 函数输出 Y 的值,再输出 Z 的值,因此最终的输出结果为 "10 20"。最后退出程序时会调用 Derived 类的析构函数和 Base 类的析构函数,分别输出 "~Derived()" 和 "~Base()"。
相关问题
代码改错:#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; }
代码有一个语法错误,需要在 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;
}
```
代码改错:#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; }
代码有一个语法错误,需要在 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;
}
```
相关推荐
![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)