实验三++类与对象

上传人:ji****n 文档编号:46056144 上传时间:2018-06-21 格式:DOC 页数:5 大小:26.50KB
返回 下载 相关 举报
实验三++类与对象_第1页
第1页 / 共5页
实验三++类与对象_第2页
第2页 / 共5页
实验三++类与对象_第3页
第3页 / 共5页
实验三++类与对象_第4页
第4页 / 共5页
实验三++类与对象_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

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

1、实验三 类与对象实验目的掌握类和对象的概念、定义和访问方法 掌握不同类型成员的访问限制 掌握构造函数和析构函数的使用方法实验示例例 3-1 运用面向对象程序设计方法编写一个程序,计算两个给定长方形的面积,其 类中设计一个用于计算两个长方形的总面积成员函数。解题思路: 设计一个类 Rect,包含数据成员 len(表示长度) 、wid(表示宽度) ,以及求面积的公 有成员函数 area()、求两个长方形的总面积成员函数 addarea(),并设计两个重载的构造函 数。 实现步骤: 1新建 Win32 Console Application 程序项目 2在文件编辑区窗口编写如下程序代码: #incl

2、ude #include class Rect float len,wid;public:Rect() ; / 默认的构造函数(内联的)Rect(float,float); / 构造函数float area() return len*wid; ; / 求面积(内联的)void addarea(Rect,Rect); / 求两个长方形的面积 ; Rect:Rect(float a, float b) len=a; wid=b; void Rect:addarea(Rect r1, Rect r2) cout class score int fs;static int sum;static int

3、 num; public:score(int s)fs=s;sum+=fs;num+; static void average()if (num=0) couts;if (s class Date public: void getdate() / 输出日期,格式:“2000 年 1 月 1 日“ couty“年“m“月“d“日“; int getyear() / 返回年份 return y; int getmonth() / 返回月份 return m; int getday() / 返回日期 return d; void setdate(int y1, int m1, int d1) / 设

4、置日期 y=y1;m=m1; d=d1; private: int y,m,d; ;class employee char name10; int num; Date birth; public: void set(char a10,int b) name10=a10; num=b; void Birth(int y2,int m2,int d2) birth.setdate(y2,m2,d2); void show() cout“编号为“num“的雇员生日为“; birth.getdate(); coutendl; int isBirth(Date a,employee b) / 判断今天是

5、否为某雇员的生日 int mn,my,dn,dy; mn=a.getmonth(); my=b.birth.getmonth(); dn=a.getday(); dy=b.birth.getday(); if(mn=my else return 0; ;int main() Date today; today.setdate(2014,3,27);employee f; f.set(“Anna“,1); f.Birth(1992,8,19); f.show(); cout“今天是“; today.getdate();coutendl; if(f.isBirth(today,f) cout“今天是该雇员的生日。“endl; else cout“今天不是该雇员的生日。“endl;return 0;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 医学/心理学 > 基础医学

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