类与对象.doc

上传人:hs****ma 文档编号:563299794 上传时间:2024-01-26 格式:DOC 页数:41 大小:37.19KB
返回 下载 相关 举报
类与对象.doc_第1页
第1页 / 共41页
类与对象.doc_第2页
第2页 / 共41页
类与对象.doc_第3页
第3页 / 共41页
类与对象.doc_第4页
第4页 / 共41页
类与对象.doc_第5页
第5页 / 共41页
点击查看更多>>
资源描述

《类与对象.doc》由会员分享,可在线阅读,更多相关《类与对象.doc(41页珍藏版)》请在金锄头文库上搜索。

1、类与对象1:Point类1Description定义一个Point类,数据成员包括私有数据成员为double类型的点坐标x,y;成员函数包括构造函数Point(用于实现对数据成员x,y的初始化),成员函数Display(用于输出点坐标x、y,输出格式为点坐标用逗号分隔并半角圆括号括起来)。main函数如下(不得修改main函数):int main() double x,y; cinxy; Point p1(x,y); p1.Display(); return 0;InputOutputSample Input12.5 22.7 Sample Output(12.5,22.7)*#include

2、using namespace std;class Pointpublic: Point(double xx,double yy) x=xx;y=yy; void Display() cout(x,y)xy; Point p1(x,y); p1.Display(); return 0;类与对象2:Point类2Description定义一个Point类,数据成员包括私有数据成员为double类型的点坐标x,y;成员函数包括构造函数Point(用于实现对数据成员x,y的初始化)、成员函数Set(用于改变数据成员x、y的值)、成员函数Display(用于输出点坐标x、y,输出格式为点坐标用逗号分隔

3、并半角圆括号括起来)。main函数如下(不得修改main函数):int main() double x1,y1,x2,y2; cinx1y1; cinx2y2; Point p1(x1,y1); p1.Display(); p1.Set(x2,y2); p1.Display(); return 0;InputOutputSample Input10 25.5 5.5 20Sample Output(10,25.5)(5.5,20)*#includeusing namespace std;class Pointpublic: Point(double xx,double yy) x=xx;y=y

4、y; void Display() cout(x,y)x1y1; cinx2y2; Point p1(x1,y1); p1.Display(); p1.Set(x2,y2); p1.Display(); return 0;类与对象3:Point类3Description定义一个Point类,数据成员包括私有数据成员为double类型的点坐标x,y;成员函数包括构造函数Point(用于实现对数据成员x,y的初始化)、成员函数Set(用于改变数据成员x、y的值)、成员函数LeftMove(点坐标向左移动detax)、成员函数上移UpMove(点坐标上移detay)、成员函数Display(用于输出

5、点坐标x、y,输出格式为点坐标用逗号分隔并半角圆括号括起来)。main函数如下(不得修改main函数):int main() double x1,y1,x2,y2,dx,dy; cinx1y1; cindx; cindy; cinx2y2; Point p1; p1.Display(); p1.Set(x1,y1); p1.Display(); p1.LeftMove(dx); p1.Display(); p1.UpMove(dy); p1.Display(); Point p2(x2,y2); p2.Display(); return 0;InputOutputSample Input10

6、25.5 5 10 5.5 20Sample Output(0,0)(10,25.5)(5,25.5)(5,35.5)(5.5,20)*#includeusing namespace std;class Pointpublic: Point(double xx=0,double yy=0) x=xx;y=yy; void Display() cout(x,y)x1y1; cindx; cindy; cinx2y2; Point p1; p1.Display(); p1.Set(x1,y1); p1.Display(); p1.LeftMove(dx); p1.Display(); p1.UpM

7、ove(dy); p1.Display(); Point p2(x2,y2); p2.Display(); return 0;类与对象4:Rectangle类1Description定义一个长方形类Rectangle,私有数据成员为double型width、height(表示长方形的宽和高),成员函数包括构造函数Rectangle(用于实现对数据成员width、height的初始化)、成员函数GetArea(计算并返回长方形的面积)。main函数如下(不得修改main函数):int main() double width,height; cinwidthheight; Rectangle r1

8、; coutr1.GetArea()endl; Rectangle r2(width,height); coutr2.GetArea()endl; return 0;InputOutputSample Input10.2 25.5Sample Output0260.1*#includeusing namespace std;class Rectanglepublic: Rectangle(double w=0,double h=0); double GetArea();private: double width,height;Rectangle:Rectangle(double w,doubl

9、e h) width=w;height=h;double Rectangle:GetArea() return width*height;int main() double width,height; cinwidthheight; Rectangle r1; coutr1.GetArea()endl; Rectangle r2(width,height); coutr2.GetArea()widthheight; cinn; Rectangle r1; coutr1.GetArea()endl; Rectangle r2(width,height); coutr2.GetArea()endl; r2.Expand(n); coutr2.GetArea()endl;

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号