c++实验指导书.doc

上传人:豆**** 文档编号:23972462 上传时间:2022-07-03 格式:DOC 页数:86 大小:610.50KB
返回 下载 相关 举报
c++实验指导书.doc_第1页
第1页 / 共86页
c++实验指导书.doc_第2页
第2页 / 共86页
点击查看更多>>
资源描述

《c++实验指导书.doc》由会员分享,可在线阅读,更多相关《c++实验指导书.doc(86页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-datec+实验指导书实验 1 类和对象实验 1 类和对象1.1实验目的和要求(1) 理解类和对象的概念,掌握声明类和定义对象的方法。(2) 掌握构造函数和析构函数的实现方法。(3) 初步掌握使用类和对象编制C+程序。(4) 掌握对象数组、对象指针和string类的使用方法。(5) 掌握使用对象、对象指针和对象引用作为函数参数的方法。(6) 掌握类对象作为成员的使用方法。(7

2、) 掌握静态数据成员和静态成员函数的使用方法。(8) 理解友元的概念和掌握友元的使用方法。1.2实验内容和步骤1. 输入下列程序/test4-1.cpp#includeusing namespace std;class Coordinate public: Coordinate(int x1,int y1) x=x1; y=y1;Coordinate(Coordinate &p);Coordinate() cout”Destructor is callededn”; int getx() return x; int gety() return y;private: int x,y;Coordi

3、nate:Coordinate(Coordinate &p) x=p.x; y=p.y; cout”copy-initialization Constructou is calledn”;int main() Coordinate p1(3,4); Coordinate p2(p1); Coordinate p3=p2; cout”p3=(“p3.getx()”,”p3.gety()”)n”; return(0);(1) 写出程序的运行结果。(2) 将Coordinate类中带有两个参数的构造函数进行修改,在函数体内增添下述语句:cout”Constructor is called.n”;写出

4、程序的运行结果,并解释输出结果。(3)按下列要求进行调试: 在主函数体内,添加下列语句:Coordinate p4;Coordinata p5(2);调试程序时会出现什么错误?为什么?如何对已有的构造函数进行适当修改?这是因为P4和P5(2)没有定义,而导致程序运行时找不到它们的赋值。所以出现报错。(4)经过以上第(2)步和第(3)步的修改后,结合运行结果分析:创建不同的对象时会调用不同的构造函数。2.设计一个4*4魔方程序,让魔方的各行值的和等于各列值的和,并且等于两对角线值的和。例如一下魔方:31 3 5 259 21 19 1517 13 11 237 27 29 1各行、各列以及对角线

5、值的和都是64.【提示】 求4*4魔方的一般步骤如下:(1)设置初始魔方的起始值和相邻元素之间的差值。例如上述魔方的初始魔方的起始值(first)和相邻元素之间的差值(step)分别为: first=1 step=2 (2)设置初始魔方元素的值。例如上述魔方的初始魔方为: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31(3)生成最终魔方。方法如下: 求最大元素值与最小元素值的和sum,该实例的sum是: 1+31=32 用32减去初始魔方所有对角线上元素的值,然后将结果放在原来的位置,这样就可求得最终魔方。本例最终魔方如下: 31 3 5 25 9 21

6、19 15 17 13 11 23 7 27 29 1本题的魔方类magic的参考框架如下: class magic public: void getdata(); void setfirstmagic(); void generatemagic(); void printmagic();private: int m44; int step; int first; int sum;原代码如下:#include using namespace std;class magicpublic:void getdata();void getfirstmagic();void generatemagic(

7、);void printmagic();private:int m44;int step;int first;int sum;void magic:getdata()first = 1;step = 2;sum = 32;void magic:getfirstmagic()int temp = first;for (int i = 0; i 4; +i)for (int n = 0; n 4; +n)min = temp;temp += step;void magic:generatemagic()for (int i = 0, n = 3; (i = 0); +i, -n)mii = sum

8、 - mii;min = sum - min;void magic:printmagic()for (int i = 0; i 4; +i)for (int n = 0; n 4; +n)cout min t;cout endl;int main(void)cout 初始魔方:n;magic square;square.getdata();square.getfirstmagic();square.printmagic();cout 生成最终魔方:n;square.generatemagic();square.printmagic();return 0;运行结果:3.设计一个用来表示直角坐标系

9、的Location类,在主程序中创建类Location的两个对象A和B,要求A的坐标点在第3象限,B的坐标在第2象限,分别采用成员函数和友元函数计算给定两个坐标点之间的距离,要求按如下格式输出结果: A(x1,y1),B(x2,y2) Distance1=d1 Distance1=d2其中:x1、x2、y1、y2为指定的坐标值,d1和d2为两个坐标点之间的距离。【提示】 类Location的参考框架如下: class Location public: Location(double,double); double Getx() double Gety() double distance(Loc

10、ation &); friend double distance (Location &,Location &); private: double x,y;原代码如下:3、#include #include class Location public: Location(double,double); double getx(); double gety(); double distance(Location &); friend double distance(Location & , Location &); private: double x,y; ; Location:Location

11、(double r,double i) x=r; y=i; double Location:getx() return x; double Location:gety() return y; double Location:distance(Location &t) double dx=x-t.x; double dy=y-t.y; return sqrt(dx*dx+dy*dy); double distance(Location &a , Location &b) double dx=a.x-b.x; double dy=a.y-b.y; return (sqrt(dx*dx+dy*dy)

12、; int main() Location A(-1,-1); Location B(-2,2); coutA(A.getx(),A.gety(),B(B.getx(),B.gety()endl; double d=A.distance(B); coutDistance1=dendl; coutDistance2=distance(A,B)endl; return 0; 运行结果:4.声明一个Student类,在该类中包括一个数据成员score(分数)、两个静态数据成员total_score(总分)和count(学生人数);还包括一个成员函数account()用于设置分数、累计学生成绩之和、累

13、计学生人数,一个静态成员函数sum()用于返回学生的成绩之和,另一个静态成员函数average()用于求全班成绩的平均值。在main函数中,输入 某班同学的成绩,并调用上述函数求出全班学生的成绩之和和平均分。代码如下:4、#include using namespace std; class Student public: void account(float); static float sum(); static float average(); private: float score; static float total_score; static int count; ; void

14、 Student:account(float score1) score=score1; total_score+=score; count+; float Student:sum() float k=total_score; return k; float Student:average() float t=total_score/(count-1); return t; float Student:total_score=0.0; int Student:count=0; int main() float score1; int n=0; Student a; cout-请输入学生成绩-e

15、ndl; cout-若要退出输入按00-score1; a.account(score1); if(score1=00) break; n+; while(1); coutthe class size is : nendl; coutthe total score is : a.sum()endl; coutthe average score is : a.average()endl; return 0; 运行结果:5.使用C+的string类,将5个字符串按逆转后的顺序显示出来。例如,逆转前的5个字符串是:Germany Japan America Britain France按逆转后的顺序

16、输出字符串是:France Britain America Japan Germany运行代码:#include #include using namespace std; int main() int i; string ch5; for(i=0;ichi; for(i=4;i=0;i-) coutchi ; coutendlendl; return 0; 运行结果:实验2 派生类与继承2.1实验目的和要求(1) 掌握派生类的声明方法和派生类构造函数的定义方法。(2) 掌握不同继承方式下,基类成员在派生类中的访问属性。(3) 掌握在继承方式下,构造函数与析构函数的执行顺序与构造规则。(4)

17、学习虚基类在解决二义性问题中的作用。2.2实验内容与步骤1.输入下列程序。/test4_1.cpp#includeusing namespace std;class Basepublic:void setx(int i)x=i;Int getx()return x;public:int x;class Derived:public Basepublic:void sety(int i)y=i;int gety()return y;void show()cout”Base:x=”xendl;public:inty;int main()Derived bb;bb,setx(16);bb.sety(

18、25);bb.show();cout”Base:x=”bb.xendl;cout”Derived:y=”bb.yendl;cout”Base:x=”bb.getx()endl;cout”Derived:y=”bb.gety()endl;return 0;(1) 写出程序的运行结果。(2) 按以下要求,对程序进行修改后再调试,指出调试中出错的原因。将基类Base中数据成员x的访问权限改为private时,会出现哪些错误?为什么?将基类Base中数据成员x的访问权限改为protected时,会出现哪些错误?为什么?在源程序的基础上,将派生类Derived的继承方式改为private时,会出现哪些错

19、误?为什么?在源程序的基础上,将派生类Derived的继承方式改为protected时,会出现哪些错误?为什么?2.编写一个学生和教师的数据输入和显示程序。学生数据有编号、姓名、性别、年龄、系别和成绩,教师数据有编号、姓名、性别、年龄、职称和部门。要求将编号、姓名、性别、年龄的输入和显示设计成一个类Person,并作为学生类Student和教师类Teacher的基类。供参考的类结构如下:class Person.;class Student:public Person.;class Teacher:public Person.;3.按要求阅读、编辑、编译、调试和运行以下程序。(1) 阅读、编辑

20、、编译、调试和运行以下程序,并写出程序的运行结果。 /test4_3_1.cpp#include#includeusing namespace std;class MyArraypublic:MyArray(int leng);MyArray;void Input();void Display(string);protected:int*alist;int length;MyArray:MyArray(int leng)if(leng=0)cout”error length”;exit(1);alist=new int leng;length=leng;if(alist=NULL)cout”a

21、ssign failure”;exit(1);cout”MyArray类对象已创建。”endl;MyArray:MyArray()delete alist;cout”MyArray类对象被撤销。”endl;void MyArray:Display(string str)int i;int *p=alist;coutstrlength”个整数:“;for(i=0;ilength;i+,p+)cout*p”;coutendl;void MyArray:Inputcout”请键盘输入”length”个整数:”;int i;int *p =alist;for(i=0;i*p;int main()MyA

22、rray a(5);a.Input();a.Display(“显示已输入的”);return 0;(2) 声明一个类SortArray继承类MyArray,在该类中定义一个函数,具有将输入的整数从小到大进行排序的功能。【提示】在第(1)步的基础上可增加下面的参考框架:class SortArray : public MyArray public:void Sort();SortArray(int leng):MyArray(leng)cout”SortArray类对象已创建。”endl;virtual SortArray();SortArray:SortArray()cout”SortArra

23、y类对象被撤销。”endl;void SortArray:Sort()/请自行编写Sort函数的代码,将输入的整数从小到大排序。/并将主函数修改为:int main()SortArray a(5);s.Input();s.Display(“显示排序以前的”);s.Sort();s.Display(“显示排序以后的”);return 0;声明一个类ReArray继承类MyArray,在该类中定义一个函数,具有将输入的整数进行倒置的功能。【提示】在第(1)步的基础上可增加下面的参考框架:Class ReArray:public MyArrayPublic:Void reverse();ReArra

24、y(int leng);Virtual ReArray();请读者自行编写构造函数、析构函数和倒置函数ReArray,以及修改主函数。(3) 声明一个类AverArray继承类MyArray,在该类中定义一个函数,具有求输入的整数平均值的功能。 【提示】 在第(1)步的基础上增加下面的参考框架:class AverArray:public MyArrayPublic:AverArray(int leng);AverArray();Double Aver();请读者自行编写构造函数、析构函数和求平均值函数Aver(求解整数的平均值),以及修改主函数。(2) 声明一个NewArray类,同时继承了

25、类SortArray,ReArray和AverArray,使得类NewArray的对象同时具有排序、倒置和求平均值的功能。在继承的过程中声明MyArray为虚基类,体会虚基类在解决二义性问题中的作用。实验3 多态性3.1实验目的和要求(1) 了解多态性的概念。(2) 掌握运算符重载的基本方法。(3) 掌握虚函数的定义和使用方法。(4) 掌握纯虚函数和抽象类的概念和用法。3.2实验内容与步骤1.分析并调试下列程序,写出程序的输出结果,并解释输出结果。/test5_1.cpp#includeusing namespace std;class Bpublic:virtual void f1 (dou

26、ble x)cout”B:f1(double)”xendl;void f2(double x)cout”B:f2(double)”2*xendl;void f3(double x)cout”B:f3(double)”3*xendl;class D:public Bpublic:virtual void f1(double x)cout”D:f1(double)”xendl;void f2(double x)cout”D:f2(double)”2*xendl;void f3(double x)cout”D:f3(double)”3*xf1(1.23);pb-f1(1.23);pb-f2(1.23

27、);pb-f3(1.23);pb-f3(3.14);return 0;2.编写一个程序,其中设计一个时间类Time,用来保存时、分、秒等私有数据成员,通过重载操作符“+”实现两个时间的相加。要求将小时范围限制在大于等于0,分钟范围限制在059,秒钟范围限制在059秒。【提示】时间类Timepublic:Time(int h=0,int m=0,int s=0);Time operator+(Time&);void disptime(string);private: int hourse; int minutes; int seconds; 3.给出下面的抽象基类container; class

28、 container protected: double radius; public: container(double radius1); virtual double surface_area()=0; virtual double volume()=0; ;要求建立3个继承container的派生类cube、sphere与cylinder,让每一个派生类都包含虚函数surface_area()和volume(),分别用来计算正方体、球体和圆柱体的表面积及体积。要求写出主程序,应用C+的多态性,分别计算边长为6.0的正方体、半径为5.0的球体,以及半径为5.0和高为6.0的圆柱体的表面积

29、和体积。4.编写一个程序,用于进行集合的并、差和交运算。例如输入整数集合9 5 4 3 6 7和2 4 6 9 ,计算出它们进行集合的并、差和交运算后的结果。【提示】i. 可用以下表达式实现整数集合的基本运算: s1+s2 两个整数集合的并运算s1-s2 两个整数集合的差运算s1*s2 两个整数集合的交运算ii. 参考以下Set类的框架,用于完成集合基本运算所需的各项功能。 class set public: set:set(); void set:input(int d); int set:length(); int set:getd(int i); void set:disp(); set

30、 set:operator+(set s1); set set:operator-(set s1); set set:operator*(set s1); set set:operator=(set s1); protected: int len; int sMAX; ;实验 4 模板与异常处理4.1 实验目的和要求 (1)正确理解模板的概念。 (2)掌握函数模板和类模板的声明和使用方法。 (3)学习简单的异常处理方法。4.2 实验内容和步骤 1.分析并调试下列程序,写出运行结果并分析原因。 (1) /test6_1_1.cpp #include using namespace std; te

31、mplate T max (T x,T y) return xy? x:y;int max(int a,int b)return ab? a:b;double max (double a,double b)return ab? a:b;int main() cout”max(3,7) is “max(3,7)endl; return 0;(2)/test6_1_2.cpp #include using namespace std;int max(int a,int b)return ab? a:b;double max (double a,double b)return ab? a:b;int

32、 main() cout”max(3,7) is “max(3,7)endl; return 0;2. 编写一个求任意类型数组中最大元素和最小元素的程序,要求将求最大元素和最小元素的函数设计成函数模板。3. 编写一个程序,使用类模板对数组元素进行排序、倒置、查找和求和。【提示】设计一个类模板template class Array .;具有对数组元素进行排序、倒置、查找和求和功能,然后产生类型实参分别为int型和double型的两个模板类,分别对整型数组与双精度数组完成所要求的操作。4. 编写一个程序,求输入数的平方根。设置异常处理,对输入负数的情况给出提示。实验 5 C+的流类库与输入输出5

33、.1 实验目的和要求(1)掌握C+格式化的输入输出方法。(2)掌握重载运算符“”的方法。(3)掌握磁盘文件的输入输出方法。5.2 实验内容和步骤 1. 下面给出的test7_1_1.cpp程序用于打印九九乘法表,但程序中存在错误。请上机调试,使得此程序运行后,能够输出如下所示的九九乘法表。* 1 2 3 4 5 6 7 8 91 12 2 43 3 6 9 4 4 8 12 165 5 10 15 20 256 6 12 18 24 30 367 7 14 21 28 35 42 498 8 16 24 32 40 48 56 649 9 18 27 36 45 54 63 72 81/tes

34、t7_1_1.cpp#include #include using namespace std;int main() int i,j; cout”*”; for(i=1;i=9;i+)couti” ”; coutendl;for(i=1;i=9;i+) couti; for(j=1;j=i;j+) Couti*j;return 0;2.下面的程序用于统计文件xyz.txt中的字符个数,请填空完成程序。 /test7_2_1.cpp #include #include using namespace std; int main() char ch;int i=0;ifstream file;fi

35、le.open(“xyz.txt”,ios:in);if( ) cout”xyz.txt cannot open”endl; abort();While (!file.eof() i+; cout”文件字符个数:”iendl; return 0;3.重载运算符“”,使其能够输入一件商品的信息和输出这件商品的信息。商品的信息由编号、商品名和价格。假如商品类Merchandise的框架如下:class merchandisepublic: Merchandiss(); Merchandiss(); friend istream& operator(istream& in,Merchandiss& s); friend ostream&operatormer; coutmer; return 0;4.编写一个程序,将两个文本文件连接成一个文件,然后将此文件中所有小写字母转换成大写字母,并打印出来。-

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 小学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com