c++程序设计实验报告 .doc
实验报告七 类与对象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_time() cin>>hours>>minutes>>seconds; void display_time() cout<<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 main() Time 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 运行程序,输入下面两组数据: 2 34 451 47 56 2 67 100 1 56 200分析运行结果是否正确。分析与思考l 定义构造函数对Time类的对象进行初始化(即不用成员函数get_time)。l 该程序要求用户输入的分钟数和秒数必须小于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 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 main() int 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_time() cin>>hours>>minutes>>seconds; */ void display_time() cout<<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 main() Time 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 &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; 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:efficiency() return payload/(payload+weight);void main() vehicle 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性质,如何修改程序以达到相同的输出结果。person(2)下面程序对应图1所示的类层次继承结构:person#include <iostream.h>#include <iomanip.h>graduateteacher#include <string.h>class person protected:char name20;in-service_graduateint 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 person protected: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<<" graduate age grade specialtyn"cout<<setw(20)<<name<<setw(5)<<cal_age(this_year);cout<<setw(7)<<grade<<setw(17)<<specialty<<endl;class teacher :public person protected: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<<" teacher age title specialtyn"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_graduate age title work_specialty grade study_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 main()graduate gr("zhang_ling",1978,2001,"computer");teacher te("wang_qiang", 1976,"tutor","electronics");in_service_graduate sg("liu_hua",1975,"lectuer","automation",2002,"computer");gr.display(2002);cout<<endl;te.display(2002);cout<<endl;sg.display(2002);基本要求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:efficiency() return payload/(payload+weight);void main() vehicle 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 person protected: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 person protected: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<<" graduate age grade specialtyn"cout<<setw(20)<<name<<setw(5)<<cal_age(this_year);cout<<setw(7)<<grade<<setw(17)<<specialty<<endl;class teacher :virtual public person protected: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<<" teacher age title specialtyn"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_graduate age title work_specialty grade study_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 main()graduate gr("zhang_ling",1978,2001,"computer");teacher te("wang_qiang", 1976,"tutor","electronics");in_service_graduate sg("liu_hua",1975,"lectuer","automation",2002,"computer");gr.display(2002);cout<<endl;te.display(2002);cout<<endl;sg.display(2002);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:efficiency() return 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