源程序:
#include<iostream>
#include<string>
using namespace std;
class A
{
int an;
public:
A() {}
A(int n)
{
an = n;
}
void print()
{
cout << "A的对象:";
cout << "an:" << an << endl;
}
void print(int k) //不同的输出格式
{
cout << "an:" << an << endl;
}
};
class B :public A //公有派生
{
int bn;
public:
B(int n) :A(2 * n)
{
bn = n;
}
void print()
{
cout << "B的对象:";
A::print(1);
cout << ",bn:" << bn << endl;
}
};
int main()
{
A a(10);
B b(20);
a.print();
b.print();
a = b; //派生类对象赋值给基类对象
a.print();
b.print();
system("pause");
return 0;
}
运行结果: