c++实验指导书.doc
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《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(
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+ 实验 指导书
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内