利用多继承,编写电话手表应用,继承电话,手表两个父类,要求熟悉掌握多继承的语法格式和使用。测试并运行查看结果。
#include <iostream>
using namespace std;
// 手表类
class Watch {
public:
void showTime() {
cout << "现在时间:2022年7月10日 10:00" << endl;
}
};
// 电话类
class Phone {
public:
void makeCall(string number) {
cout << "正在拨打电话:" << number << endl;
}
};
// 电话手表类,继承于手表类和电话类
class PhoneWatch : public Watch, public Phone {
public:
void sendMessage(string number, string message) {
cout << "正在发送短信:" << message << ",给 " << number << endl;
}
};
int main() {
PhoneWatch myPhoneWatch;
myPhoneWatch.showTime();
myPhoneWatch.makeCall("123456789");
myPhoneWatch.sendMessage("123456789", "你好,我是一名AI助手!");
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话: