C++面向对象程序设计基础实验报告.doc_第1页
C++面向对象程序设计基础实验报告.doc_第2页
C++面向对象程序设计基础实验报告.doc_第3页
C++面向对象程序设计基础实验报告.doc_第4页
C++面向对象程序设计基础实验报告.doc_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

太原理工大学现代科技学院面向对象程序设计基础A 课程实验报告 实验名称 c+基础、类对象、继承与派生、多态性专业班级 计算机 学 号 姓 名 指导教师 王幸民 太原理工大学现代科技学院实验二C+基础一、 实验目的:(1)熟悉基本的输入输出方法;(2)掌握函数的定义、调用和声明方法,带默认形参的函数,重载函数;(3)熟悉编译预处理命令;(4)掌握常用算法的实现。二、 实验内容:(1) 编写重载函数MAX1可分别求取2个整数、3个整数、2个双精度数、3个双精度数的最大值。(2) 编程计算图形的面积。程序可计算圆形、长方形、正方形的面积,运行时先提求用户选择图形的类型,然后,再要求用户对圆形输入半径值,对长方形输入长与宽,对正方形输入边长,计算出面积的值后,在屏幕上显示出来。三、 实验代码:(1) #include using namespace std;int MAX1(int a,int b)return ab?a:b;int MAX1(int a,int b,int c)int max=a; if(bmax)max=b; if(cmax)max=c; return max;double MAX1(double a,double b)return ab?a:b;double MAX1(double a,double b,double c)double max=a; if(bmax)max=b; if(cmax)max=c; return max;int main() int i=3,j=5,k=8;double x=1.5,y=46.2,z=23.2; coutmax(i,j)=MAX1(i,j)endl;coutmax(i,j,k)= MAX1(i,j,k)endl; coutmax(x,y)= MAX1(x,y)endl;coutmax(x,y,z)=MAX1(x,y,z)endl;return 0;(2)#includeusing namespace std;int main() int x; float radius,length,width,sidelength,area; do cout计算图形的面积,请选择: 1.圆形2.长方形 3.正方形 0.退出 x; switch(x) case 1: coutradius; area=3.1415*radius*radius; break; case 2: coutlengthwidth; area=length*width; break; case 3: coutsidelength; area=sidelength*sidelength; break;case 0: cout结束计算,退出;exit(0); default:cout选择错,请重新选择:; if(x=1) cout半径:radius 圆面积:areaendl;else if(x=2) cout长:length 宽:width 长方形面积:areaendl;else if(x=3) cout边长:sidelength 正方形面积:areaendl; while(x!=1 |x!=2|x!=3);return 0;四、 实验结果(1)(2)实验三 类与对象一、 实验目的(1) 掌握类的概念;(2) 理解对象与类的关系,掌握对象的创建和使用;(3) 掌握构造函数、析构函数的概念以及使用方法;(4) 理解构造函数与析构函数的调用过程。二、 实验内容(1)定义一个矩形类Rectangle,矩形的左上角(Left,Top)与右下角坐标(Right,Bottom)定义为保护数据成员。用公有成员函数Diagonal()计算出矩形对角线的长度,公有成员函数Show()显示矩形左上角与右下角坐标及对角线长度。在主函数中用new运算符动态建立矩形对象r1,初值为(10,10,20,20)。然后调用Show()显示矩形左上角与右下角坐标及对角线长度。最后用delete运算符回收为矩形动态分配的存储空间。(2)定义一个复数类Complex,复数的实部Real与虚部Image定义为私有数据成员。用复数类定义复数对象c1、c2、c3,用默认构造函数将c1初始化为c1=20+40i,将c2初始化为c2=0+0i,用拷贝构造函数将c3初始化为c3=20+40i。用公有成员函数Dispaly()显示复数c1、c2与c3 的内容。三、 实验代码(1)#includeiostream#includeheadusing namespace std;class Rectangleprotected:float left_top_x;float left_top_y;float right_bottom_x;float right_bottom_y;public:float dia;Rectangle(float a,float b,float c,float d);float diagonal(Rectangle &c);/float a,float b,float c,float d);void show();Rectangle:Rectangle(float a,float b,float c,float d)left_top_x=a;left_top_y=b;right_bottom_x=c;right_bottom_y=d;dia=sqrt(left_top_x-right_bottom_x)*(left_top_x-right_bottom_x)+(left_top_y-right_bottom_y)*(left_top_y-right_bottom_y);void Rectangle:show()cout左上角坐标:left_top_x,left_top_yendl;cout右下角坐标:right_bottom_x,right_bottom_yendl;cout对角线长为:diashow();delete r1;return 0;(2)#include#includeusing namespace std;class Complexprivate:int real;int image;public:Complex(int x,int y);Complex(Complex &c);void display(Complex &c);Complex:Complex(int x,int y)real=x;image=y; Complex:Complex(Complex &c)real=c.real;image=c.image;void Complex:display(Complex &c)coutc.real+c.imageiendl;int main()Complex c1(20,40);Complex c2(0,0);Complex c3(c1);coutc1=;c1.display(c1);coutc2=;c2.display(c2);coutc3=;c3.display(c3);return 0;四、 运行结果(1)(2)实验五 继承与派生一、 实验目的(1)初步掌握派生类构造函数的定义与使用方法,理解构造函数的调用过程,及基类成员的初始化过程;(2)理解冲突、支配规则与赋值兼容性原则的概念。二、 实验内容(1)定义描述职工档案的类Archives,私有数据成员为职工号(No)、姓名(Name8)、性别(Sex)、年龄(Age)。成员函数有:构造函数、显示职工信息的函数Show()。再由职工档案类派生出职工工资类Laborage,在职工工资类Laborage中新增数据成员:应发工资(SSalary)、社保金(Security)、实发工资(Fsalary),其成员函数有:构造函数,计算实发工资的函数Count(),计算公式为:实发工资=应发工资社保金。显示职工档案及工资的函数Display()。在主函数中用Laborage类定义职工对象lab,并赋初始值(1001,”Cheng”,M,21,2000,100),然后显示职工档案与工资。(2)定义描述矩形的类Rectangle,其数据成员为矩形的中心坐标(X,Y)、长(Length)与宽(Width)。成员函数为计算矩形面积的函数Area()与构造函数。再定义描述圆的类Circle,其数据成员为圆的中心坐标(X,Y)与半径R,其成员函数为构造函数。再由矩形类与圆类多重派生出长方体类Cuboid,其数据成员为长方体的高(High)与体积(Volume)。成员函数为:构造函数,计算体积的函数Vol(),显示矩形坐标(X,Y)、长方体的长、宽、高与体积的函数Show()。主函数中用长方体类定义长方体对象cub,并赋初始值(10,10,10,20,30,30,10,10),最后显示长方体的矩形坐标(X,Y)与长方体的长、宽、高与体积。三、 实验代码(1)#includeiostream#includecstringusing namespace std;class archivespublic:char name8;int no;char sex;int age;public:archives:archives(int ano,char *aname,char asex,int aage)no=ano;strcpy(name,aname);sex=asex;age=aage;void show(archives &a);class laborage:public archivesprivate:int ssalary;int security;int fsalary;public:laborage(int lno,char *lname,char lsex,int lage,int lssalary,int lsecurity ):archives(lno,lname,lsex,lage)ssalary=lssalary;security=lsecurity;int count(laborage &b);void display(laborage &a);void archives:show(archives &a)char *p;int i;p=;cout工号:a.noendl姓名:;for(i=0;i8;i+)i;coutendl性别:a.sexendl;cout年龄:a.ageendl;int laborage:count(laborage &b)b.fsalary=b.ssalary-b.security;return b.fsalary;void laborage:display(laborage &a)cout工资:a.fsalaryendl;int main()laborage lab(1001,Cheng ,m,21,2000,100);/strcpylab.show(lab);lab.count(lab);lab.display(lab);getchar();return 0;(2)#include#includeusing namespace std;const double pi=3.14;class rectanglefloat rx,ry,length,width;public:rectangle(float x,float y,float len,float wid);float area();class circlefloat cx,cy,r;public:circle(float cx,float cy,float r);class cuboid:public rectangle,public circleprivate:float high,volume;public:cuboid(float x1,float y1,float len,float wid,float x,float y,float r,float h):rectangle(x1,y1,len,wid),circle(x,y,r)high=h;double vol(cuboid &a);void show(cuboid &a);rectangle:rectangle(float x,float y,float len,float wid)rx=x;ry=y;length=len;width=wid;float rectangle:area()return rx*ry;/*/circle:circle(float x,float y,float cr)cx=x;cy=y;r=cr;/*/double cuboid:vol(cuboid &a)a.volume=a.length*a.width*a.high;return a.volume;void cuboid:show(cuboid &a)cout长方体的endl;cout矩形坐标:(a.rx,a.ry)endl;cout长:a.lengthendl;cout宽:a.widthendl;cout高:a.highendl;cout体积:a.volumeendl;#include#includehead.husing namespace std;int main()cuboid cub(10,10,10,20,30,30,10,10);cub.vol(cub);cub.show(cub);getchar();return 0;四、 运行结果(1) (2) 实验六 多态性一、 实验目的(1)掌握C+中运算符重载的机制和运算符重载的方式;(2)理解类型转换的必要性,掌握类型转换的使用方法;(3)理解多态性,掌握虚函数的设计方法;(4)学习使用Visual Studio调试虚函数。二、 实验内容(1)编写一个抽象类SHAPE,在此基础上派生出Rectangle和Circle,二者都有GetArea()函数计算对象的面积,计算周长的函数GetPerim();完善类的功能与结构。(2)声明一个车(Vehicle)基类,有Run、Stop等成员函数,由此派生出自行车(Bicycle)类、汽车(Motorcar)类,从(Bicycle)和(Motorcar)派生出摩托车(Motorcycle)类,它们都有Run、Stop等成员函数。利用虚函数解决问题。(3)编写一个程序,用于计算正方形、长方形、直角三角形和圆的总面积。三、 实验代码(1)#include using namespace std;class SHAPEpublic: SHAPE() SHAPE() virtual float GetArea()=0; virtual float GetPerim()=0; ;class Rectangle:public SHAPE float itsLength; float itsWidth;public:Rectangle( float length, float width)itsLength=length;itsWidth=width; float Getl()return itsLength; float Getw()return itsWidth; float GetArea()return itsLength*itsWidth; float GetPerim()return 2*(itsLength+itsWidth);class Circle:public SHAPE float itsRadius; public:Circle( float radius)itsRadius=radius; float Getr()return itsRadius; float GetArea()return itsRadius*itsRadius*3.14159; float GetPerim()return 2*itsRadius*3.14159;#include using namespace std;int main() Rectangle A(4,5);Circle B(4); cout长方形的长:A.Getl() 宽:A.Getw() 面积: A.GetArea() 周长:A.GetPerim()endl;cout圆的半径:B.Getr() 面积: B.GetArea() 周长:B.GetPerim()endl;return 0;(2)#include using namespace std;class Vehicle protected: int wheel,weight;public: Vehicle(int i,int j): wheel(i), weight(j) virtual int Getwheel()return wheel ; virtual int Getweight()return weight ; virtual void Run()=0; virtual void Stop()=0; ;class Bicycle:virtual public Vehicle public:Bicycle(int x,int y) : Vehicle(x,y) int Getwheel()return Vehicle: Getwheel(); int Getweight()return Vehicle: Getweight(); void Run()cout Bicycle :run轮子数: Getwheel() ; void Stop()cout Bicycle :stop 重量: Getweight()endl;class Motorcar:virtual public Vehicle public:Motorcar(int x,int y):Vehicle(x,y) int Getwheel()return Vehicle: Getwheel (); int Getweight()return Vehicle: Getweight ();void Run()cout Motorcar :run轮子数: Getwheel() ; void Stop()cout Motorcar :stop 重量: Getweight()endl;class Motorcycle:public Bicycle,public Motorcar public:Motorcycle (int x,int y):Vehicle(x,y),Bicycle(x,y),Motorcar(x,y) int Getwheel()return Vehicle: Getwheel(); int Getweight()return Vehicle: Getweight();void Run()cout Motorcycle :run轮子数: Getwheel() ; void Stop()cout Motorcycle :stop 重量: Getweight() Run(); p- Stop (); p=&b; p- Run(); p- Stop ();p=&c; p- Run(); p- Stop ();return 0;(3)#includeusing namespace std;const double pi=3.1416;class sq

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论