C++面向对象程序设计实验报告.doc_第1页
C++面向对象程序设计实验报告.doc_第2页
C++面向对象程序设计实验报告.doc_第3页
C++面向对象程序设计实验报告.doc_第4页
C++面向对象程序设计实验报告.doc_第5页
已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论