c++程序设计实验报告.docx
试验报告七 类与对象1. 试验目的(1) 把握类的定义和实现。(2) 把握对象创立及使用的根本方法。2. 试验设备硬件环境:微型计算机软件环境:操作系统: Windows语言环境: Visual C+3. 试验内容(1) 下面程序定义了一个以hours, minutes 和 seconds 作为数据成员的Time 类。设计了成员函数将两个Time 对象相加即时间相加,并进展相应的检查,查看增加的分钟数及秒数是否大于59。假设秒数大于 59,则分钟数向前递增 1。类似地,假设分钟数大于 59,则小时数向前增 1。#include <iostream.h>class Timeprivate:int hours, minutes, seconds; public:void get_timecin>>hours>>minutes>>seconds;void display_timecout<<hours<<”:”<<minutes<<”:”<<seconds<<endl;void add_time(Time & t1, Time & t2)hours=t1.hours+t2.hours; minutes=t1.minutes+t2.minutes; seconds=t1.seconds+t2.seconds; if(seconds>=60)seconds-=60; minutes+;if(minutes>=60)minutes-=60; hours+;void mainTime one, two, three;cout<<“nEnter the first time(hours minutes seconds):“; one.get_time;cout<<“nEnter the second time(hours minutes seconds):“; two.get_time;three.add_time(one,two); cout<<“the result is:“<<endl; three.display_time;根本要求l 上机录入、调试上面程序。l 运行程序,输入下面两组数据:23445147562 67 100156200分析运行结果是否正确。分析与思考l 定义构造函数对Time 类的对象进展初始化即不用成员函数get_time。l 该程序要求用户输入的分钟数和秒数必需小于 60,如何修改程序使得用户在输入分钟数和秒数大于等于 60时,也能得到正确的结果。(2) 阅读下面的一段程序代码,代码可能有错误,请认真分析并体会。class Date public:void Date;int Date(int year,int month,int day); void Date;int &GetYearreturn year;int &GetMonthreturn month;int &GetDayreturn day; private:int year=2023; int month=12; int day=31;static bool IsLeapyear;/是否闰年;bool Date:IsLeapyear=true;int Date:Date(int year,int month,int day)(*this).year=year; (*this).month=month; (*this).day=day;void mainint year,month,day; cin>>year>>month>>day; Date mydate(year,month,day);int&myyear=mydate.GetYear; int&mymonth=mydate.GetMonth; int&myday=mydate.GetDay;cout<<myyear<<endl<<mymonth<<endl<<myday<<endl; myyear=8888;cout<< mydate.GetYear;根本要求·认真阅读上面程序,假设有错误,请更正。·上机录入、调试上面程序。分析和思考main 函 数 中 int &myyear=mydate.GetYear; 、 int &mymonth=mydate.GetMonth;和 int &myday=mydate.GetDay;语句表达的是什么思想?这样做的目的是什么?这种方法是否“好”呢?为什么?假设“不 好”应当怎样修改?4. 源代码1.#include <iostream.h> class Timeprivate:int hours, minutes, seconds;public:Time Time (int x,int y,int z)hours=x;minutes=y;seconds=z;/* void get_timecin>>hours>>minutes>>seconds;*/void display_timecout<<hours<<”:”<<minutes<<”:”<<seconds<<endl;void add_time(Time & t1, Time & t2)hours=t1.hours+t2.hours; minutes=t1.minutes+t2.minutes; seconds=t1.seconds+t2.seconds; while(seconds>=60)seconds-=60; minutes+;while(minutes>=60)minutes-=60; hours+;void mainTime one(2 , 67 , 100), two( 1 , 56 , 200), three; three.add_time(one,two);cout<<“the result is:“<<endl; three.display_time;2.#include<iostream.h>class Date public:Date;Date(int year,int month,int day);Date;int &GetYearreturn year;int &GetMonthreturn month; int &GetDayreturn day;private:int year; int month; int day;static bool IsLeapyear;/是否闰年;bool Date:IsLeapyear=true; Date:Date(int year,int month,int day)(*this).year=year; (*this).month=month; (*this).day=day;void mainint year,month,day; cin>>year>>month>>day; Date mydate(year,month,day);int&myyear=mydate.GetYear; int&mymonth=mydate.GetMonth; int&myday=mydate.GetDay;cout<<myyear<<endl<<mymonth<<endl<<myday<<endl; myyear=8888;cout<< mydate.GetYear;5. 代码测试1.2.6. 测试过程和运行结果分析1、if(seconds>=60)seconds-=60; minutes+;if(minutes>=60)minutes-=60; hours+;用 if 时当 seconds 和 minutes>=60 时,程序只减一次 60,假设 seconds 和 minutes 是 60 的两倍或以上的话,明显减的不够。所以改用while 的话就可以很好的解决这个问题了。2、int&myday=mydate.GetDay;是对 mydate.GetDay的引用,相当于给它起了个别名叫做myday,所以当myyear=8888;时,cout<< mydate.GetYear;输出的也是 8888.7. 思考题解答main 函 数 中 int &myyear=mydate.GetYear; 、 int &mymonth=mydate.GetMonth;和 int &myday=mydate.GetDay;语句表达的是什么思想?这样做的目的是什么?这种方法是否“好”呢?为什么?假设“不 好”应当怎样修改?答:int &myyear=mydate.GetYear; 、int &mymonth=mydate.GetMonth; 和 int &myday=mydate.GetDay;是引用,相当于给右边的变量起了个别名。这样做,“myyear=8888;cout<< mydate.GetYear;”输出的就是 8888 了。这样不好,破坏了类的封装性,导致类的私有成员数据在类外可以被任凭修改。试验报告八 继承与派生类1. 试验目的(1) 把握单继承程序设计的根本方法。(2) 把握多继承程序设计的根本方法。2. 试验设备硬件环境:微型计算机软件环境:操作系统: Windows语言环境: Visual C+3. 试验内容(1) 下面程序定义一个vehicle 类,并派生出car 和 truck 两个派生类。#include<iostream.h>class vehicleprotected:int wheels; double weight;public:;void initialize(int whls, double wght); int get_wheels return wheels; double get_weight return weight; double wheel_loading return weight/wheels; class car: public vehicleprivate:int passenger_load;public:;void initialize(int whls, double wght, int people =4); int passengers return passenger_load; class truck: public vehicleprivate:int passenger_load; double payload;public:;void init_truck(int number =2, double max_load =24000.0); double efficiency;int passengers return passenger_load; void vehicle:initialize(int whls, double wght)wheels=whls; weight=wght;void car:initialize(int whls, double wght, int people)wheels=whls;weight=wght; passenger_load=people;void truck:init_truck(int number, double max_load)passenger_load=number; payload=max_load;double truck:efficiencyreturn payload/(payload+weight);void mainvehicle bicycle; bicycle.initialize(2,25);cout<<“the bicycle has “<<bicycle.get_wheels<<“ wheels.n“; cout<<“the bicycle weighs “<<bicycle.get_weight<<“ pounds.n“;cout<<“the bicycle”s wheel loading is “<<bicycle.wheel_loading<<“ pounds per tire.nn“;car audi; audi.initialize(4,3500.0,5);cout<<“the audi has “<<audi.get_wheels<<“ wheels.n“; cout<<“the audi weighs “<<audi.get_weight<<“ pounds.n“;cout<<“the audi”s wheel loading is “<<audi.wheel_loading<<“ pounds per tire.nn“;truck jief; jief.initialize(18,12500.0); jief.init_truck(2,33675.0);cout<<“the jief has “<<jief.get_wheels<<“ wheels.n“; cout<<“the jief weighs “<<jief.get_weight<<“ pounds.n“;cout<<“the jief”s efficiency is “<<100.0*jief.efficiency<<“ percent.n“;根本要求l 上机录入、调试上面程序。l 运行程序,观看运行结果是否正确且满足题意要求。l 将 class car: public vehicle 和 class truck: public vehicle 分别改为: class car: private vehicle 和 class truck: private vehicle程序运行结果有无变化,为什么?分析与思考l 定义并实现 vehicle 类、car 类和 truck 类的构造函数,完成vehicle 类、car 类和 truck 类的数据成员初始化工作。l 将 vehicle 中数据成员wheels 和 weight 改为private 性质,如何修改程序以到达一样的输出结果。personperson2下面程序对应图 1 所示的类层次继承构造: #include <iostream.h>teachergraduate#include <iomanip.h> #include <string.h>class personin-service_graduateprotected:char name20;int birth_year; public:person(char *na, int year) strcpy(name,na); birth_year=year;int cal_age(int this_year) return this_year-birth_year;class graduate :public personprotected: int grade;char specialty20; public:graduate(char *na, int y, int g, char *spec):person(na,y) grade=g;strcpy(specialty,spec);void display(int this_year) cout<<“graduateagegradespecialtyn“; cout<<setw(20)<<name<<setw(5)<<cal_age(this_year); cout<<setw(7)<<grade<<setw(17)<<specialty<<endl;class teacher :public personprotected:char title15;char specialty20; public:teacher(char *na, int y, char *ti, char *spec):person(na,y) strcpy(title,ti);strcpy(specialty,spec);void display(int this_year) cout<<“teacheragetitlespecialtyn“; cout<<setw(20)<<name<<setw(5)<<cal_age(this_year); cout<<setw(14)<<title<<setw(17)<<specialty<<endl;class in_service_graduate:public teacher, public graduatepublic:in_service_graduate(char *na, int y, char *ti, char *spec1, int g, char *spec2): teacher(na, y, ti, spec1), graduate(na, y, g, spec2)void display(int this_year) cout<<“ in_service_graduateagetitlework_specialtygradestudy_specialtyn“; cout<<setw(20)<<name<<setw(5)<<cal_age(this_year)<<setw(10)<<title; cout<<setw(15)<<teacher:specialty<<setw(7)<<grade<<setw(17)<<graduate:specialty<<endl;void maingraduate gr(“zhang_ling“,1978,2023,“computer“); teacher te(“wang_qiang“, 1976,“tutor“,“electronics“);in_service_graduate sg(“liu_hua“,1975,“lectuer“,“automation“,2023,“computer“); gr.display(2023);cout<<endl; te.display(2023); cout<<endl; sg.display(2023);根本要求l 阅读程序,完成in_service_graduate 类中的display函数的程序。l 上机录入、调试上面程序。l 运行程序,观看运行结果是否正确且满足题意要求。分析与思考l 在上面程序中类person 中的数据成员name 和 birth_year 在 in_service_graduate 类中有两个副本,请使用虚基类使它们在in_service_graduate 类中只有一个副本。留意同时修改程序的其他相关局部。4. 源代码1.#include<iostream.h> class vehicleprotected:int wheels; double weight;public: vehicle(int x,double y)wheels=x;weight=y;int get_wheels return wheels; double get_weight return weight; double wheel_loading return weight/wheels; ;class car: public vehicleprivate:int passenger_load;public:car(int x,double y,int z=4):vehicle(x,y)passenger_load=z;/ void initialize(int whls, double wght, int people=4 ); int passengers return passenger_load; ;class truck: public vehicleprivate:int passenger_load; double payload;public: truck(int x,double y,int z=2,double w=24000.0):vehicle(x,y)passenger_load=z;payload=w;/ void init_truck(int number =2, double max_load =24000.0); double efficiency;int passengers return passenger_load; ;double truck:efficiencyreturn payload/(payload+weight);void mainvehicle bicycle(2,25);cout<<“the bicycle has “<<bicycle.get_wheels<<“ wheels.n“; cout<<“the bicycle weighs “<<bicycle.get_weight<<“ pounds.n“;cout<<“the bicycle”s wheel loading is “<<bicycle.wheel_loading<<“ pounds per tire.nn“;car audi(4,3500.0,5);cout<<“the audi has “<<audi.get_wheels<<“ wheels.n“; cout<<“the audi weighs “<<audi.get_weight<<“ pounds.n“;cout<<“the audi”s wheel loading is “<<audi.wheel_loading<<“ pounds per tire.nn“; truck jief(18,12500.0,2,33675.0);cout<<“the jief has “<<jief.get_wheels<<“ wheels.n“; cout<<“the jief weighs “<<jief.get_weight<<“ pounds.n“;cout<<“the jief”s efficiency is “<<100.0*jief.efficiency<<“ percent.n“;2.#include <iostream.h> #include <iomanip.h> #include <string.h>class personprotected:char name20; int birth_year;public:person(char *na, int year) strcpy(name,na); birth_year=year;int cal_age(int this_year) return this_year-birth_year;class graduate :virtual public personprotected:int grade;char specialty20; public:graduate(char *na, int y, int g, char *spec):person(na,y) grade=g;strcpy(specialty,spec);void display(int this_year) cout<<“graduateagegradespecialtyn“; cout<<setw(20)<<name<<setw(5)<<cal_age(this_year); cout<<setw(7)<<grade<<setw(17)<<specialty<<endl;class teacher :virtual public personprotected:char title15;char specialty20; public:teacher(char *na, int y, char *ti, char *spec):person(na,y) strcpy(title,ti);strcpy(specialty,spec);void display(int this_year) cout<<“teacheragetitlespecialtyn“; cout<<setw(20)<<name<<setw(5)<<cal_age(this_year); cout<<setw(14)<<title<<setw(17)<<specialty<<endl;class in_service_graduate:public teacher, public graduatepublic:in_service_graduate(char *na, int y, char *ti, char *spec1, int g, char *spec2): teacher(na, y, ti, spec1), graduate(na, y, g, spec2),person(na,y)void display(int this_year) cout<<“ in_service_graduateagetitlework_specialtygradestudy_specialtyn“; cout<<setw(20)<<name<<setw(5)<<cal_age(this_year)<<setw(10)<<title; cout<<setw(15)<<teacher:specialty<<setw(7)<<grade<<setw(17)<<graduate:specialty<<endl;void maingraduate gr(“zhang_ling“,1978,2023,“computer“); teacher te(“wang_qiang“, 1976,“tutor“,“electronics“);in_service_graduate sg(“liu_hua“,1975,“lectuer“,“automation“,2023,“computer“); gr.display(2023);cout<<endl; te.display(2023); cout<<endl; sg.display(2023);5. 代码测试1.2.6. 测试过程和运行结果分析1、将 class car: public vehicle 和 class truck: public vehicle 分别改为: class car: private vehicle 和 class truck: private vehicle程序运行无法正常运行了,由于继承访问把握变成私有之后,基类的共有成员在派生类中就会变成私有的了,继承的 函数在主函数中就不能访问了。2、两个基类的virtual 都要添上,否则name 等还是有二义性。不添virtual 时不需要在初始化列表中添加person(na,y),但是只要添了一个virtual,就得加person(na,y)。7. 思考题解答将 vehicle 中数据成员wheels 和 weight 改为private 性质,如何修改程序以到达一样的输出结果。答:将vehicle 中数据成员wheels 和 weight 改为private 性质后,double truck:efficiencyreturn payload/(payload+weight);中的 weight 就无法被访问了。可以定义一个指向weight 的指针来访问。最简洁的方法-把 weight 换成 get_weight就万事大吉了。试验报告九 多态性与虚函数1. 试验目的(1) 把握虚函数定义及实现。(2) 把握具有多态性的面对对象程序设计的根本方法。(3) 把握纯虚函数与抽象类的定义、实现及应用。2. 试验设备硬件环境:微型计算机软件环境:操作系统: Windows语言环境: Visual C+3. 试验内容有一个整数链表,现从今链表派生出一个整数集合类,在集合类中增加一个元素个数的数据项。集合类的插入操 作与链表相像,只是不插入重复元素,并且插入后,元素个数的数据成员需增加。集合类的删除操作是在链表删除操 作的根底上对元素个数做减 1 操作。而查找和输出操作是一样的,因此在集合类中不需要重复定义。#include <iostream.h> #include <conio.h>/ enum bool false,true;struct element /定义链表中的结点构造int val; element *next;class list /定义链表类element *elems; public:list elems=0;list;virtual bool insert(int); /此虚函数在派生类中可重定义virtual bool deletes(int); /此虚函数在派生类中可重定义bool contain(int);void print;class set:public list /将集合类set 定义为链表类list 的派生类int card;public:set card=0; bool insert(int);/重定义此函数bool deletes(int); /重定义此函数;list:list/list 类得析构函数定义,循环释放各元素所占的存储element *tmp=elems;for(element *elem=elems; elem!=0;)tmp=elem; elem=elem->next;delete tmp;bool list:insert(int val)/定义 list 类中插入元素的成员函数element *elem=new element;/为元素安排存储if (elem!=0) elem->val=val;/将元素插入到链表头elem->next=elems;elems=elem; return true;else return false;bool list:deletes(int val)/定义 list 类中删除元素的成员函数if(elems=0) return false;/假设表为空,返回false element *tmp=elems;if(elems->val=val)/假设待删除的元素为表头元素elems=elems->next;delete tmp; return true;elsefor(element *elem=elems; elem->next!=0; elem=elem->next) if(elem->next->val=val)/循环查找待删除元素tmp=elem->next;elem->next=tmp->next; delete tmp;return true;return false;bool list:contain(int val)/判元素 val 在链表中是否存在if(elems=0)return false;if(elems->val=val) return true; elsefor(element *elem=elems; elem->next!=0; elem=elem->next) if(elem->next->val=val)return true;return false;void list:print/输出链表中各元素if(elems=0) return;for(element *elem=elems; elem!=0; elem=elem->next) cout<<elem->val<<“;cout<<endl;bool set:insert(int val)/在 set 类中的insert 的重定义版本if(1)/先推断此元素