#include <iostream>
using namespace std;
class A
{
protected:
int a,b;
public:
A(int aa, int bb):a(aa), b(bb) {}
void printA(){
cout<<"a: "<<a<<"\tb: "<<b<<endl;
}
};
class B: public A
{
int c;
public:
B(int aa, int bb, int cc):A(aa,bb),c(cc) {}
void printB()
{
cout<<"a: "<<a<<"\tb: "<<b<<"\tc: "<<c<<endl;
}
};
int main()
{
A a(1,1);
B b(2,3,4);
b=a;
a.printA();
b.printA();
b.printB();
return 0;
}
运行结果:
过程分析:
error 运算符两边不匹配
原因:
基类对象不能给派生类的对象赋值
相反,派生类对象可以给基类的对象赋值