c++组合类rectangle

c++组合类rectangle

ID:38978222

大小:32.50 KB

页数:3页

时间:2019-06-22

c++组合类rectangle_第1页
c++组合类rectangle_第2页
c++组合类rectangle_第3页
资源描述:

《c++组合类rectangle》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、用组合技巧写一个矩形类 尽量用多文件结构去写,多文件去写的时候注意一般要用到条件编译,防止一个类多次编译,造成类重定义错误//point.hclassPoint{public:Point(){x=0;y=0;}Point(intinitX,intinitY);intGetX()const;intGetY()const;private:intx,y;};//point.cpp#include"point.h"Point::Point(intinitX,intinitY){x=initX;y=initY;

2、}intPoint::GetX()const{returnx;}intPoint::GetY()const{returny;}//rect.h#include"point.h"classRect{public:Rect();Rect(intl,intt,intb,intr);Rect(Pointtopleft,Pointbottomright);Rect(Rect&r);~Rect();intGetWidth()const;intGetLength()const;intGetArea();PointG

3、etTopLeft()const;PointGetBottomRight();private:PointtopLeft,bottomRight;//左上角,左下角};//rect.cpp#include"rect.h"//#include"point.h"//'class'typeredefinitionRect::Rect(){}3Rect::Rect(intl,intt,intr,intb):topLeft(l,t),bottomRight(r,b){}Rect::Rect(Pointtoplef

4、t,Pointbottomright):topLeft(topleft),bottomRight(bottomright){}Rect::Rect(Rect&r):topLeft(r.topLeft),bottomRight(r.bottomRight){}Rect::~Rect(){}intRect::GetWidth()const{returntopLeft.GetX()-bottomRight.GetX();}intRect::GetLength()const{returntopLeft.Get

5、Y()-bottomRight.GetY();}intRect::GetArea(){return(topLeft.GetX()-bottomRight.GetX())*(topLeft.GetY()-bottomRight.GetY());}PointRect::GetTopLeft()const{returntopLeft;}PointRect::GetBottomRight(){returnbottomRight;}//main.cpp#include"rect.h"//#include"poi

6、nt.h"#includeusingnamespacestd;voidmain(){intx1,x2,y1,y2;cout<<"请输入矩形的左上角点坐标"<>x1>>y1;Pointp1(x1,y1);cout<<"请输入矩形的右下角点坐标"<>x2>>y2;Pointp2(x2,y2);Rectr1(p1,p2);cout<<"矩形r1的长"<

7、r2;cout<<"矩形r2的长"<

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。