#include <iostream> using namespace std; class A { private: int a; public: void seta(int i) { a = i; } int geta() { return a; } }; class B:public A { private: int b; public: void setb(int i) { b = i; } int getb() { return b; } void show() { cout<<"A::a="<<geta()<<endl; } }; int main() { B bb; bb.seta(6); bb.setb(3); bb.show(); cout<<"A::a="<<bb.geta()<<endl; cout<<"B::b="<<bb.getb()<<endl; cout<<"A::a="<<bb.geta()<<endl; cout<<"B::b="<<bb.getb()<<endl; return 0; }将派生类B的继承方式改为private时,会出现哪些错误和不正常现象?为什么?
时间: 2024-03-29 21:40:23 浏览: 229
将派生类 `B` 的继承方式改为 `private`,会出现以下错误和不正常现象:
1. 在 `class B` 内部,无法访问基类 `A` 的成员变量和成员函数,因为继承方式为 `private`,只能在 `B` 内部访问,而不能在外部访问。
2. 在 `main()` 函数中,无法访问 `B` 类中继承自 `A` 类的成员变量和成员函数,因为继承方式为 `private`,只能在 `B` 类内部和 `B` 类的友元函数中访问,而不能在 `main()` 函数中访问。
3. 在使用 `B` 类对象时,只能使用 `B` 类自己的成员函数和成员变量,不能使用 `A` 类的成员函数和成员变量。
综上所述,将派生类 `B` 的继承方式改为 `private`,会导致访问权限受限,使得代码的灵活性和可读性下降,因此不建议使用这种方式。
相关问题
#include<iostream> using namespace std; class A { protected: int a,b; private: int c,d; public: int getc(); }; int A::getc() { return c; } int main() { A onea; cout<<onea.a<","<<onea.getc()<<endl; return 0; }完善这段代码
这段代码的问题已经被解决了,你可以在main函数中使用onea来打印a的值和getc的返回值。同时,你可以在类A中添加一个公共成员函数来设置c的值,以便从外部代码中修改它。下面是完整的代码示例:
```c++
#include<iostream>
using namespace std;
class A {
protected:
int a, b;
private:
int c, d;
public:
int getc();
void setc(int value);
};
int A::getc() {
return c;
}
void A::setc(int value) {
c = value;
}
int main() {
A onea;
onea.a = 10;
onea.setc(20);
cout << onea.a << "," << onea.getc() << endl;
return 0;
}
```
在这个示例中,我们在main函数中创建了一个A类的实例onea,并分别使用onea.a和onea.setc来设置a和c的值。然后,我们使用onea.a和onea.getc来打印a和c的值。输出将是“10,20”。
#include <iostream> #include <string> #include <iomanip> using namespace std; class house{ private: string location; int num_of_rooms; int year_built; public: house(); house(string,int,int); void set_location(string); string get_location(); void set_num_of_rooms(int); int get_num_of_rooms(); void set_year_built(int); int get_year_built(); void print_house_details(); };
This is a class declaration for a `house` class with some private member variables and public member functions. Here is an example implementation for the `house_impl.cpp` file:
```
#include "house.h"
// Default constructor
house::house() {
location = "";
num_of_rooms = 0;
year_built = 0;
}
// Parameterized constructor
house::house(string loc, int num, int year) {
location = loc;
num_of_rooms = num;
year_built = year;
}
// Setter and getter functions for location
void house::set_location(string loc) {
location = loc;
}
string house::get_location() {
return location;
}
// Setter and getter functions for number of rooms
void house::set_num_of_rooms(int num) {
num_of_rooms = num;
}
int house::get_num_of_rooms() {
return num_of_rooms;
}
// Setter and getter functions for year built
void house::set_year_built(int year) {
year_built = year;
}
int house::get_year_built() {
return year_built;
}
// Function to print house details
void house::print_house_details() {
cout << "Location: " << location << endl;
cout << "Number of rooms: " << num_of_rooms << endl;
cout << "Year built: " << year_built << endl;
}
```
Note that the implementation file should include the header file `house.h` which contains the declaration of the `house` class.
阅读全文