13_2.cpp

来自「为初学者提供的最佳的C++程序设计源程序库」· C++ 代码 · 共 39 行

CPP
39
字号
#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 + -
显示快捷键?