//继承派生中对象相互赋值情况 //派生类的对象可以赋值给基类的对象,这时是把派生类对象中从对应基类中继承来的隐藏对象赋值给基类对象。 //反过来不行,因为派生类的新成员无值可赋。 #include <iostream> using namespace std; class B { public: B() { cout<<"B"<<endl; } void fun() { cout<<"B::fun()"<<endl; } private: int x; }; class D : public B { public: D() { cout<<"D"<<endl; } void fun() { cout<<"D::fun()"<<endl; } void show() { cout<<"D::show()"<<endl; } private: int y; }; void main() { B b; b.fun(); D d; d.fun(); b = d; //正确,把大的给小的,给局部赋值 //d = b; 错误,小的给大的,不能赋值 } <img src="http://img.blog.csdn.net/20150512213119072?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZG91ZG91d2ExMjM0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
时间: 2024-10-05 00:31:57