#include <iostream>using namespacestd;classdemoa{ Public: intM_a; voidShow (); Demoa (intval);;D Emoa::D Emoa (intval) {M_a=Val;}voiddemoa::show () {cout<<"ashow:demoa.m_a="<< This->m_a<<Endl;}//-------------------classDemob: Publicdemoa{ Public: intM_b; voidShow (); Demob (intAval,intbval);};D Emob::D emob (intAval,intbval):D Emoa (aval) {M_b=Bval;}voiddemob::show () {cout<<"bshow:demob.m_a="<< This->m_a<<Endl; cout<<"bshow:demob.m_b="<< This->m_b<<Endl;}//---------------intMain () {Demoa CA ( One); Demob CB (111,222); Ca.show (); Cb.show (); CA=cb;//derived class objects can assign a value ca.show () to a base class;//Cb=ca;the//base class cannot be assigned to a derived classerror C2679: Binary "=": operator not found to accept right operand of "Demoa" type (or no acceptable conversion)//cb.show ();// while(1); return 0 ;}
C + +, derived class objects can assign values to base classes, and base classes cannot be assigned to derived classes