资源预览内容
第1页 / 共16页
第2页 / 共16页
第3页 / 共16页
第4页 / 共16页
第5页 / 共16页
第6页 / 共16页
第7页 / 共16页
第8页 / 共16页
第9页 / 共16页
第10页 / 共16页
亲,该文档总共16页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
南昌航空大学实验报告 年 月 日课程名称:面向对象程序设计 实验名称:类与结构 班 级: XXXXXXX 学生姓名: XXXXXX 学号: 指导教师评定: XX 签名: XXXXX 1、实验目的学习掌握声明和定义类及成员。 学习掌握访问类对象成员。学习掌握保护数据如何屏蔽外部访问的原理,更好的认识类的封装2、实验内容(1) 定义一个满足下列要求的Date类: 用下面的格式输出日期:日/月/年可运行在日期上加一天操作设置日期/Date.h#include class Datepublic: void Display(); void AddOneDay(); void SetDay(int y,int m,int d);protected: bool Legal(int y, int m, int d); bool IsLeapYear(int y); int year; int month; int day;void Date:Display() cout day / month / year 9999|y1|d1|m12) return false; int dayLimit=31; switch(m) case 4: case 6: case 9: case 11: dayLimit-; if(m=2) dayLimit = IsLeapYear(y) ? 29 : 28; return (ddayLimit)? false : true;bool Date:IsLeapYear(int y) return !(y%4)&(y%100)|!(y%400);(2) 定义一个时间类Time,能提供和设置由时、分、秒组成的时间,并编出应用程序,定义时间对象,设置时间,输出该对象提供的时间。并将类定义作为接口,用多文件结构实现之。/Time.hclass Timepublic:Time();void Print();void Set(int,int,int);protected:int min;int sec;int hour;/time.cpp#include #include Time.hTime:Time()min=23;sec=20;hour=18;void Time:Print()cout hour : min :sec endl;void Time:Set(int s,int m,int h)min=m;sec=s;hour=h;/test.cpp#include #include Time.hvoid main()int m,s,h;Time time;cout Now the time is: endl;time.Print();cout Please input the time:(sec/min/hour) s m h;while (s=60 | m=60 | h24)cout Please input again: s m h;time.Set(s,m,h);cout The time of being changed is: endl;time.Print(); 南昌航空大学实验报告年 月 日课程名称:面向对象程序设计 实验名称:静态成员与友元 班 级: XXXXXXX 学生姓名: XXXXXX 学号: 指导教师评定: XX 签名: XXXXX 1、实验目的学习友元函数的定义和原理。学习静态数据成员和静态成员函数的使用。学习静态成员代替全局变量实现对象间的共享。2、实验内容(1)有如下类的定义。类成员函数copy用于实现两个对象的相互拷贝,请完成该函数的实现。(有两种方法即不用this 指针和用this指针),并利用友员函数实现copy.include class Myclass public: Myclass (int a,int b) x=a;y=b; void copy(Myclass & my);void print( ) cout“x=”xendl; cout”y=”yendl; private:int x,y; void main() Myclass my(10,20),t(30,40); my.print( );my. Copy(t);my.print( );成员函数实现:#include class Myclass public: Myclass (int a,int b) x=a;y=b; void copy(Myclass & my); void print( ) coutx=xendl; couty=yx=my.x; y=my.y; /this-y=my.y; void main() Myclass my(10,20),t(30,40); my.print( );my. copy(t);my.print( );友员函数实现:#include class Myclass public: Myclass (int a,int b) x=a;y=b; friend void copy(Myclass &o,Myclass y); void print( ) coutx=xendl; couty=yendl; private:int x,y;void copy(Myclass &o,Myclass y) o.x=y.x; o.y=y.y; void main() Myclass my(10,20),t(30,40); my.print( ); copy(my,t); my.print( );(2)商店经销一种货物,货物成箱购进,成箱卖出,购进和卖出时以重量为单位,各箱的重量不一样,单价不一样,因此商店需要记录下目前库存的货物的总重量和总价值。编写一个程序,通过定义类Carlo来模拟商店货物购进和卖出的情况。(本题目主要练习静态数据成员的使用,定义私有变量存每件货物的价格和重量,用静态数据成员存货物的总重量和总价钱,定义构造函数和析构函数,当定义新的对象完成初始化的功能和删除对象时,从总重量和总价钱中减去对象的重量和价格)/Menu.h#ifndef MENU_H#define MENU_Hclass Menupublic:int show();#endif;/ Carlo.hclass Carlopublic: Carlo(double Weight = 0,double Price = 0); virtual Carlo(); void SetCarlo(double = 0,double = 0); const double &GetCurrentTotalWeight() const; const double &GetCurrentTotalPrice() const; void BuyBox(); void SellBox(); void ShowBoxInfor() const; protected: static double TotalWeight; static double TotalPrice; private: double BoxWeight; double BoxPrice;/Carlo.cpp#include #include Carlo.hdouble Carlo:TotalPrice=0.0;double Carlo:TotalWeight=0.0;Carlo:Carlo(double Weight, double Price) BoxWeight = Weight; BoxPrice = Price; TotalPrice += Weight * Price; TotalWeight += Weight;void Carlo:BuyBox() system(cls); Carlo carlo; double Weight , Price; coutWeightPrice;
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号