有如下类定义: class A { public: virtual void func(int x){ cout<<"func() in class A with input "<<x<<endl; } }; class B : public A { public: void func(int x) final{ cout<<"func() in class B with input "<<x<<endl; } }; class C : public B { public: void func(int x){ cout<<"func() in class C with input "<<x<<endl; } }; 则以下语句的输出为: A a; B b; C c; A* arr[3]={&a, &b, &c}; for (int i = 0; i < 3; i++) arr[i]->func(i); A. func() in class A with input 0 func() in class A with input 1 func() in class A with input 2 B. func() in class A with input 0 func() in class B with input 1 func() in class C with input 2 C. func() in class C with input 0 func() in class C with input 1 func() in class C with input 2 D. 编译时出错
时间: 2024-04-21 08:25:05 浏览: 15
答案是B。
在这个类继承关系中,类B重写了类A的虚函数func,并将其标记为final,这意味着类C无法再重写该函数。然而,类C仍然尝试重写func函数,这是不合法的。
在循环中,arr数组中的第一个元素指向类A的对象,因此调用的是类A中的func函数。第二个元素指向类B的对象,因为类B重写了func函数,所以调用的是类B中的func函数。第三个元素指向类C的对象,由于类C试图重写func函数,但由于该函数已被标记为final,所以调用的仍然是类B中的func函数。因此,输出应为“func() in class A with input 0 func() in class B with input 1 func() in class C with input 2”。
相关问题
class A {public:A(){ } virtual void func(){ cout <<"A::func" <<endl; }~A() { } virtual void fund(){ cout <<"A:fund" << endl;}}; class B:public A {public: B() { func(); } void fun() { func(); }~B() { fund(); }};class C : public B {public : C() {} void func(){ cout <<"C:.func" <<endl;}~c() { fund();} void fund(){ cout <<"Cifund" <<endl;}}; int main(){ C c;return O;}
这段代码是一个继承与多态的示例,定义了三个类 A、B、C。其中,A 是基类,B 继承自 A,C 继承自 B。
A 类有两个虚函数 func 和 fund,B 类的构造函数中调用了 func 函数,B 类也有一个自己的函数 fun,其中也调用了 func 函数。B 类还重写了基类 A 中的虚函数 fund,并在自己的析构函数中调用了 fund 函数。
C 类重写了 A 中的虚函数 func,并在自己的析构函数中调用了基类 B 中的虚函数 fund,并输出了一条信息。
在主函数中,我们创建了一个 C 类型的对象 c,并没有进行其他操作。由于 C 类并没有自己的构造函数,所以会调用基类 B 的构造函数,从而打印出 "A::func" 这条信息。
最终,这个程序的输出应该是:
A::func
C:fund
C:fund
class A { public: int a = 100; void func() {cout << "A 成员函数"} }; class B { public: int b = 200; void func() { cout << "B 成员函数" } }; A *a1; B b1; a1 = &b1; a1->func(); b1.func();
这段代码中存在一些语法错误,应该修改为以下代码:
```
#include <iostream>
using namespace std;
class A {
public:
int a = 100;
void func() { cout << "A 成员函数" << endl; }
};
class B {
public:
int b = 200;
void func() { cout << "B 成员函数" << endl; }
};
int main() {
A* a1;
B b1;
a1 = &b1;
a1->func();
b1.func();
return 0;
}
```
此时的输出结果应该为:
```
A 成员函数
B 成员函数
```
因为 `a1` 是一个指向 `A` 类型的指针,但是却指向了 `B` 类型的对象 `b1`。当调用 `a1->func()` 时,由于 `a1` 的静态类型是 `A*`,因此编译器会将 `a1->func()` 解释为调用 `A` 类型的 `func()` 函数,但是实际上 `a1` 指向的是 `B` 类型的对象 `b1`,因此会调用 `B` 类型的 `func()` 函数,输出字符串 `"B 成员函数"`。而当调用 `b1.func()` 时,直接调用 `B` 类型的 `func()` 函数,输出字符串 `"B 成员函数"`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)