版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、南昌航空大学实验报告 年 月 日课程名称:面向对象程序设计 实验名称:类与结构 班 级: XXXXXXX 学生姓名: XXXXXX 学号: 指导教师评定: XX 签名: XXXXX 1、实验目的·学习掌握声明和定义类及成员。 ·学习掌握访问类对象成员。·学习掌握保护数据如何屏蔽外部访问的原理,更好的认识类的封装2、实验内容(1) 定义一个满足下列要求的Date类: 用下面的格式输出日期:日/月/年可运行在日期上加一天操作设置日期/Date.h#include <iostream.h>class Datepublic: void Display(); v
2、oid 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 <<endl;void Date:AddOneDay() if(Legal(yea
3、r,month,day+1) day+; else if(Legal(year,month+1,1) month+,day=1; else if(Legal(year+1,1,1) day=1,month=1,year+;void Date:SetDay(int y, int m, int d) if(Legal(y,m,d) day=d, month=m, year=y;bool Date:Legal(int y, int m, int d) if(y>9999|y<1|d<1|m<1|m>12) return false; int dayLimit=31; s
4、witch(m) case 4: case 6: case 9: case 11: dayLimit-; if(m=2) dayLimit = IsLeapYear(y) ? 29 : 28; return (d>dayLimit)? false : true;bool Date:IsLeapYear(int y) return !(y%4)&&(y%100)|!(y%400);(2) 定义一个时间类Time,能提供和设置由时、分、秒组成的时间,并编出应用程序,定义时间对象,设置时间,输出该对象提供的时间。并将类定义作为接口,用多文件结构实现之。/Time.hclass
5、Timepublic:Time();void Print();void Set(int,int,int);protected:int min;int sec;int hour;/time.cpp#include <iostream.h>#include "Time.h"Time:Time()min=23;sec=20;hour=18;void Time:Print()cout <<hour <<":" <<min <<":"<<sec <<endl;v
6、oid Time:Set(int s,int m,int h)min=m;sec=s;hour=h;/test.cpp#include <iostream.h>#include "Time.h"void main()int m,s,h;Time time;cout <<"Now the time is:" <<endl;time.Print();cout <<"Please input the time:(sec/min/hour)" <<endl;cin >>s
7、 >>m >>h;while (s<0 | s>=60 | m<0 | m>=60 | h<0 | h>24)cout <<"Please input again:" <<endl;cin >>s >>m >>h;time.Set(s,m,h);cout <<"The time of being changed is:" <<endl;time.Print(); 南昌航空大学实验报告年 月 日课程名称:面向对象程
8、序设计 实验名称:静态成员与友元 班 级: XXXXXXX 学生姓名: XXXXXX 学号: 指导教师评定: XX 签名: XXXXX 1、实验目的·学习友元函数的定义和原理。·学习静态数据成员和静态成员函数的使用。·学习静态成员代替全局变量实现对象间的共享。2、实验内容(1)有如下类的定义。类成员函数copy用于实现两个对象的相互拷贝,请完成该函数的实现。(有两种方法即不用this 指针和用this指针),并利用友员函数实现copy.include <iostream.h> class Myclass public: Myclass (int a,i
9、nt b) x=a;y=b; void copy(Myclass & my);void print( ) cout<<“x=”<<x<<endl; cout<<”y=”<<y<<endl; private:int x,y; void main() Myclass my(10,20),t(30,40); my.print( );my. Copy(t);my.print( );成员函数实现:#include <iostream.h> class Myclass public: Myclass (int a,
10、int b) x=a;y=b; void copy(Myclass & my); void print( ) cout<<"x="<<x<<endl; cout<<"y="<<y<<endl; private:int x,y;void Myclass:copy(Myclass & my)x=my.x; /this->x=my.x; y=my.y; /this->y=my.y; void main() Myclass my(10,20),t(30,40);
11、my.print( );my. copy(t);my.print( );友员函数实现:#include <iostream.h> class Myclass public: Myclass (int a,int b) x=a;y=b; friend void copy(Myclass &o,Myclass y); void print( ) cout<<"x="<<x<<endl; cout<<"y="<<y<<endl; private:int x,y;void
12、 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来模拟商店货物购进和卖出的情况。(本题目主要练习静态数据成员的使用,定义私有变量存每件货物的价格和重量,用静态数据成员存货物的总重量和总价钱,定义构造函数和析
13、构函数,当定义新的对象完成初始化的功能和删除对象时,从总重量和总价钱中减去对象的重量和价格)/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 dou
14、ble &GetCurrentTotalPrice() const; void BuyBox(); void SellBox(); void ShowBoxInfor() const; protected: static double TotalWeight; static double TotalPrice; private: double BoxWeight; double BoxPrice;/Carlo.cpp#include <iostream.h>#include "Carlo.h"double Carlo:TotalPrice=0.0;dou
15、ble 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; cout<<"请输入进货的重量和价格:" cin>>Weight>>
16、Price; carlo.SetCarlo(Weight,Price); const double &Carlo:GetCurrentTotalPrice() const return TotalPrice; const double &Carlo:GetCurrentTotalWeight() const return TotalWeight;void Carlo:SetCarlo(double Weight, double Price) BoxWeight = Weight; BoxPrice = Price; TotalPrice += Weight * Price; T
17、otalWeight += Weight;void Carlo:SellBox() system("cls"); double Weight,Price; cout<<"输入出货的重量和单价:" cin>>Weight>>Price; while (Weight < 0 | Price < 0) cout<<"参数不对,请重新输入:" cin>>Weight>>Price; if (Weight * Price > TotalPrice) | W
18、eight > TotalWeight | TotalPrice < 0) cout<<"存货不够!" cin.get(); cin.get(); else TotalPrice -= Weight * Price; TotalWeight -= Weight; cout<<"出货完成!" cin.get(); cin.get(); void Carlo:ShowBoxInfor() constsystem("cls"); cout<<"库中的存货总重量为:" cou
19、t.precision(6); cout.setf(ios_base:showpoint); cout<<GetCurrentTotalWeight()<<endl; cout<<"库中的存货总价钱为:" cout.precision(6); cout<<GetCurrentTotalPrice(); cin.get(); cin.get();Carlo:Carlo()/Menu.cpp#include <iostream.h>#include <windows.h>#include "Men
20、u.h"int Menu:show() system("cls"); int chioce; cout<<"nnnnnttt"<<">>>" cout<<"进出货演示"<<"<<<"<<endl<<endl; cout<<"ttt"<<"1.进货登记."<<endl<<endl; cout&l
21、t;<"ttt"<<"2.出货登记."<<endl<<endl; cout<<"ttt"<<"3.获取库存信息."<<endl<<endl; cout<<"ttt"<<"4.退出."<<endl<<endl; cout<<"ttt"<<"请选择操作:" cin>>c
22、hioce; return (chioce);/CarloDemo.cpp#include <iostream.h>#include "Menu.h"#include "Carlo.h"void main() Menu menu;Carlo carlo; int chioce; while( (chioce = menu.show() - 4) switch(chioce) case 1: carlo.BuyBox(); break; case 2: carlo.SellBox(); break; case 3: carlo.ShowBoxI
23、nfor(); break;南昌航空大学实验报告年 月 日课程名称:面向对象程序设计 实验名称:构造函数与析构函数 班 级: XXXXXXX 学生姓名: XXXXXX 学号: 指导教师评定: XX 签名: XXXXX 1、实验目的·理解类与对象的区别。 ·学习掌握定义构造函数与析构函数的方法。 ·学习把握默认构造函数的意义。·了解类成员初始化,掌握构造与析构类成员的方式。2、实验内容(1)创建一个Employee类,该类中用字符数组存放Employee的信息,如姓名、地址、市、省、及邮政编码;每个成员函数的定义放在类定义之外;成员函数包括改变姓名数据成员
24、等;构造函数完成成员数据的初始化;用Display()函数将完整的对象数据打印出来;其中数据成员是保护的,成员函数是公共的。 #include <iostream.h>#include <string.h>#include <stdio.h>class Employeepublic:Employee(char n20,char a50,char s20,char sh20,char p6);void ChangeName(char ch_name20);void Display();protected:char name20;char address50;c
25、har shi20;char sheng20;char post6;Employee:Employee(char n20,char a50,char s20,char sh20,char p6)strcpy(name,n);strcpy(address,a);strcpy(shi,s);strcpy(sheng,sh);strcpy(post,p);void Employee:ChangeName(char ch_name20)strcpy(name,ch_name);void Employee:Display()cout <<"name:" <<n
26、ame <<endl <<"address:"<<address <<endl <<"shi:"<<shi <<endl <<"sheng:"<<sheng <<endl <<"post:"<<post <<endl;void main()Employee em("rer r34t3","5 West St.","
27、;Revere","CA","12290");em.Display();em.ChangeName("sdfesfefe");em.Display();(2) 设计一个表示矩形的类Rect,其矩形成员长float * Length ,宽float * Width为指针变量,设计相应成员函数,实现下列功能: 构造函数设置长和宽(默认为1)。 复制构造函数用老对象生成新对象。 set()函数设置长和宽(默认为0)。 计算并返回长方形的周长。 计算并返回长方形的面积。 析构函数释放动态分配的长和宽。编制主程序应用指针建立对象测试类
28、。#include <iostream.h>class Rectfloat *Length,*Width;public:Rect(float a=1,float b=1);Rect(Rect &);void set(float a=0,float b=0);float peri();float area();Rect()delete Length;delete Width;Rect:Rect(float a,float b) Length=new float; Width=new float;*Length=a;*Width=b;Rect:Rect(Rect &x)
29、 Length=new float; Width=new float;*Length=*x.Length;*Width=*x.Width;cout<<"执行复制构造函数"<<endl;void Rect:set(float a,float b)*Length=a;*Width=b;float Rect:peri()return (*Length + *Width)*2;float Rect:area()return (*Length) * (*Width);/主函数void main()Rect r1(55.5,40);cout<<&qu
30、ot;周长为:"<<r1.peri()<<" 面积为: "<<r1.area()<<endl;cout<<"-n"Rect *p=new Rect(r1);/思考:此处改为Rect *p=new Rect; 下一行的输出会是什么结果cout<<"周长为:"<<p->peri()<<" 面积为: "<<p->area()<<endl;p->set(20.5,13);cou
31、t<<"周长为:"<<p->peri()<<" 面积为: "<<p->area()<<endl;南昌航空大学实验报告年 月 日课程名称:面向对象程序设计 实验名称:继承与多态性 班 级: XXXXXXX 学生姓名: XXXXXX 学号: 指导教师评定: XX 签名: XXXXX 1、实验目的了解类的两种使用方式学习从现有类派生出新类的方式了解在派生类中如何使用基类的成员了解基类成员在派生类中的访问控制了解虚函数对多态性的支持2、实验内容题目:应用多态设计学生类生成表示学生的类XS,提
32、供成员函数dispXM()、dispXB()和dispNL()分别用来显示姓名、性别和年龄,并将他们全部定义为纯虚函数;生成CZS类表示初中生,包含数据成员xm、xb和nl表示学生的姓名、性别和年龄,提供成员函数dispXM()、dispXB()和dispNL()分别用来显示姓名、性别和年龄;再生成类GZS表示高中生和类DXS表示大学生,同样包含相同含义的数据成员xm、xb和nl,也包括成员函数dispXM()、dispXB()和dispNL()。实验要求 设计和实现基类XS。 设计和实现派生类CZS、GZS、DXS。 分别生成CZS、GZS、DXS类的对象。 将CZS、GZS、DXS类对象的
33、地址赋给XS类的指针变量。 分别用XS类的指针和引用访问dispXM()、dispXB()和dispNL()函数。 观察程序结果。#include <iostream.h>#include <string.h>/设计抽象类(XS)表示学生类,定义其中dispXM(),dispXB(),dispNL()三个函数为纯虚函数class XSpublic:virtual void dispXM()=0;virtual void dispXB()=0;virtual void dispNL()=0;/*定义CZS类,使其继承XS类,包含三个数据成员xm(4个以内汉字,默认名为&q
34、uot;张三"), xb(int型,1表示男,0表示女,默认为1),nl(int型,默认为14);定义构造函数,初始化各数据成员.另:实现基类的三个纯虚函数,显示各数据成员.*/class CZS:public XSpublic:CZS(char * m="张三",int b=1,int n=14);void dispXM();void dispXB();void dispNL();protected:char xm9;int xb,nl;/仿照上面的写法,定义高中生类(GZS)和大学生类(DXS)class GZS:public XSpublic:GZS(cha
35、r * m="张三",int b=1,int n=17);void dispXM();void dispXB();void dispNL();protected:char xm9;int xb,nl;class DXS:public XSpublic:DXS(char * m="张三",int b=1,int n=20);void dispXM();void dispXB();void dispNL();protected:char xm9;int xb,nl;/实现上面CZS类的成员函数CZS:CZS(char * m,int b,int n)strcpy(xm,m);xb=b;nl=n;void CZS:dispXM()cout<<endl<<"初中生的姓名为:"<<xm;void CZS:dispXB()cout<<"t性别:"<<(xb=1?"男":"女");void CZS:dispNL()cout<<"t年龄:"<<nl;/实现上面GZS类
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 探寻中国经济发展密码:通货膨胀与经济增长的动态权衡与协同策略
- 高中人工智能教育课程教学效果评价方法与实践研究教学研究课题报告
- 2026年航天航空行业商业航天创新报告
- 2026年农业基因编辑技术行业报告
- 2025年智能音箱内容五年投资趋势报告
- 环境监测数据采集系统部署手册
- 农产品电商平台运营规范
- 2026年人工智能技术原理与应用试题集
- 2026年软件项目中的需求分析与设计考核题
- 2026年语言学新理论与现代外语教学方法试题集
- 小学阶段关联词重点归纳
- 华住协议书酒店
- 高标准农田建设工程质量专项整治技术手册
- 海关面试题目解析及答案
- 2025年江西省农村(社区)“多员合一岗”工作人员招聘考试历年参考题库含答案详解(5套)
- (高清版)DB44∕T 1075-2012 《蒸压陶粒混凝土墙板》
- 体育场馆方案汇报
- 2025中国西电集团校园招聘笔试历年参考题库附带答案详解
- 2025年苏州市中考物理试卷真题(含答案)
- 变电站综合自动化课件 二次回路识图
- 家用太阳能与风能发电系统在节约电力资源中的应用研究
评论
0/150
提交评论