017--C++养成之路(多继承)

    科技2022-07-11  102

    笔记:多继承,子类隐式转换为基类(皆然性)

    #include<iostream> using namespace std; class A{ public: A(){ cout << "A构造" << endl; } void call(){ cout << "打电话··" << endl; } }; class B{ public: B(){ cout << "B构造" << endl; } void take(){ cout << "照相···" << endl; } }; class C{ public: C(){ cout << "C构造" << endl; } void play(){ cout << "播放··" << endl; } }; class iphoneX:public A,public B,public C{ public: iphoneX(){ cout << "iphoneX构造" << endl; } }; int main(){ iphoneX ip; ip.call(); ip.play(); ip.take(); cout << "---------------------" << endl; A* p1 = &ip;//子类对象都看作基类对象,隐式转换 B* p2 = &ip; C* p3 = &ip;//皆然性,将子类对象隐式转换为基类对象 cout <<&ip<<"\t"<< p1 << "\t" << p2 << "\t" << p3 << endl; getchar(); return 0; }

     

    Processed: 0.045, SQL: 8