#include <iostream>
using namespace std;
class A
{
public:
A(int aa){a=aa;}
void show()const{cout<<"A:"<<a<<endl;} int a;
};
class B:
public A
{
public:
B(int aa,int bb):A(aa),b(bb){ }
B(A &ra); //写出该转换构造函数的类外定义
void show()const;
private:
int b;
};
void B::show()const
{
A::show();
cout<<"B:"<<b<<endl;
}
void f(const B &rb)
{
rb.show();
}
int main()
{
A obj(5);
f(obj);
return 0;
}
B::B(A &ra):A(ra),b(0){}
B::B(A &ra):A(ra.a),b(0) { ... }
形参为 A 的对象引用,将 ra的变量赋值给 父类的 a 即可