#include<iostream> using namespace std; class B { public: B(int i){b=i+50;show();} B(){} virtual void show() { cout<<"B::show()called."<<b<<endl; } protected: int b; }; class D:public B { public: D(int i):B(i){d=i+100;show();} D(){} void show() { cout<<"D::show()called."<<d<<endl; } protected: int d; }; int main() { D d1(108); }分析该程序原因及结果
时间: 2024-02-26 12:55:08 浏览: 17
该程序是一个带有继承的类的实现。类B是一个基类,类D是一个派生类。在main函数中,创建了一个D类的对象d1,传入参数108给构造函数D(int i),同时调用基类B的构造函数B(i),将i+50赋值给b。然后调用D类自己的show()函数,输出"D::show()called."和d的值(d=i+100),即"D::show()called.208"。
由于类B中的show()函数被声明为虚函数,因此在派生类D中也定义了一个同名的虚函数show()。当调用d1的show()函数时,会根据对象实际类型的不同,动态地选择调用哪个show()函数。在本程序中,d1的实际类型是D,因此调用的是D类中的show()函数,输出"D::show()called.208"。
相关问题
为什么以下程序会导致错误?#include <iostream> #include <string> using namespace std; class B { int b; public: B(int i){ b = i; } }; class C { B b; public: C(int i){ b = B(i); } friend void show(); }; void show() { C c(10); cout<<"value of b is: "<<c.b.b<<endl;} int main(){ show(); return 0; }
这个程序会导致错误,因为在C类的构造函数中,你试图将一个int类型的参数转换为B类的对象,然后将其赋值给C类的成员变量b。但是,由于B类没有默认构造函数,因此在C类的构造函数中,b成员变量在初始化时未被初始化。这导致在show函数中访问b.b时会导致未定义的行为。
要解决这个问题,你可以使用初始化列表来初始化C类的成员变量b。以下是修复后的示例代码:
```c++
#include <iostream>
#include <string>
using namespace std;
class B {
int b;
public:
B(int i) {
b = i;
}
};
class C {
B b;
public:
C(int i) : b(i) {} // 使用初始化列表来初始化b成员变量
friend void show();
};
void show() {
C c(10);
cout << "value of b is: " << c.b.b << endl;
}
int main() {
show();
return 0;
}
```
现在,你可以通过使用初始化列表来初始化C类的成员变量b,从而避免了未定义的行为。程序将输出"value of b is: 10"。
Why the following programs will cause error? #include <iostream> #include <string> using namespace std; class B { int b; public: B(){} B(int i){ b = i; } int show(){ return b; } }; class C { B b; public: C(int i){ b = B(i); } friend void show(){ C c(10); cout<<"value of b is: "<<c.b.show()<<endl; } }; int main(){ C c(1); c.show(); return 0; }
The program will cause an error because the `show()` function is defined as a friend function of the `C` class, but it tries to access the private member `b` of the `C` class through the `b.show()` method call.
To fix it, you can change the `show()` function to a member function of the `C` class, and then access the private member `b` directly using the `.` operator. Here's the corrected code:
```
#include <iostream>
#include <string>
using namespace std;
class B {
int b;
public:
B() {}
B(int i) { b = i; }
int show() { return b; }
};
class C {
B b;
public:
C(int i) { b = B(i); }
void show() {
cout<<"value of b is: "<<b.show()<<endl;
}
};
int main() {
C c(1);
c.show();
return 0;
}
```
Alternatively, you can keep the `show()` function as a friend function of the `C` class, but change the `b.show()` method call to `c.b.show()`, like this:
```
#include <iostream>
#include <string>
using namespace std;
class B {
int b;
public:
B() {}
B(int i) { b = i; }
int show() { return b; }
};
class C {
B b;
public:
C(int i) { b = B(i); }
friend void show() {
C c(10);
cout<<"value of b is: "<<c.b.show()<<endl;
}
};
int main() {
show();
return 0;
}
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)