2.改成单继承 class A { public: A(int num):data(num) {} void AFuncs() {cout<<"This is A \'s public function!"<<endl;} protected: int data; }; class B { public: B(int num):value(num) {} void BFuncs() {cout<<"This is B \'s public function!"<<endl;} protected: int value; }; class C:public A,private B { public: C(int num1,int num2,int y); void MyFuncs() { BFuncs(); cout<<"This function call B::BFuncs() !"<<endl; } private: int yyy; }; 1)完成C类的构造函数。 2)在main 函数中可以向C类对象发送哪些消息? 3)在不改变C类的功能的条件下,利用类的组合重新定义并实现C类,使其变成单继承。 4)实现新的C类的构造,析构,拷贝、赋值函数。
时间: 2024-01-22 15:04:17 浏览: 38
1) 完成C类的构造函数。
```c++
C::C(int num1, int num2, int y): A(num1), B(num2), yyy(y) {}
```
2) 在main函数中可以向C类对象发送以下消息:
- AFuncs():调用A类的公有函数。
- MyFuncs():调用C类的公有函数。
3) 在不改变C类的功能的条件下,利用类的组合重新定义并实现C类,使其变成单继承。
```c++
class C {
public:
C(int num1, int num2, int y);
void AFuncs() { a.AFuncs(); }
void MyFuncs() { b.BFuncs(); cout << "This function call B::BFuncs() !" << endl; }
private:
A a;
B b;
int yyy;
};
```
4) 实现新的C类的构造、析构、拷贝、赋值函数。
```c++
C::C(int num1, int num2, int y): a(num1), b(num2), yyy(y) {}
C::~C() {}
C::C(const C& other): a(other.a), b(other.b), yyy(other.yyy) {}
C& C::operator=(const C& other) {
if (this != &other) {
a = other.a;
b = other.b;
yyy = other.yyy;
}
return *this;
}
```