/*
* @Issue: 生成一个Object抽象类
* @Author: 一届书生
* @LastEditTime: 2020-02-24 10:34:13
*/
#include<iostream>
using namespace std;
// Point类
class Point{
int x,y;
public:
// 带参构造函数
Point(int x1,int y1){
this->x=x1;
this->y=y1;
}
// 默认构造函数
Point(){
this->x=0;
this->y=0;
}
// 析构函数
~Point(){}
int GetX(){
return x;
}
int GetY(){
return y;
}
void setX(int x1){
x=x1;
}
void setY(int y1){
y=y1;
}
void MoveTo(int x1,int y1){
setX(x1);
setY(y1);
}
void Display(){
cout<<"x:"<<x<<endl;
cout<<"y:"<<y<<endl;
}
};
// Object抽象类
class Object{
public:
virtual double CalArea()=0;
virtual bool IsIn(Point p)=0;
};
// 矩形Rect类
class Rect:public Object{
Point lt; //左上角
Point rb; //右下角
public:
// 初始化列表构造函数
Rect(Point p1,Point p2):lt(p1),rb(p2){}
// 求面积函数
double CalArea(){
return (rb.GetX()-lt.GetX())*(rb.GetY()-lt.GetY());
}
// 判断点是否在矩阵内(边缘也算)
bool IsIn(Point p){
return (p.GetX()>=lt.GetX()&&p.GetX()<=rb.GetX()
&&p.GetY()>=lt.GetY()&&p.GetY()<=rb.GetY());
}
};
int main(){
Object *obj[2];
obj[0]=new Rect(Point(0,0),Point(3,3));
cout<<obj[0]->CalArea()<<endl;
return 0;
}
原文:https://www.cnblogs.com/52dxer/p/12355889.html