




已阅读5页,还剩10页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
面向对象程序设计实验报告 姓名:唐靖琳 学号:200902220117 学校:桂林航天工业高等技术专科学校 指导老师:唐建清 实验2实验题目:用类定义,输入半径和高,输出圆柱体的底面积和体积实验程序代码:【circle.h】#includeusing namespace std;class circleprivate:float r,h;const double pi;public:circle(float x,float y);void print();【circle.cpp】#includecircle.hcircle:circle(float x,float y):pi(3.14)r=x;h=y;void circle:print()cout底面积为:r*r*piendl;cout体积为:r*r*pi*hendl;【main.cpp】#includeusing namespace std;#includecircle.hvoid main()circle a(1,2); a.print();实验结果:底面积为:3.14体积为:6.28实验三实验题目:用类实现计算两点之间的距离。(可以定义点类(Point),再定义一个类(Distance)描述两点之间的距离,其数据成员为两个点类对象,两点之间距离的计算可设计由构造函数来实现。)实验程序代码:【point.h】#includeusing namespace std;#ifndef point_h#define point_hclass pointprivate:int x1;int y1;public:point(int ,int );#endif【point.cpp】#include point.hpoint:point(int x,int y)x1=x;y1=y;【distance.h】#includeusing namespace std;#includepoint.h#include#ifndef distance_h#define distance_hnamespace meclass distanceprivate:point p1;point p2;public:distance(int x1,int x2,int y1,int y2);【distance.cpp】#includeusing namespace std;#include distance.h#include point.hme:distance:distance(int x1,int x2,int y1,int y2):p1(x1,y1),p2(x2,y2)cout”两点间的距离为:”sqrt(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);【main.cpp】#includeusing namespace std;#includedistance.h#include point.hvoid main()me:distance s(1,2,1,2);实验结果:两点间的距离为:1.41实验四实验题目:定义学生类(姓名,学号),生日类(年,月,日),利用友元类,输出某个学生的姓名,学号,及其生日。实验程序代码:【student.h】#includeusing namespace std;#include birthday.hclass studentprivate:char *name;int ID;public:student(char *s,int n);void print(birthday &a);【birthday.h】#includeusing namespace std;#ifndef bi#define biclass birthdayprivate:int year,mouth,day;friend class student;public:birthday(int x,int y,int z);void print();#endif【student.cpp】#include student.hstudent:student(char *s,int n)ID=n;name=new charstrlen(s)+1;strcpy(name,s);void student:print(birthday &b)cout姓名:nameendl;cout学号:IDendl;cout生日:b.yearb.mouthb.dayendl;【birthday.h】#include birthday.hbirthday:birthday(int x,int y,int z)year=x; mouth=y; day=z;【main.cpp】#includeiostreamusing namespace std;#includestudent.h#includebirthday.h void main()student a(tjl,117);birthday b(1990,9,1);a.print(b);实验结果:姓名:tjl学号:117生日:199091实验五实验题目: student(name,id,total),total为静态成员,在student中设置静态函数来处理total。在main()函数中显示name,id,total实验程序代码:【student.h】#includeusing namespace std;class studentprivate:char *name;int id;static int total;public:student(char *na,int id1);void print();static void print1();【total.cpp】#includeusing namespace std;#includestudent.h#includestring.hint student:total=0;student:student(char *na,int id1)id=id1;name=new charstrlen(na)+1;strcpy(name,na);+total;void student:print1()couttotal;void student:print()coutname:nameendl;coutid:idendl;【main.cpp】#includeusing namespace std;#includestudent.hvoid main()student a(tang,61);student b(guo,45);a.print();b.print();student:print1();实验结果:name:tangid:61name:guoid:45实验六实验题目:基类-学生类(姓名,学号),子类-大学生类(专业),继承方式:公有继承,在主函数中显示某个大学生的信息。实验程序代码:【student.h】#includeusing namespace std;#ifndef _st1#define _st1class studentprivate: char *name; int id;public: student();student(char *na,int id1);void print();#endif【student.cpp】#includeusing namespace std;#includestring.h#includestudent.hstudent:student()delete name;student:student(char *na,int id1)id=id1;name=new charstrlen(na)+1;strcpy(name,na);void student:print()coutname:nameendl;coutid:idendl;【cstudent.h】#includeusing namespace std;#includestudent.h#ifndef _cst#define _cstclass cstudent:public studentprivate: char *project;public: cstudent();cstudent(char *na,int id1,char *pro);void print1();#endif【cstudent.cpp】#includeusing namespace std;#includestring.h#includecstudent.h#includestudent.hcstudent:cstudent()delete project;cstudent:cstudent(char *na,int id1,char *pro):student(na,id1)project=new charstrlen(pro)+1;strcpy(project,pro);void cstudent:print1()student:print();coutproject:projectendl;【main.cpp】#includeusing namespace std;#includestring.h#includestudent.h#includecstudent.hmain()cstudent a(tang,17,computer);a.print1();实验结果:name:tangid:17project:conputer实验七实验题目:基类shape,派生类circle(半径R),tangcle(长a,宽b),它们都有求面积的函数area(),利用虚函数求两个派生类对象的面积实验程序代码:【shape.h】#includeiostreamusing namespace std;#ifndef _shape#define _shapeclass shapepublic:virtual void area();#endif【shape.cpp】#includeshape.hvoid shape:area()【circle.h】#includeshape.hclass circle:public shapeprivate:float r;const float pi;public:circle(float r1);void area(); ;【circle.cpp】#includecircle.hcircle:circle(float r1):pi(3.14)r=r1;void circle:area()coutpi*r*rendl;【tangcle.h】#includeiostream#includeshape.husing namespace std;class tangcle:public shapeprivate:float a,b;public:tangcle(float c,float d);void area();【tangcle.cpp】#includetangcle.htangcle:tangcle(float c, float d)a=c;b=d;void tangcle:area()couta*barea();p=&b;p-area();实验结果:12.5612实验八实验题目:重载减法运算符,使得两个复数可以相减实验程序代码:【complex.h】#include#ifndef _COMPLEX_H#define _COMPLEX_Hclass complexpublic: double a;double b;public:complex(double a,double b);void print();friend void operator -(complex x,complex y);#endif【complex.cpp】#include#includecomplex.h#includestring.hcomplex:complex(double a1,double b1)a=a1;b=b1;【main.cpp】#include#includecomplex.h#includestring.hvoid operator-(complex x,complex y)cout实部是:x.a-y.aendl;cout虚部是:x.b-y.bendl;void main()complex A1(1.0,2.0),A2(3.0,4.0);A1-A2;实验结果:实部是: -2虚部:-2实验九实验题目:将文件text1的内容拷贝到文件text2中实验程序代码:#inc
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【正版授权】 IEC 60730-2-13:2025 EXV EN Automatic electrical controls - Part 2-13: Particular requirements for humidity sensing controls
- 暑假培优练:传送带模型 -2025高一物理暑假专项提升(人教版)
- 新解读《GB 31144-2014木工机床安全 手动式摇臂锯》
- 视觉界面设计师专业知识考试题库与答案
- 社会科学研究方法 课件 第1-6章 导论-实验研究
- 老年人热水袋应用课件
- 浪潮继续教育方案
- CN120209125A 单克隆抗体与分泌单克隆抗体的杂交瘤细胞及应用
- 老年人安全防护规范课件
- 三角形及其性质(14个高频考点)原卷版
- 工程人员驻场服务方案
- 中小学校保安服务方案(技术方案)
- 医院医保新员工岗前培训
- 直播供货协议合同范本
- DB3411∕T 0039-2024 废包装桶回收再利用技术规范
- 2024年二次离婚起诉状范文
- 北师大版高中英语让学生自由飞翔
- T-CCSAS014-2022《化工企业承包商安全管理指南》
- 医学教育中的全科医学与专科医学的比较与协同
- 肠梗阻小讲课
- 《小儿支气管肺炎》课件
评论
0/150
提交评论