版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上实验 1 类和对象1.1实验目的和要求(1) 理解类和对象的概念,掌握声明类和定义对象的方法。(2) 掌握构造函数和析构函数的实现方法。(3) 初步掌握使用类和对象编制C+程序。(4) 掌握对象数组、对象指针和string类的使用方法。(5) 掌握使用对象、对象指针和对象引用作为函数参数的方法。(6) 掌握类对象作为成员的使用方法。(7) 掌握静态数据成员和静态成员函数的使用方法。(8) 理解友元的概念和掌握友元的使用方法。1.2实验内容和步骤1. 输入下列程序/test4-1.cpp#includeusing namespace std;class Coordina
2、te 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;Coordinate:Coordinate(Coordinate &p) x=p.x; y=p.y; cout”copy-initialization Constructou is calledn”;int main() Coordinate p1
3、(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”;写出程序的运行结果,并解释输出结果。(3)按下列要求进行调试: 在主函数体内,添加下列语句:Coordinate p4;Coordinata p5(2);调试程序时会出现什么错误?为什么?如何对已有的构造函数进行适当修改?这是因为P4和P5
4、(2)没有定义,而导致程序运行时找不到它们的赋值。所以出现报错。(4)经过以上第(2)步和第(3)步的修改后,结合运行结果分析:创建不同的对象时会调用不同的构造函数。2.设计一个4*4魔方程序,让魔方的各行值的和等于各列值的和,并且等于两对角线值的和。例如一下魔方:31 3 5 259 21 19 1517 13 11 237 27 29 1各行、各列以及对角线值的和都是64.【提示】 求4*4魔方的一般步骤如下:(1)设置初始魔方的起始值和相邻元素之间的差值。例如上述魔方的初始魔方的起始值(first)和相邻元素之间的差值(step)分别为: first=1 step=2 (2)设置初始魔方
5、元素的值。例如上述魔方的初始魔方为: 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 19 15 17 13 11 23 7 27 29 1本题的魔方类magic的参考框架如下: class magic public: void getdata(); void setfirstmagic(); void generate
6、magic(); void printmagic();private: int m44; int step; int first; int sum;原代码如下:#include using namespace std;class magicpublic:void getdata();void getfirstmagic();void generatemagic();void printmagic();private:int m44;int step;int first;int sum;void magic:getdata()first = 1;step = 2;sum = 32;void ma
7、gic: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 - 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
8、 main(void)cout 初始魔方:n;magic square;square.getdata();square.getfirstmagic();square.printmagic();cout 生成最终魔方:n;square.generatemagic();square.printmagic();return 0;运行结果:3.设计一个用来表示直角坐标系的Location类,在主程序中创建类Location的两个对象A和B,要求A的坐标点在第3象限,B的坐标在第2象限,分别采用成员函数和友元函数计算给定两个坐标点之间的距离,要求按如下格式输出结果: A(x1,y1),B(x2,y2)
9、Distance1=d1 Distance1=d2其中:x1、x2、y1、y2为指定的坐标值,d1和d2为两个坐标点之间的距离。【提示】 类Location的参考框架如下: class Location public: Location(double,double); double Getx() double Gety() double distance(Location &); friend double distance (Location &,Location &); private: double x,y;原代码如下:3、#include #include class Location
10、 public: Location(double,double); double getx(); double gety(); double distance(Location &); friend double distance(Location & , Location &); private: double x,y; ; Location:Location(double r,double i) x=r; y=i; double Location:getx() return x; double Location:gety() return y; double Location:distan
11、ce(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); 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
12、(B); coutDistance1=dendl; coutDistance2=distance(A,B)endl; return 0; 运行结果:4.声明一个Student类,在该类中包括一个数据成员score(分数)、两个静态数据成员total_score(总分)和count(学生人数);还包括一个成员函数account()用于设置分数、累计学生成绩之和、累计学生人数,一个静态成员函数sum()用于返回学生的成绩之和,另一个静态成员函数average()用于求全班成绩的平均值。在main函数中,输入 某班同学的成绩,并调用上述函数求出全班学生的成绩之和和平均分。代码如下:4、#includ
13、e 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 Student:account(float score1) score=score1; total_score+=score; count+; float Student:sum() float k=total_score; retu
14、rn 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-请输入学生成绩-endl; cout-若要退出输入按00-score1; a.account(score1); if(score1=00) break; n+; while(1); coutthe class size is : nendl; coutt
15、he 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按逆转后的顺序输出字符串是:France Britain America Japan Germany运行代码:#include #include using namespace std; int main() int i; string ch5; f
16、or(i=0;ichi; for(i=4;i=0;i-) coutchi ; coutendlendl; return 0; 运行结果:实验2 派生类与继承2.1实验目的和要求(1) 掌握派生类的声明方法和派生类构造函数的定义方法。(2) 掌握不同继承方式下,基类成员在派生类中的访问属性。(3) 掌握在继承方式下,构造函数与析构函数的执行顺序与构造规则。(4) 学习虚基类在解决二义性问题中的作用。2.2实验内容与步骤1.输入下列程序。/test4_1.cpp#includeusing namespace std;class Basepublic:void setx(int i)x=i;Int
17、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(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;
18、return 0;(1) 写出程序的运行结果。(2) 按以下要求,对程序进行修改后再调试,指出调试中出错的原因。将基类Base中数据成员x的访问权限改为private时,会出现哪些错误?为什么?将基类Base中数据成员x的访问权限改为protected时,会出现哪些错误?为什么?在源程序的基础上,将派生类Derived的继承方式改为private时,会出现哪些错误?为什么?在源程序的基础上,将派生类Derived的继承方式改为protected时,会出现哪些错误?为什么?2.编写一个学生和教师的数据输入和显示程序。学生数据有编号、姓名、性别、年龄、系别和成绩,教师数据有编号、姓名、性别、年龄、
19、职称和部门。要求将编号、姓名、性别、年龄的输入和显示设计成一个类Person,并作为学生类Student和教师类Teacher的基类。供参考的类结构如下:class Person.;class Student:public Person.;class Teacher:public Person.;3.按要求阅读、编辑、编译、调试和运行以下程序。(1) 阅读、编辑、编译、调试和运行以下程序,并写出程序的运行结果。 /test4_3_1.cpp#include#includeusing namespace std;class MyArraypublic:MyArray(int leng);MyAr
20、ray;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”assign failure”;exit(1);cout”MyArray类对象已创建。”endl;MyArray:MyArray()delete alist;cout”MyArray类对象被撤销。”endl;void MyArray:Di
21、splay(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()MyArray a(5);a.Input();a.Display(“显示已输入的”);return 0;(2) 声明一个类SortArray继承类MyArray,在该类中定义一个函数,具有将输入的整数从小到大进行排序的功能。【提示】在第(1)
22、步的基础上可增加下面的参考框架:class SortArray : public MyArray public:void Sort();SortArray(int leng):MyArray(leng)cout”SortArray类对象已创建。”endl;virtual SortArray();SortArray:SortArray()cout”SortArray类对象被撤销。”endl;void SortArray:Sort()/请自行编写Sort函数的代码,将输入的整数从小到大排序。/并将主函数修改为:int main()SortArray a(5);s.Input();s.Display
23、(“显示排序以前的”);s.Sort();s.Display(“显示排序以后的”);return 0;声明一个类ReArray继承类MyArray,在该类中定义一个函数,具有将输入的整数进行倒置的功能。【提示】在第(1)步的基础上可增加下面的参考框架:Class ReArray:public MyArrayPublic:Void reverse();ReArray(int leng);Virtual ReArray();请读者自行编写构造函数、析构函数和倒置函数ReArray,以及修改主函数。(3) 声明一个类AverArray继承类MyArray,在该类中定义一个函数,具有求输入的整数平均值
24、的功能。 【提示】 在第(1)步的基础上增加下面的参考框架:class AverArray:public MyArrayPublic:AverArray(int leng);AverArray();Double Aver();请读者自行编写构造函数、析构函数和求平均值函数Aver(求解整数的平均值),以及修改主函数。(2) 声明一个NewArray类,同时继承了类SortArray,ReArray和AverArray,使得类NewArray的对象同时具有排序、倒置和求平均值的功能。在继承的过程中声明MyArray为虚基类,体会虚基类在解决二义性问题中的作用。实验3 多态性3.1实验目的和要求(
25、1) 了解多态性的概念。(2) 掌握运算符重载的基本方法。(3) 掌握虚函数的定义和使用方法。(4) 掌握纯虚函数和抽象类的概念和用法。3.2实验内容与步骤1.分析并调试下列程序,写出程序的输出结果,并解释输出结果。/test5_1.cpp#includeusing namespace std;class Bpublic:virtual void f1 (double 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;cl
26、ass 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);pb-f3(1.23);pb-f3(3.14);return 0;2.编写一个程序,其中设计一个时间类Time,用来保存时、分、秒等私有数据成员,通过重载操作符“+”实现两个时间的相加。要求将小时范围限制在大于等于0,分钟范围限制在
27、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 container protected: double radius; public: container(double radius1); virtual double surface_area()=0; virtual doubl
28、e volume()=0; ;要求建立3个继承container的派生类cube、sphere与cylinder,让每一个派生类都包含虚函数surface_area()和volume(),分别用来计算正方体、球体和圆柱体的表面积及体积。要求写出主程序,应用C+的多态性,分别计算边长为6.0的正方体、半径为5.0的球体,以及半径为5.0和高为6.0的圆柱体的表面积和体积。4.编写一个程序,用于进行集合的并、差和交运算。例如输入整数集合9 5 4 3 6 7和2 4 6 9 ,计算出它们进行集合的并、差和交运算后的结果。【提示】i. 可用以下表达式实现整数集合的基本运算: s1+s2 两个整数集合
29、的并运算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 set:operator+(set s1); set set:operator-(set s1); set set:operator*(set s1); set set:operator=(set s1); protected: in
30、t len; int sMAX; ;实验 4 模板与异常处理4.1 实验目的和要求 (1)正确理解模板的概念。 (2)掌握函数模板和类模板的声明和使用方法。 (3)学习简单的异常处理方法。4.2 实验内容和步骤 1.分析并调试下列程序,写出运行结果并分析原因。 (1) /test6_1_1.cpp #include using namespace std; template 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;
31、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 main() cout”max(3,7) is “max(3,7)endl; return 0;2. 编写一个求任意类型数组中最大元素和最小元素的程序,要求将求最大元素和最小元素的函数设计成函数模板。3. 编写一个程序,使用类模板对数组元素进行排序、倒置、查找和求和。【提示】设计一个类模板template class Array .;具有对数组元素进行排序、倒置、查找和求和功能,然后产生类型实参分别为int型和double型的两个模板类,分别对整型数组与双精度数组完成所要求的操作。4. 编写一个程序,求输入数的平
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年南充职业技术学院单招面试题库及答案
- 2026年湖北省高职单招职业适应性测试题库及答案
- 2026版医疗纠纷预防和处理条例试题与答案
- 初中网络课程设计
- C语言迷宫最佳路径求解课程设计
- bgp协议课程设计摘要
- 洪涝灾害卫星数据监测工具课程设计
- PCA降维分析教程课程设计
- Linu防火墙高级技巧课程设计
- 成产管理课程设计
- 《老年人健康管理实务》老年保健与管理专业全套教学课件
- cnas文件考试试题及答案
- DZ/T 0132-1994钻孔压水试验规程
- 幕墙安全管理制度
- 中医康复中的适宜技术选择试题及答案
- DB37T 1342-2021 平原水库工程设计规范
- 2024低温阀门深冷处理规范
- 广西燃气安全检查标准 DBJ T45-1472-2023(2023年7月1日实施)
- 外聘电工合同范本
- 临床肺泡出血综合征CT影像表现
- JTS-252-2015水运工程施工监理规范
评论
0/150
提交评论