|
发表于 2020-1-20 15:27:01
|
显示全部楼层
一
class Base {
protected:
int iBody;
public:
virtual void printOn() = 0;
Base(int i = 0) : iBody(i) {}
};
class Sub1 : public Base {
// …
protected:
char* pChar;
public:
void printOn();
Sub1(int i, char* s);
};
class Sub2 : public Base {
// …
protected:
short ss;
public:
void printOn();
Sub2(int i, short s);
};
Sub1::Sub1(int i, char* s)
{
iBody = i;
pChar = s;
}
void Sub1::printOn()
{
printf("<%d: %s>\n", iBody, pChar);
}
Sub2::Sub2(int i, short s)
{
iBody = i;
ss = s;
}
void Sub2::printOn()
{
printf("<%d and %d>\n", iBody, ss);
}
//试完成类Sub1和Sub2的定义和操作的实现代码,使之能符合
//下面程序及在注释中描述的运行结果的要求:
main()
{
Sub1 s1(1000, "This is an object of Sub1");
Sub2 s2(2000, 10);
s1.printOn();
// 此时显示出: <1000: This is an object of Sub1>
s2.printOn(); // 此时显示出: <10 and 2000>
}
|
|