定义一个CMP3类,其有一个构造函数和公有函数Play;定义一个Phone类,其有一个构造函数和一个公有函数Call;再定义一个CMp3Phone类,其是由CMP3类和Phone类派生而来。用main函数测试基类构造函数的执行顺序以及访问基类的成员函数。
#include<iostream>
using namespace std;
class CMP3{
public:
CMP3(){
std::cout << "this is CMP3" << std::endl;
}
void Play(){
std::cout << "CMP3::Play()" << std::endl;
}
};
class Phone{
public:
Phone(){
std::cout << "this is Phone" << std::endl;
}
void Call(){
std::cout << "Phone::Call()" << std::endl;
}
};
class CMp3Phone:public CMP3,public Phone{
public:
CMp3Phone() {
std::cout << "this is CMp3Phone" << std::endl;
}
};
int main()
{
CMp3Phone *mp3Phone = new CMp3Phone();
mp3Phone->Play();
mp3Phone->Call();
return 0;
}