prog5.cpp

来自「C++语言程序设计题典」· C++ 代码 · 共 40 行

CPP
40
字号
#include <iostream.h>
class base
{
	int x,y;
public:
	base(int i,int j) { x=i;y=j; }
	virtual int add() { return x+y; }
};
class two:public base
{
public:
	two(int i,int j):base(i,j) {}
	int add() { return base::add(); }
};
class three:public base
{
	int z;
public:
	three(int i,int j,int k):base(i,j)
	{
		z=k;
	}
	int add()
	{
		return (base::add()+z);
	}
};
void disp(base *obj)    //动态联编
{
	cout << "  参数相加:" << obj->add() << endl;
}
void main()
{
	two *p=new two(10,20);
	three *q=new three(10,20,30);
	cout << "输出结果:" << endl;
	disp(p);
	disp(q);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?