《c++实验报告-.pdf》由会员分享,可在线阅读,更多相关《c++实验报告-.pdf(32页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、面向对象的编程技术实验报告安徽工业大学工商学院2012年 12 月学号姓名班级计 1142 指导教师2 实验一 简单 C+程序设计一、实验目的1、熟悉 VC+6.0 的开发环境,学习用V+6.0 建立标准的C+源程序文件;2、复习基本数据类型、运算符与表达式及基本控制结构的应用;3、使用简单的输入输出,观察头文件的作用。二、实验内容1、以教材例2-1 为例,用 V+6 0 建立标准的C+源程序文件,试修改其中的错误并描述源程序从创建到运行的过程。2-1 运行结果:总结:严重感觉到c+运行环境的复杂和难度,不可能做到上手即会,反复摸索和请教才知道如何运行出结果。2、输入并运行教材例题2-7,即:
2、用do-while 语句编程,求自然数110 之和。程序正确运行之后,去掉源程序中include 语句重新编译,观察会有什么问题。2-7 运行结果:总结:有了第一题的铺垫,这一题做的倒还算顺手,只是对于去掉include 语句后觉得还是不大好做。3、将 2 中 do-while 语句用 for 语句代替,完成相同的功能。源程序(for 语句):#include using namespace std;void main()int i(1),sum(0);for(i=1;i=10;i+)sum+=i;coutsum=sumendl;运行结果:3 体会:只要熟悉dowhile 语句和 for 语句
3、的使用即可。4、编程计算图形的面积。程序可计算圆形、长方形、正方形的面积,运行时先提示用户选择图形的类型,然后对圆形要求用户输入半径值,对长方形要求用户输入长和宽的,对正方形要求用户输入边长的值,计算出面积的值后将其显示出来。源程序:#include using namespace std;void main()cout 请选择你需要的图形endl;cout1 正方形 endl;cout2 长方形 endl;cout3 圆形 a;if(a=1)cout 请输入正方形边长b;s=b*b;coutsendl;if(a=2)cout 请分别输入长方形的长和宽cd;float s;s=c*d;cout
4、 长方形的面积为:sendl;if(a=3)cout 请输入圆的半径r;s=3.14*r*r;cout 圆形的面积为:sendl;三种运行结果:4 心得与体会:首先构造函数,然后分析算法语句,最后写出语句并检测。只是过程中频频犯些小错误,必须严格要求自己,细心,认真,勤奋起来!5、定义一个表示时间的结构体,可以精确表示年、月、日、小时、分、秒;提示用户输入年、月、日、时、分、秒的值,然后完整地显示出来。源程序:#include using namespace std;typedef struct int year;int month;int day;int hour;int minute;5
5、int second;Time;int main()Time*time=new Time();cout time-year;cout time-month;cout time-day;cout time-hour;cout time-minute;cout time-second;cout year 年 month 月 day 日 hour 时 minute 分 second 秒 endl;return 0;运行结果:体会:这道题在做课堂作业时也有,当时拿到题目不知道该如何下手,不知道该用什么语法或是结果,然后我便上网查了下,才知道用结构体比较方便,于是便尽量往便准答案靠近,然后就这样了。实验
6、二函数重载与应用一、实验目的1、掌握函数的定义和调用方法;2、练习重载函数的使用;3、练习使用系统函数。二、实验内容1、编写一个函数把华氏温度转换为摄氏温度,转换公式为:c(F 一 32)*5/9。6 源程序:#include using namespace std;int main()double f,c;coutf;c=(f-32.0)*5.0/9.0;cout“摄氏温度为:”c;return 0;运行结果:总结:对与这题我首先用c 语言的思想考虑了的,再用c+的语言写出来,在运行出结果,再一次在c+上有所成长。2、编写重载函数Max 可分别求取两个整数,三个整数,两个双精度数、三个双精度
7、数的最大值。源程序:#include using namespace std;int max(int a,int b)return(ab)?a:b);int max(int a,int b,int c)int d=max(a,b);return(cd)?c:d);double max(double a,double b)return(ab?a:b);7 double max(double a,double b,double c)double d=max(a,b);return(cd?c:d);void main()coutmax(1,2)endl;coutmax(4,3,5)endl;cout
8、max(7.0,6.0)endl;coutmax(9.0,10.0,8.0)endl;运行结果:体会:做到这题让我想到了C 语言。3、使用系统函数pow(x,y)计算yx的值,注意包含头文件mathh。#include using namespace std;#include int main()int x,y;cout 请输入两个数用空格隔开xy;coutx 的y 次方为:pow(x,y)endl;return 0;运行结果:8 总结与体会:这题让我学会了系统函数pow(x,y)计算yx的值及其使用。4、用递归的方法编写函数求Fibonacci 级数(如下公式),观察递归调用的过程。2112
9、)2()1()(或nnnfibnfibnfib源程序:#include using namespace std;int fib(int n)if(n=2)return 1;return fib(n-1)+fib(n-2);int main()int n;cout n;cout endl;cout 结果:fib(n)endl;system(pause);运行结果:9 总结与体会:作函数题要抓住函数公式的使用,以前也用C 语言做过,对于C+当然有区别,但是总体思想没变,所以做起来难度不算很大。实验三类与对象(一)一、实验目的1、掌握类的定义和使用;2、掌握类的定义和对象的声明;3、复习具有不同访问
10、属性的成员的访问方式;4、观察构造函数和析构函数的执行过程。二、实验内容1定义一个CPU 类,包含等级(rank)、频率(frequency)、电压(voltage)等属性。有两个公有成员函数run、stop。其中,rank 为枚举类型CPU_rank,定义为enum CPU_rank P11,P2,P3,P4,P5,P6,P7,frequency 为单位是MHz 的整型数,voltage 为浮点型的电压值。观察构造函数和析构函数的调用顺序。源程序:#include using namespace std;enum CPU_Rank P1=1,P2,P3,P4,P5,P6,P7;class C
11、PU public:CPU(int fre,float vol,int m);CPU()cout 析构函数被调用endl;void run();void stop();private:CPU_Rank r;int frequency;float voltage;void CPU:run()coutCPU 开始运行 endl;void CPU:stop()coutCPU 停止运行 endl;10 CPU:CPU(int fre,float vol,int m)r=(CPU_Rank)m;frequency=fre;voltage=vol;cout 取得的频率是:frequencyendl;cou
12、t 取得的电压是:voltageendl;cout 输出等级 rendl;cout 构造函数被调用endl;int main()CPU cpu(20,10.0,2);cpu.run();cpu.stop();return 0;运行结果:总结:这题不会做,需要加强练习了。2定义一个简单的Computer 类,有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,有两个公有成员函数run、stop。cpu 为 CPU 类的一个对象,ram 为 RAM 类的一个对象,cdrom 为 CDROM类的一个对象定义并实现这个类。源程序:#include enum CPU_RankP1=1,P2
13、,P3,P4,P5,P6,P7;class CPU private:CPU_Rank rank;int frequency;float voltage;public:CPU(CPU_Rank r,int f,float v)11 rank=r;frequency=f;voltage=v;cout 构造了一个CPU!endl;CPU()cout 析构了一个CPU!endl;CPU_Rank GetRank()constreturn rank;int GetFrequency()constreturn frequency;float GetVoltage()constreturn voltage;
14、void SetRank(CPU_Rank r)rank=r;void SetFrequency(int f)frequency=f;void SetVoltage(float v)voltage=v;void Run()coutCPU开始运行 endl;void Stop()coutCPU停止运行 endl;class RAM int x;public:RAM(int a)x=a;cout 构造了一个RAM!endl;RAM()cout 析构了一个RAM!endl;class CDROM char y;public:CDROM(char v)y=v;cout构造了一个DROM!endl;CD
15、ROM()cout析构了一个DROM!endl;class Computer CPU a;RAM b;CDROM c;public:Computer(CPU_Rank x,int y,float z,int xx,char yy):a(x,y,z),b(xx),c(yy)cout 构造了一个omputerendl;Computer()cout 析构了一个omputer!endl;void Run()coutComputer开始运行 endl;void Stop()coutComputer停止运行 endl;12 void main()Computer niu(P6,3,2.9,4,G);niu
16、.Run();niu.Stop();体会:做的有点头大,不过还是得做,哪怕是借鉴的也得自己琢磨一番,否则将永远止步不前。3调试教材例题42 和 44,分析程序运行结果。42 源程序:#include using namespace std;class Point/Point 类的声明 public:/外部接口Point(int xx=0,int yy=0)X=xx;Y=yy;/构造函数Point(Point&p);/拷贝构造函数int GetX()return X;int GetY()return Y;private:/私有数据int X,Y;/成员函数的实现Point:Point(Point
17、&p)X=p.X;Y=p.Y;cout 拷贝构造函数被调用endl;/形参为 Point 类对象的函数void fun1(Point p)13 coutp.GetX()endl;/返回值为Point 类对象的函数Point fun2()Point A(1,2);return A;/主程序void main()Point A(4,5);/第一个对象A Point B(A);/情况一,用A 初始化 B。第一次调用拷贝构造函数coutB.GetX()endl;fun1(B);/情况二,对象B 作为 fun1 的实参。第二次调用拷贝构造函数B=fun2();/情况三,函数的返回值是类对象,函数返回时,
18、调用拷贝构造函数coutB.GetX()endl;/4_4.cpp#include#include using namespace std;class Point public:Point(int xx=0,int yy=0)X=xx;Y=yy;Point(Point&p);int GetX()return X;int GetY()return Y;private:int X,Y;Point:Point(Point&p)/拷贝构造函数的实现 X=p.X;Y=p.Y;14 coutPoint 拷贝构造函数被调用endl;class Line public:Line(Point xp1,Point
19、 xp2);Line(Line&);double GetLen()return len;private:Point p1,p2;double len;Line:Line(Point xp1,Point xp2):p1(xp1),p2(xp2)coutLine 构造函数被调用endl;double x=double(p1.GetX()-p2.GetX();double y=double(p1.GetY()-p2.GetY();len=sqrt(x*x+y*y);Line:Line(Line&Seg):p1(Seg.p1),p2(Seg.p2)coutLine 拷贝构造函数被调用endl;len=
20、Seg.len;void main()Point myp1(1,1),myp2(4,5);Line line(myp1,myp2);Line line2(line);coutThe length of the line is:;coutline.GetLen()endl;coutThe length of the line2 is:;coutline2.GetLen()endl;15 实验四类与对象(二)一、实验目的1、掌握类的定义和使用;2、掌握类的定义和对象的声明;3、复习具有不同访问属性的成员的访问方式;4、观察构造函数和析构函数的执行过程。二、实验内容1声明并实现一个矩形类,有长、宽两
21、个属性,用成员函数计算矩形的面积。源程序:#include using namespace std;class Rectangle public:Rectangle(float L,float W);float GetArea();private:float length;float wide;Rectangle:Rectangle(float L,float W)length=L;wide=W;float Rectangle:GetArea()return length*wide;void main()float length;float wide;coutlengthwide;Rectan
22、gle A(length,wide);cout 矩形的面积是:A.GetArea()endl;16 2声明一个 Circle 类,有数据成员Radius(半径),成员函数GetArea(),计算圆的面积,构造一个Circle 的对象进行测试。#include using namespace std;class Circle public:Circle(float radius)this-radius=radius;Circle()float getArea()return 3.14*radius*radius;private:float radius;int main()float radiu
23、s;coutradius;Circle p(radius);cout 半径为:圆的面积为:p.getArea()endl;return 0;运行结果:体会:类与函数的结合,就可以写出了了,只是我遇到的问题在运行上,开始运行不出来,回头再做时才运行出来结果的。3声明一个 Tree(树)类,有成员 ages(树龄),成员函数grow(int years)对 ages加上 years,age()显示 tree 对象的 ages的值。源程序:17#include using namespace std;class Tree int ages;public:Tree(int n=0);void grow
24、(int years);void age();Tree:Tree(int n)ages=n;void Tree:grow(int years)ages+=years;void Tree:age()cout这棵树的年龄为agesendl;int main()Tree t(20);t.age();t.grow(4);return 0;include using namespace std;class Tree int ages;public:Tree(int n=0);void grow(int years);void age();Tree:Tree(int n)ages=n;void Tree:
25、grow(int years)ages+=years;void Tree:age()cout这棵树的年龄为agesendl;int main()18 Tree t(20);t.age();t.grow(4);return 0;运行结果:实验五C+程序的结构一、实验目的1、观察程序运行中变量的作用域、生存期和可见性2、学习类的静态成员的使用。3、学习多文件结构在C+程序中的使用。二、实验内容1、运行下面的程序、观察变量x、y 的值,并分析执行过程。源程序:#include using namespace std;void fn1();int x=1,y=2;int main()cout Begi
26、n.endl;cout x=x endl;cout y=y endl;cout Evaluate x and y in main().endl;int x=10,y=20;cout x=x endl;cout y=y endl;cout Step into fn1().endl;fn1();cout Back in main endl;cout x=x endl;cout y=y endl;return 0;void fn1()int y=200;cout x=x endl;cout y=y endl;19 体会:函数处理,输入输出,也就这么多了。2、实现客户机(CLIENT)类。定义字符型静
27、态数据成员ServerName、保存其服务器名称:整型静态数据成员clientNum,记录巳定义的客户数量;定义静态函数ChangeServerName()改变服务器名称。在头文件client h 中定义类,在文件 client cpp 中实现,在文件test cpp中测试这个类,观察相应的成员变量取值的变化情况。#include using namespace std;class Client private:static char SeverName10;static int Clientnum;public:Client()coutClient 构造函数被调用endl;Clientnum
28、+;Client()coutClient 析构函数被调用endl;Clientnum-;static void ChangeServername()int i;cout 请输入服务器的名字:SeverName;20 void show();int Client:Clientnum=0;char Client:SeverName10=abcd;void Client:show()cout 该用户机的服务器端名字是:endl;coutSeverNameendl;cout 用户数量有:endl;coutClientnumendl;void main()Client:ChangeServername(
29、);Client a;a.show();Client b;b.show();体会:这一题对我来说有些难度,不知道很多东西的感觉,感觉树不熟悉,好多内容看了记住了,过段时间就忘了,然后就怎么也想不通,我觉得我们需要多多请教老师,而且对于老师的额讲解要倍加认真听解和强化练习等。实验六数组、指针与字符串一、实验目的1、学习使用数组。2、学习字符串数据的组织和处理3、学习标准C+库的使用。21 4、掌握指针的使用方法。二、实验内容1、编写并测试33 矩阵转置函数,使用数组保存3 3 矩阵。源程序:#include void move(int matrix33)int i,j,k;for(i=0;i3;
30、i+)for(j=0;ji;j+)k=matrixij;matrixij=matrixji;matrixji=k;void main()int i,j,data33;cout 输入矩形的元素 endl;for(i=0;i3;i+)for(j=0;j3;j+)cout第i+1 行第 j+1 dataij;move(data);cout 转置后的矩阵为:endl;for(i=0;i3;i+)for(j=0;j3;j+)cout dataij ;cout endl;22 体会:矩阵的转置,需要熟悉数组的灵活使用。2、编写函数int index(char*s,char*t),返回字符串t 在字符串s中
31、出现的最左边的位置。如果在 s中没有与t 匹配的子串,就返回-1。#include int index(char*s,char*t)int m=0,n=0;for(;*(s+m)!=0;m+)if(*t=*(s+m)return m+1;void main()char yh64,yk64;coutyh;cout 原字符串:yhendl;coutyk;cout 查找字符:ykendl;coutyk 在 yh 中左起第 index(yh,yk)个位置 endl;23 体会:我的运行软件自从坏过一次后老是运行不出来结果,总是找不出理由的说我的程序运行不出来,有点烦,所以好多程序都折腾很久,加强训练吧
32、。3、定义一个Employee 类,其中包括姓名、街道地址、城市和邮编等属性,以及 change_name()和 display()等函数。display()显示姓名、街道地址、城市和邮编等属性change_name()改变对象的姓名属性。实现并测试这个类。源程序:#include#include using namespace std;class Employee private:char name30;char street30;char city18;char zip6;public:Employee(char*n,char*str,char*ct,char*z);void change
33、_name(char*n);void display();void setName(char*_name);void Employee:setName(char*_name)strcpy(name,_name);Employee:Employee(char*n,char*str,char*ct,char*z)strcpy(name,n);strcpy(street,str);strcpy(city,ct);strcpy(zip,z);24 void Employee:change_name(char*n)strcpy(name,n);void Employee:display()cout na
34、me t street t;cout city t zip endl;int main()Employee e1(猪猪,湖东路 59 号,马鞍山,2000000);e1.display();coutendl;e1.setName(菲菲);e1.display();coutendl;return 0;运行结果:4、定义包含5 个元素的对象数组,每个元素都是Employee 类型的对象,实现并测试这个数组。源程序:#include#include using namespace std;class Employee private:char name30;char street30;char ci
35、ty18;char zip16;public:25 Employee(char*n,char*str,char*ct,char*z);void change_name(char*n);void display();void setName(char*_name);void Employee:setName(char*_name)strcpy(name,_name);Employee:Employee(char*n,char*str,char*ct,char*z)strcpy(name,n);strcpy(street,str);strcpy(city,ct);strcpy(zip,z);voi
36、d Employee:change_name(char*n)strcpy(name,n);void Employee:display()cout name t street t;cout city t zip endl;int main()Employee emp5=Employee(张三,湖东路 45 号,马鞍山,100000),Employee(王五,南京路 3 号,上海,400000),Employee(刘七,解放路 52 号,六安,400000),Employee(赵一,人民路 4 号,上合肥,100000),Employee(周八,湖南路 46 号,马鞍山,200000);for(i
37、nt i=0;i5;i+)empi.display();26 体会:本题需要注意空间以及字符类型的问题,可将原本字节数规定数值设大点,然后再用析构函数释放出之前申请的多余空间,与上一题相比,就是在原有基础上再加个FOR 循环即可。实验七继承与派生一、实验目的1、学习定义和使用类的继承关系,定义派生类;2、熟悉不同继承方式下对基类成员的访问控制;3、学习利用虚基类解决二义性问题。二、实验内容1定义一个基类Animal,有私有整型成员变量age,构造其派生类dog,在其成员函数 Setage(int n)中直接给age赋值,看看会有什么问题,把age改为公有成员变量,还会有问题吗?编程试试看。#i
38、nclude using namespace std;class Animal public:int age;public:Animal();Animal();class Dog:private Animal public:Dog();Dog();void SetAge(int n)age=n;int main()Dog a;27 a.SetAge(10);错误指示:error c2248:age:cannot access private member declared in class Animal error c2248:age:cannot access private member
39、declared in class Animal 以下为运行结果部分截图:把 age改为共有成员变量后重新编译即可。2定义一个基类BaseClass,有整型成员变量Number,构造其派生类DerivedClass,参考教材例7-5 和例 7-6 设计构造函数和析构函数,观察构造函数和析构函数的执行情况。#include using namespace std;class BaseClass public:BaseClass()cout 构造基类对象!endl;BaseClass()cout 析构基类对象!endl;class DerivedClass:public BaseClass pub
40、lic:DerivedClass()cout 构造派生类对象!endl;DerivedClass()cout 析构派生类对象!endl;int main()DerivedClass d;运行结果:28 3、声明一个基类Base,有两个公有成员函数fn1()、fn2(),私有派生出Derived 类,如何通过Derived 类的对象调用基类的函数fn1(),编程试试看。源程序:#include using namespace std;class Base public:int fn1()const return 1;int fn2()const return 2;class Derived:pr
41、ivate Base public:int fn1()return Base:fn1();int fn2()return Base:fn2();int main()Derived a;a.fn1();return 0;实验八多态性一、实验目的1掌握运算符重载的方法;2学习使用虚函数实现动态多态性。二、实验内容1、定义 Point 类,有坐标x、y 两个成员变量,对Point 类重载“+”(自增)、“-”(自减)运算符,实现对坐标值的改变。源程序:#include using namespace std;class Point private:int _x,_y;public:Point&ope
42、rator+();29 Point operator+(int);Point&operator-();Point operator-(int);Point()_x=_y=0;int x()return _x;int y()return _y;Point&Point:operator+()_x+;_y+;return*this;Point Point:operator+(int)Point temp=*this;+*this;return temp;Point&Point:operator-()_x-;_y-;return*this;Point Point:operator-(int)Point
43、 temp=*this;-*this;return temp;int main()Point A;cout A 的值为:A.x(),A.y()endl;A+;cout A 的值为:A.x(),A.y()endl;+A;cout A 的值为:A.x(),A.y()endl;A-;cout A 的值为:A.x(),A.y()endl;-A;cout A 的值为:A.x(),A.y()endl;30 运行结果:2、定义一个车(vehicle)基类,有只 Run、Stop 等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从 bicycle 和 motorcar 派生出摩托
44、车(motorcycle)类,它们都有Run、stop等成员函数。观察虚函数的作用。源程序:#include using namespace std;class vehicle private:int MaxSpeed;int Weight;public:vehicle()MaxSpeed=0;Weight=0;vehicle();virtual void Run()cout A vehicle is running!endl;virtual void Stop()cout A vehicle has stopped!endl;class bicycle:virtual public vehi
45、cle private:int Height;public:bicycle();bicycle();void Run()cout A bicycle is running!endl;void Stop()cout A bicycle has stopped!endl;class motorcar:virtual public vehicle private:int SeatNum;public:motorcar();motorcar();31 void Run()cout A motorcar is running!endl;void Stop()cout A motorcar has stopped!endl;class motorcycle:public bicycle,public motorcar public:motorcycle();motorcycle();void Run()cout A motorcycle is running!endl;void Stop()cout A motorcycle has stopped!Run();ptr=&b;ptr-Run();ptr=&c;ptr-Run();ptr=&d;ptr-Run();32 体会:必须好好看看这一章了,编写和编译都有很大难度,基础不牢固,扩充面不大,训练强度不够,作为专业课我深感愧疚,继续努力!
限制150内