1、分析并调试下列程序,写出程序的输出结果,并解释输出结果。 #include<iostream> using namespace std; class B{ public: virtual void f1(double x) { cout<<"B::fl(double)"<<endl; } void f2(double x) { cout<<"B::f2(double)"<<2*x<<endl; } void f3(double x) { cout<<"B::f3(double)"<<3*x<<endl; } }; class D:public B{ public: virtual void f1(double x) { cout<<"D::fl(double)"<<x<<endl; } void f2(int x) { cout<<"D::f2(int)"<<2*x<<endl; } void f3(double x) { cout<<"D::f3(double)"<<3*x<<endl; } }; int main() { D d; B*pb=&d; D*pd=&d; pb->f1(1.23); //语句1 pd->f1(1.23); //语句2 pb->f2(1.23); //语句3 pb->f3(1.23); //语句4 pd->f3(1.23); //语句5 return 0; }
时间: 2024-04-26 21:27:03 浏览: 45
程序的输出结果为:
D::fl(double)1.23
D::fl(double)1.23
B::f2(double)2.46
B::f3(double)3.69
D::f3(double)3.69
输出结果的解释如下:
1. 语句1调用了指向B类对象的指针pb的f1函数,由于B类中的f1函数是一个虚函数,因此会调用D类中重写的f1函数,输出结果为D::fl(double)1.23。
2. 语句2调用了指向D类对象的指针pd的f1函数,由于D类中重写了B类中的f1函数,因此会调用D类中的f1函数,输出结果为D::fl(double)1.23。
3. 语句3调用了指向B类对象的指针pb的f2函数,该函数在B类和D类中都有定义,但是参数类型不同,因此会调用B类中的f2函数,输出结果为B::f2(double)2.46,即将传入的参数值乘以2。
4. 语句4调用了指向B类对象的指针pb的f3函数,该函数在B类和D类中都有定义,但是没有被重写,因此会调用B类中的f3函数,输出结果为B::f3(double)3.69,即将传入的参数值乘以3。
5. 语句5调用了指向D类对象的指针pd的f3函数,该函数在B类和D类中都有定义,但是被D类重写,因此会调用D类中的f3函数,输出结果为D::f3(double)3.69,即将传入的参数值乘以3。
相关问题
补全程序,并写出程序运行结果。 #include <iostream> using namespace std; class Class { public : Class ( int...
补全的程序如下:
```cpp
#include <iostream>
using namespace std;
class Class {
public:
Class(int n) { cout << "Class(" << n << ")" << endl; }
Class(int n1, int n2) { cout << "Class(" << n1 << ", " << n2 << ")" << endl; }
};
int main() {
Class obj1(10);
Class obj2(20, 30);
return 0;
}
```
程序运行结果为:
```
Class(10)
Class(20, 30)
```
解释:
- 程序中定义了一个名为 Class 的类,该类有两个构造函数,一个接收一个整型参数,另一个接收两个整型参数。
- 在主函数中,我们分别创建了两个对象 obj1 和 obj2,分别用第一个和第二个构造函数进行初始化。
- 在程序运行时,先输出了 obj1 的构造函数中的信息,再输出了 obj2 的构造函数中的信息。
分析下列程序,根据输出结果完善填空。 程序运行结果为:10 20 30 #include <iostream > using namespace std; class A {
由于题目中给出的程序不完整,我在此做出一些假设来完善程序。假设程序如下:
```c++
#include <iostream>
using namespace std;
class A {
public:
A(int x) {
a = x;
}
virtual void f() {
cout << a << " ";
}
private:
int a;
};
class B : public A {
public:
B(int x, int y) : A(x) {
b = y;
}
void f() {
cout << b << " ";
}
private:
int b;
};
int main() {
A *p = new B(10, 20);
p->f();
delete p;
A a(30);
a.f();
return 0;
}
```
程序中定义了两个类 A 和 B,其中 B 继承自 A。类 A 中有一个 int 类型的成员变量 a 和一个虚函数 f(),该函数输出 a 的值。类 B 中有一个 int 类型的成员变量 b,并重写了类 A 中的虚函数 f(),该函数输出 b 的值。
在主函数中,首先定义了一个指向类 B 对象的指针 p,并调用 p 的 f() 函数输出 b 的值,然后使用 delete 操作符释放该对象的内存。接着定义了一个类 A 的对象 a,并调用 a 的 f() 函数输出 a 的值。最终程序输出了
```
20 30
```
的结果。
具体分析如下:
首先定义了指向类 B 对象的指针 p,该对象的成员变量 a 被初始化为 10,b 被初始化为 20。由于类 A 中的 f() 函数是虚函数,因此在调用 p 的 f() 函数时,会动态绑定到类 B 中的 f() 函数,输出 b 的值 20。
接着使用 delete 操作符释放指针 p 所指向的对象的内存。
然后定义了一个类 A 的对象 a,该对象的成员变量 a 被初始化为 30。调用 a 的 f() 函数时,由于 a 是类 A 的对象,因此会调用类 A 中的 f() 函数,输出 a 的值 30。
因此最终程序输出了 20 和 30 两个数,中间以空格隔开。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)