动态绑定(点类和矩形类)2.txt

来自「学C++的同学是不是遇到麻烦了」· 文本 代码 · 共 43 行

TXT
43
字号
#include <iostream.h>
class Point
{
public:
	Point(double  i,double j) {x=i;y=j;}
	virtual double Area() const {return 0.0;}
private:
	double x,y;
};
class Rectangle:public Point
{
public:
	Rectangle(double i,double j,double k,double l);
	virtual double Area() const {return w*h;}
private:
	double w,h;
};
Rectangle::Rectangle(double i,double j,double k,double l):Point(i,j)
{
	w=k;h=l;
}
void fun(Point  s)
{
    Point *p=&s;
	cout<<p->Area()<<endl;
}
void main()
{
	Rectangle rec(3.0,5.2,15.0,25.0);
	fun(rec); 
               Point s(3.0,5.0);
	fun(s);
}








0
0

⌨️ 快捷键说明

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