ImageVerifierCode 换一换
格式:DOCX , 页数:20 ,大小:82.73KB ,
资源ID:9787699      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/9787699.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(《C++面向对象程序设计》实验报告.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

《C++面向对象程序设计》实验报告.docx

1、C+面向对象程序设计实验报告C+面向对象程序设计实验报告实验序号:2 实验项目名称:类和对象学号姓名专业实验地点指导教师实验时间一、实验目的及要求(1)理解类和对象的概念;(2)了解C+在非面向对象方面对C功能的扩充与增强。(3)初步掌握使用类和对象编制C+程序。(4)掌握对象数组、对象指针和string类的使用方法。(5)掌握使用对象、对象指针和对象引用作为函数参数的方法。(6)掌握类对象作为成员的使用方法。(7)掌握静态数据成员和静态成员函数的使用方法。(8)理解友元的概念和掌握友元的使用方法。二、实验设备(环境)及要求 Micorsoft Visual C+ 6.0三、实验内容与步骤(题

2、目、算法和结果描述)1、输入下列程序。#includeusing namespace std;class Coordinatepublic: Coordinate(int x1,int y1) x=x1; y=y1; Coordinate(Coordinate &p); Coordinate() coutDestructor is calledn; int getx() return x; int gety() return y;private: int x,y;Coordinate:Coordinate(Coordinate &p) x=p.x; y=p.y; coutCopy-initia

3、lization Constructour is calledn;int main() Coordinate p1(3,4); Coordinate p2(p1); Coordinate p3=p2; coutp3=(p3.getx(),p3.gety()n; return 0;(1)写出程序的运行结果。【运行结果截图】:(2)将Coordinator类中带有两个参数的构造函数进行修改,在函数体内增添下列语句: cout”constructor is called.n”; 【运行结果截图】:【运行结果分析】:带有两个参数的构造函数在主函数中只被调用了一次,其他的都是调用使用对象引用作为函数参数

4、的构造函数。(3)按下列要求进行调试:在主函数体内,添加下列语句:Coordinator p4;Coordinator p5(2);调试程序时会出现什么错误?为什么?如何对已有的构造函数进行适当修改?【运行结果截图】:【解释】:把带有两个参数的构造函数改为带有默认参数的构造函数,那么后添加的两条语句编译的时候就不会出错了。(4)经过以上第(2)步和第(3)步的修改后,结合运行结果分析:创建不同的对象时会调用不同的构造函数。【运行结果分析】:p1是调用有两个参数的构造函数,p2和p3都是p1的拷贝,但他们调用的是使用对象引用作为函数参数的构造函数,p4和p5都是调用两个参数的构造函数,p4由于没

5、有给出实参,所以使用默认参数,即x=1,y1;而p5给出一个实参,所以x=2,y=1.2、设计一个4*4魔方程序,让魔方的各行值的和等于各列值的和,并且等于两对角线的和,例如以下魔方,各行各列及两对角线值的和都是64.31 3 5 259 21 19 1517 13 11 237 27 29 1【提示】:求4*4的魔方的一般步骤如下:(1)设置初始魔方的起始值和相邻元素之间的差值。例如上述魔方的初始魔方的起始值(first)和相邻元素之间的差值(step)分别为:first=1; step=2;(2)设置初始魔方元素的值,例如上述魔方的初始魔方为:1 3 5 7 9 11 13 15 17 1

6、9 21 23 25 27 29 31(3)生成最终魔方。方法如下:1)求最大元素值与最小元素值的和sum,该实例的sum是:1+31=322)用32减去初始魔方所有对角线上元素的值,然后将结果放在原来的位置,这样就可以求得最终魔方。本题的魔法类magic的参考框架如下:class magicpublic: void getdata(); void getfirstmagic(); void generatemagic(); void printmagic();private: int m44; int step; int first; int sum;【运行结果截图】:3、设计一个用来表示直

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

8、; /成员函数,取y坐标值 double distancee(Location &); /成员函数,求给定两点之间的距离 friend double distancee(Location &,Location &); /友元函数,求给定两点之间的距离private: double x,y;【运行结果截图】:4、声明一个Student类,在该类中包括一个暑假成员score(分数)、两个静态数据成员total_score(总分)和count(学生人数);还包括一个成员函数account()用于设置分数、累计学生的成绩之和、累计学生人数,一个静态成员函数sum()用于返回学生的成绩之和,另一个静态成

9、员函数average()用于求全部成绩的平均值。在main函数中,输入某班同学的成绩,并调用上述函数求出全班同学的成绩之和和平均分。【Student类的框架】class Studentpublic: void account(); static float sum(); static float average();private: float score; static float total_score; static int count;【运行结果截图】:5、使用C+的string类,将5个字符串按逆转后的顺序显示出来。例如,逆转前5个字符的字符串是: Germany Japan Ame

10、rica Britain France按逆转后的顺序输出字符串是:France Britain America Japan Germany【运行结果截图】:6、定义一个圆类(Circle),属性为半径(radius)和圆周长、面积,操作为输入半径并计算周长、面积,输出半径、周长和面积。要求定义构造函数(以半径为参数,缺省值为0,圆周长和面积在构造函数中生成)和复制构造函数。【运行结果截图】:7、教材P134 习题3.33和3.34【运行结果截图】:四、分析与讨论(记录实验过程中出现的主要问题和心得体会)五、教师评语签名:日期:成绩附:程序源代码1、#includeusing namespace

11、 std;class Coordinatepublic: Coordinate(int x1=1,int y1=1) x=x1; y=y1; coutconstructor is called.n; Coordinate(Coordinate &p); Coordinate() coutDestructor is calledn; int getx() return x; int gety() return y;private: int x,y;Coordinate:Coordinate(Coordinate &p) x=p.x; y=p.y; coutCopy-initialization

12、Constructour is calledn;int main() Coordinate p1(3,4); Coordinate p2(p1); Coordinate p3=p2; coutp3=(p3.getx(),p3.gety()n;Coordinate p4;Coordinate p5(2); return 0;2、#includeusing namespace std;class magicpublic: void getdata(); void getfirstmagic(); void generatemagic(); void printmagic();private: in

13、t m44; int step; int first; int sum; void magic:getdata() coutfirststep;void magic:getfirstmagic() int i,j,t; t=first; for(i=0;i4;i+) for(j=0;j4;j+) mij=first; first+=step; sum=t+m33;void magic:generatemagic() int i,j; for(i=0;i4;i+) mii=sum-mii; for(i=0,j=3;i=0;i+,j-) mij=sum-mij;void magic:printma

14、gic() int i,j; for(i=0;i4;i+) for(j=0;j4;j+) coutmijt; coutendl; int main() magic A; A.getdata(); A.getfirstmagic(); A.generatemagic(); A.printmagic(); return 0;3、#include#includeclass Locationpublic: Location(double,double); double getx(); double gety(); double distance(Location &); friend double d

15、istance(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:distance(Location &t) double dx=x-t.x; double dy=y-t.y; return sqrt(dx*dx+dy*dy);double distance(Location &a , Location

16、&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(B); coutDistance1=dendl; coutDistance2=distance(A,B)endl; return 0;4、#includeusing namespace std;class Studentpubli

17、c: 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; return k;float Student:average() float t=total_scor

18、e/(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; coutthe total score is : a.sum()endl; coutthe average

19、score is : a.average()endl; return 0;5、#include#includeusing namespace std;int main() int i; string ch5; for(i=0;ichi; for(i=4;i=0;i-) coutchi ; coutendlendl; return 0;6、#includeusing namespace std;class Circleprivate: double r,area,circumference;public: Circle(double); Circle(const Circle &); doubl

20、e R(); double Area(); double Circumference();Circle:Circle(double t) r=t;Circle:Circle(const Circle & p) r=2*p.r;double Circle:R() return r;double Circle:Area() return 3.14159265*r*r;double Circle:Circumference() return 2*3.14159265*r;int main() Circle p1(1.0); coutthe circles R is : p1.R()endl; cou

21、tthe circles Area is : p1.Area()endl; coutthe circels circumference is : p1.Circumference()endl; Circle p2(p1); coutthe circles R is : p2.R()endl; coutthe circles Area is : p2.Area()endl; coutthe circels circumference is : p2.Circumference()endl; Circle p3(3.05); coutthe circles R is : p3.R()endl; c

22、outthe circles Area is : p3.Area()endl; coutthe circels circumference is : p3.Circumference()endl; return 0;7、3.33#includeusing namespace std;class bookprivate: int qu; int price;public: book(int); int getx(); int ride();book:book(int i) qu=i; price=10*qu;int book:ride() return price*qu; int main()

23、book a5=1,2,3,4,5; for(int i=0;i5;i+) coutai.ride() ; coutendl; return 0;3.34#includeusing namespace std;class bookprivate: int qu; int price;public: book(int); int getx(); int ride();book:book(int i) qu=i; price=10*qu;int book:ride() return price*qu; int main() book a5=1,2,3,4,5; book *p; p=&a4; for(int i=4;i=0;i-) coutride() ; p-; coutendl; return 0;

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1