收藏 分销(赏)

c++程序设计实验报告.pdf

上传人:曲**** 文档编号:6509256 上传时间:2024-12-10 格式:PDF 页数:41 大小:1.26MB
下载 相关 举报
c++程序设计实验报告.pdf_第1页
第1页 / 共41页
c++程序设计实验报告.pdf_第2页
第2页 / 共41页
点击查看更多>>
资源描述
实验报告七类与对象1.实验目的(1)掌握类的定义和实现。(2)掌握对象创建及使用的基本方法。2.实验设备硬件环境:微型计算机软件环境:操作系统:Windows语言环境:Visual C+3.实验内容(1)下面程序定义了一个以hours,minutes和seconds作为数据成员的Time类。设计了成员函数将两个Time对 象相加(即时间相加),并进行相应的检查,查看增加的分钟数及秒数是否大于59。如果秒数大于59,则分钟数向前 递增1。类似地,如果分钟数大于59,则小时数向前增1。#include class Time(private:int hours,minutes,seconds;public:void get_time()(cinhoursminutesseconds;)void display_time()(couthours,:,niinutes,:,secondsendl;)void add_time(Time&tl,Time&t2)(hours=tl.hours+t2.hours;minutes=t 1.minutes+t2.minutes;seconds=tl.seconds+t2.seconds;if(seconds=60)seconds-=60;minutes+;)if(minutes=60)(minutes-=60;hours+;);void main()(Time one,two,three;coutnEnter the first time(hours minutes seconds):*;one.get_time();coutnEnter the second time(hours minutes seconds):n;two.get_time();three.add_time(one,two);coutthe result is:endl;three.display_time();基本要求 上机录入、调试上面程序。运行程序,输入下面两组数据:2 34 451 47 56 2 67 1001 56 200分析运行结果是否正确。分析与思考 定义构造函数对Time类的对象进行初始化(即不用成员函数get_time。该程序要求用户输入的分钟数和秒数必须小于60,如何修改程序使得用户在输入分钟数和秒数大于等于60 时,也能得到正确的结果。(2)阅读下面的一段程序代码,代码可能有错误,请仔细分析并体会。class Date public:void Date();int Date(int year,int month,int day);void Date();int&GetYear()return year;int&GetMonth()return month;int&GetDay()return day;private:int year=2000;int months 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 main()(int year,month,day;cinyearmonthday;Date mydate(year,month,day);int&myyear=mydate.GetYear();int&mymonth=mydate.GetMonth();int&myday=mydate.GetDayO;coutmyyearendlmymonthendlmydayendl;myyear=8888;cout mydate.GetYear();)基本要求仔细阅读上面程序,如果有错误,请更正。上机录入、调试上面程序。分析和思考main 函数中 int&my y ear=my date.Get Year();、int&mymonth=mydate.GetMonthQ;和 int&myday二mydate.GetDay。;语句表达的是什么思想?这样做的目的是什么?这种方法是否“好”呢?为什么?如果“不 好”应该怎样修改?4.源代码l.#include class Time(private:int hours,minutes,seconds;public:Time()Time(int x,int y,int z)hours=x;minutes=y;seconds=z;/*void get_time()(cinhoursminutesseconds;*/void display_time()(couthours,:,minutes,:,secondsendl;)void add_time(Time&tl,Time&t2)(hours=tl.hours+t2.hours;minutes=tl.minutes+t2.minutes;seconds=tl.seconds+t2.seconds;while(seconds=60)(seconds-=60;minutes+;)while(minutes=60)(minutes-=60;hours+;void main()(Time one(2,67,100),two(1,56,200),three;three.add_time(one,two);coutthe result is:nendl;three.display_time();2.#includeclass Date public:Date();Date(int year,int month,int day);Date。什;int&GetYear()return year;int&GetMonth()return month;int&GetDay()return 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 main()(int year,month,day;cinyearmonthday;Date mydate(year,month,day);int&myy ear=my date.GetYearQ;int&mymonth=mydate.GetMonthQ;int&my day=mydate.GetDayQ;coutmyyearendlmymonthendlmydayendl;myyear=8888;cout my date.Get Year();5.代不马测试1.E D:LITTERDebug4647.exethe result is:5:8:0Press any key to continue2.正D:LnTERDebug4647.exe-2011E1620116168888Press any key to continue6.测试过程和运行结果分析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.GetYearQ;输出的也是 8888.7 思考题解答main 函数中 int&my y ear=my date.Get Year();、int&mymonth=mydate.GetMonth();和 int&myday=mydate.GetDay();语句表达的是什么思想?这样做的目的是什么?这种方法是否“好”呢?为什么?如果“不 好”应该怎样修改?答:int&myyear=mydate.GetYear();、int&mymonth=mydate.GetMonth();和 int&myday=mydate.GetDay();是弓 I 用,相当于给右边的变量起了个别名。这样做,“myyear=8888;coutmydate.GetYear();输出的就是8888 了。这样不好,破坏了类的封装性,导致类的私有成员数据在类外可 以被随意修改。实验报告八继承与派生类1.实验目的(1)掌握单继承程序设计的基本方法。(2)掌握多继承程序设计的基本方法。2.实验设备硬件环境:微型计算机软件环境:操作系统:Windows语言环境:Visual C+3.实验内容(1)下面程序定义一个vehicle类,并派生出car和truck两个派生类。#includeclass 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 vehicle(private:int passenger_load;public:void initialize(int whls,double wght,int people=4);int passengersQ return passenger_load;);class truck:public vehicle(private:int passenger_load;double payload;public:void init_truck(int number=2,double max_load=24000.0);double efficiency();int passengersQ return passenger_load;);void vehicle:initialize(int whls,double wght)(wheels=whls;weigh仁wght;)void car:initialize(int whls,double wght,int people)(wheels=whls;weigh仁wght;passenger_load=people;void truck:init_truck(int number,double max_load)passenger_load=number;payload=max_load;作。double truck:efficiency()(return payload/(payload+weight);)void main()(vehicle bicycle;bicycle.initialize(2,25);coutthe bicycle has bicycle.get_wheels()wheelsAn;coutthe bicycle weighs bicycle.get_weight()pounds.Xn;coutthe bicycles wheel loading is nbicycle.wheel_loading()pounds per tire.nnn;car audi;audi.initialize(4,3500.0,5);coutthe audi has audi.get_wheels()wheels.Xn;coutthe audi weighs audi.get_weight()pounds.n;coutthe audis wheel loading is audi.wheel_loading()pounds per tire.nnn;truck jief;jief.initialize(18,12500.0);jief.init_truck(2,33675.0);coutthe jief has jief.get_wheels()wheels.nn;coutthe jief weighs njief.get_weight()poundsAn;coutthe jief s efficiency is n100.0*jief.efficiency()percent.n;)基本要求 上机录入、调试上面程序。运行程序,观察运行结果是否正确且满足题意要求。将 class car:public vehicle 和 class truck:public vehicle 分别改为:class car:private vehicle 和 class truck:private vehicle程序运行结果有无变化,为什么?分析与思考 定义并实现vehicle类、car类和truck类的构造函数,完成vehicle类、car类和truck类的数据成员初始化工 将vehicle中数据成员wheels和weight改为private性质,如何修改程序以达到相同的输出结果。(2)下面程序对应图1所示的类层次继承结构:#include person person#include#include class person(protected:char name 20;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;);Ain-service_graduateclass graduate:public person(protected:int grade;char specialty 20;public:graduate(char*na,int y,int g,char*spec):person(na,y)grade=g;strcpy(specialty,spec);)void display(int this_year)coutn graduate age grade specialtynn;coutsetw(20)namesetw(5)cal_age(this_year);coutsetw(7)gradesetw(17)specialtyendl;);class teacher:public person(protected:char title 15;char specialty 20;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 teacher age title specialtyn;coutsetw(20)namesetw(5)cal_age(this_year);coutsetw(l 4)titlesetw(l 7)specialtyendl;);class in_service_graduate:public teacher,public graduate(public:in_service_graduate(char*na,int y,char*ti,char*specl,int g,char*spec2):teacher(na,y,ti,sped),graduate(na,y,g,spec2)()void display(int this_year)cout in_service_graduate age title work_specialty grade study_specialtyn;coutvvsetw(20)vvnamevvsetw(5)vvcal age(this year)vvsetw(10)vvtitle;coutvvsetw(15)vvteacher:specialtyvvsetw(7)vvgradevvsetw(17)vvgraduate:specialtyvvendl;);void main()(graduate gr(zhang_lingn,l978,2001 1computer);teacher te(nwang_qiang,1976,tutor,electronics);in_service_graduate sg(liu_huan,1975,lectuer,Automation,2002,ncomputern);gr.display(2002);coutendl;te.display(2002);coutendl;sg.display(2002);)基本要求 阅读程序,完成in_service_graduate类中的display。函数的程序。上机录入、调试上面程序。运行程序,观察运行结果是否正确且满足题意要求。分析与思考 在上面程序中类person中的数据成员name和birth_year在in_service_graduate类中有两个副本,请使用虚基类使它们在in_service_graduate类中只有一个副本。注意同时修改程序的其他相关部分。4.源代码#includeclass vehicle(protected: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 vehicle(private: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 passengersQ return passenger_load;);class truck:public vehicle(private: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 passengersQ return passengerjoad;);double truck:efficiency()return payload/(payload+weight);void main()(vehicle bicycle(2,25);coutthe bicycle has bicycle.get_wheels()n wheelsAn;coutthe bicycle weighs bicycle.get_weight()poundsAn;coutthe bicycles wheel loading is bicycle.wheel_loading()pounds per tire.nn;car audi(4,3500.0,5);coutthe audi has naudi.get_wheels()wheelsAn;coutthe audi weighs audi.get_weight()poundsAn;coutthe audis wheel loading is audi.wheel_loading()pounds per tire.nn;truck jief(18,12500.0,2,33675.0);coutnthe jief has njief.get_wheels()wheelsAn;coutthe jief weighs jief.get_weight()poundsAn;coutthe jief s efficiency is n100.0*jief.efficiency()percent.nn;)2.#include#include#include class person(protected:char name 20;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 person(protected:int grade;char specialty 20;public:graduate(char*na,int y,int g,char*spec):person(na,y)grade=g;strcpy(specialty,spec);)void display(int this_year)cout graduate age grade specialtyll;coutsetw(20)namesetw(5)cal_age(this_year);coutsetw(7)gradesetw(17)specialtyendl;class teacher:virtual public person(protected:char title 15;char specialty 20;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 teacher age title specialtyn;coutsetw(20)namesetw(5)cal_age(this_year);coutsetw(14)titlesetw(17)specialtyendl;class in_service_graduate:public teacher,public graduate(public:in_service_graduate(char*na,int y,char*ti,char*specl,int g,char*spec2):teacher(na,y,ti,sped),graduate(na,y,g,spec2),person(na,y)()void display(int this_year)cout in_service_graduate age title work_specialty grade study_specialtyn;coutsetw(20)namesetw(5)cal_age(this_year)setw(10)title;coutsetw(15)teacher:specialtysetw(7)gradesetw(17)graduate:specialtyendl;void main()graduate gr(zhang_ling,l978,2001/computer);teacher te(nwang_qiangn,1976,tutor,Electronics*);in_service_graduate sg(liu_huan,1975,lectuer,automation,2002,computer*);gr.display(2002);coutendl;te.display(2002);coutendl;sg.display(2002);5.代干马测试i.J D:LITTERDebug4647,exethe bicycle has 2 wheels.the bicycle weighs 25 pounds.the bicycles wheel loading is 12.5 pounds per tire.the audi has 4 wheels.the audi weighs 3500 pounds.the audiJ s wheel loading is 875 pounds per tire.the jief has 18 wheels.the jief weighs 12500 pounds.the jiefJ s efficiency is 72.9291 percent.Press any key to continue2.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:efficiency()(return payload/(payload+weight);)中的weight就无法被访问了。可以定义一个指向weight的指针来访问。最简单的方法把weight换成get_weight()就万事大吉了。实验报告九多态性与虚函数1.实验目的(1)掌握虚函数定义及实现。(2)掌握具有多态性的面向对象程序设计的基本方法。(3)掌握纯虚函数与抽象类的定义、实现及应用。2.实验设备硬件环境:微型计算机软件环境:操作系统:Windows语言环境:Visual C+3.实验内容有一个整数链表,现从此链表派生出一个整数集合类,在集合类中增加一个元素个数的数据项。集合类的插入操 作与链表相似,只是不插入重复元素,并且插入后,元素个数的数据成员需增加。集合类的删除操作是在链表删除操 作的基础上对元素个数做减1操作。而查找和输出操作是相同的,因此在集合类中不需要重复定义。#include#include/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;若表为空,返回 falseelement*tmp=elems;if(elems-val=val)/若待删除的元素为表头元素elems=elems-next;delete tmp;return true;)elsefor(element*elem=elems;elem-next!=O;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=O)retum false;if(elems-val=val)return true;elsefor(element*elem=elems;elem-next!=O;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)coutelem-valn”;coutendl;)bool set:insert(int val)在 set 类中的 insert 的重定义版本(if(1)先判断此元素是否存在,然后再调用基类的此函数版本+card;return true;)return false;)bool set:deletes(int val)/在 set 类中的 deletes 的重定义版本(if(list:deletes(val)调用基类中的此函数版本(;return true;)return false;)int main()(list*ptr,listl;set setl;ptr=&listl;ptr-insert(30);ptr-insert(40);ptr-insert(543);ptr-insert(40);ptr-print();ptr=&setl;ptr-insert(23);ptr-insert(672);ptr-insert(456);ptr-insert(23);ptr-print();getch ;return 1;基本要求 阅读程序,根据题意要求在_处填上合适的内容完成程序。上机录入、调试上面程序。运行程序,设计测试数据,观察运行结果是否正确且满足题意要求。分析与思考 在list类中设计并实现一个从链表头取一个节点(取出后该结点删除,下一个结点作为头结点)并返回节点 值(整数值)的成员函数。在集合类中增加求集合并、交、差的成员函数。4.源代码#include#include/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!=O;)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;若表为空,返回 falseelement*tmp=elems;if(elems-val=val)/若待删除的元素为表头元素elems=elems-next;delete tmp;return true;)elsefor(element*elem=elems;elem-next!=O;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=O)retum false;if(elems-val=val)return true;elsefor(element*elem=elems;elem-next!=O;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)coutelem-val;coutendl;)bool set:insert(int val)在 set 类中的 insert 的重定义版本(if(!contain(val)&list:insert(val)先判断此元素是否存在,然后再调用基类的此函数版本+card;return true;)return false;)bool set:deletes(int val)在 set 类中的 deletes 的重定义版本(if(list:deletes(val)调用基类中的此函数版本 -card;return true;)return false;)int main()(list*ptr,listl;set setl;ptr=&listl;ptr-insert(30);ptr-insert(40);ptr-insert(543);ptr-insert(40);ptr-print();pt
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服