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

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

TXT
52
字号
#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)
{
   	cout<<"fun:  "<<s.Area()<<endl;
}
void fun1(Point  s)
{
	cout<<"fun1:  "<<s.Area()<<endl;
}
void fun2(Point  *p)
{
	cout<<"fun2:  "<<p->Area()<<endl;
}
void main()
{
	Rectangle rec(3.0,5.2,15.0,25.0);
	fun(rec); 
	fun1(rec);
	fun2(&rec);
}









fun:  375
fun1:  0
fun2:  375

⌨️ 快捷键说明

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