C++实验报告模板 太原理工大学软件学院.docx
- 文档编号:6849067
- 上传时间:2023-05-10
- 格式:DOCX
- 页数:20
- 大小:134.76KB
C++实验报告模板 太原理工大学软件学院.docx
《C++实验报告模板 太原理工大学软件学院.docx》由会员分享,可在线阅读,更多相关《C++实验报告模板 太原理工大学软件学院.docx(20页珍藏版)》请在冰点文库上搜索。
C++实验报告模板太原理工大学软件学院
本科实验报告
课程名称:
C++面向对象程序设计
实验项目:
C++简单程序设计、类与对象程序设计、
继承与派生、多态性
实验地点:
虎峪校区致远楼B403
专业班级:
软件工程班学号:
学生姓名:
指导教师:
王晓明
2013年1月10日
实验一C++简单程序设计
一、实验目的和要求
(1)学习编写简单的C++程序;
(2)学习运算符与表达式的应用;
(3)练习使用简单的输入输出;
(4)学习使用VISUALC++开发环境。
二、实验内容
(1)do-while语句编程,求自然数1-10的和;并用for语句改写此程序。
(2)编程计算图形的面积。
程序可计算圆形、长方形、正方形的面积,运行时先提求用户选择图形的类型,然后,再要求用户对圆形输入半径值,对长方形输入长与宽,对正方形输入边长,计算出面积的值后,在屏幕上显示出来。
三、主要仪器设备
计算机,MicrosoftVisualStudio2010
四、操作方法与实验步骤
源程序:
(1)#include"iostream"
usingnamespacestd;
intmain()
{
inti
(1),sum(0);
do{
sum+=i;
i++;
}while(i<=10);
cout<<"sum="< } (2)#include usingnamespacestd; intmain() { cout<<"1计算圆面积"< <<"2计算矩形面积"< <<"3计算正方形面积"< <<"4退出"< <<"请选择相应程序代码: "; intx; cin>>x; doublearea; while(x! =4) { if(x==1) { doubler; cout<<"半径: "< cin>>r; area=3.14*r*r; cout<<"面积为: "< } elseif(x==2) { doublea,b; cout<<"长和宽: "; cin>>a>>b; area=a*b; cout<<"面积为"< } elseif(x==3) { doublea; cout<<"边长: "; cin>>a; area=a*a; cout<<"面积为"< } else{ cout<<"输入有误,请重新输入! "< cout<<"程序代码: "; cin>>x; } } return0; } 五、实验数据记录和处理 运行结果: (1) (2) 6、实验结果与分析 (1)实验应用循环语句,实现累加运算,输出结构。 (2)实验通过调用面积公式输出相应的结果。 七、讨论、心得 通过本次实验,我学会了简单应用C++程序,学会了运算符与表达式的应用,对VISUALC++开发环境也有了一定了解。 实验二类与对象程序设计 一、实验目的和要求 (1)掌握类的定义和使用、成员函数的定义和使用; (2)掌握类的私有成员、公有成员和保护成员的区别; (3)掌握对象的定义、类成员的访问; (4)掌握类中构造函数与析构函数的应用。 (5)掌握重载函数的定义和使用。 二、实验内容 (1)设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角的两个点的坐标,能计算矩形的面积。 要求定义构造函数与析构函数。 (2)编写重载函数MAX1可分别求取2个整数、3个整数、2个双精度数、3个双精度数的最大值。 三、主要仪器设备 计算机,MicrosoftVisualStudio2010 四、操作方法与实验步骤 源程序: (1)#include usingnamespacestd; classRectangle { public: Rectangle(inttop,intleft,intbottom,intright); ~Rectangle(){} intGetTop()const{returnitsTop;} intGetLeft()const{returnitsLeft;} intGetBottom()const{returnitsBottom;} intGetRight()const{returnitsRight;} voidSetTop(inttop){itsTop=top;} voidSetLeft(intleft){itsLeft=left;} voidSetBottom(intbottom){itsBottom=bottom;} voidSetRight(intright){itsRight=right;} intGetArea()const; private: intitsTop; intitsLeft; intitsBottom; intitsRight; }; Rectangle: : Rectangle(inttop,intleft,intbottom,intright) { itsTop=top; itsLeft=left; itsBottom=bottom; itsRight=right; } intRectangle: : GetArea()const { intWidth=itsRight-itsLeft; intHeight=itsTop-itsBottom; return(Width*Height); } intmain() { RectangleMyRectangle(100,20,50,80); intArea=MyRectangle.GetArea(); cout<<"Area: "< return0; } (2)#include"iostream" usingnamespacestd; intmax(inta,intb) { return(a>b? a: b); } intmax(inta,intb,intc) { returnmax(a,max(b,c)); } doublemax(doublea,doubleb) { return(a>b? a: b); } doublemax(doublea,doubleb,doublec) { returnmax(a,max(b,c)); } intmain() { inta,b,c; floatd,e,f; cout<<"请输入三个整数"< cin>>a; cin>>b; cin>>c; cout<<"请输入三个双精度数"< cin>>d; cin>>e; cin>>f; cout<<"a,b中较大的是: "< cout<<"d,e中较大的是: "< cout<<"最大的整数是: "< cout<<"最大的双精度数是: "< return0; } 五、实验数据记录和处理 运行结果: (1) (2) 六、实验结果与分析 (1)对于构造函数、析构函数的次序问题,先构造的后析构。 (2)通过调用重载函数输出所需结果。 七、讨论、心得 通过本次实验,我明确了构造函数和析构函数的应用,也对函数重载有了更深刻的理解,掌握了类的私有成员、公有成员和保护成员的区别,对编写程序的整体思维方法是也有了更清晰的理解。 实验三继承与派生 一、实验目的和要求 (1)掌握类的继承和派生过程; (2)掌握派生类的定义与使用; (3)掌握派生类的构造函数与析构函数的应用及调用顺序; (4)学习使用VISUALC++调试类的继承关系的应用。 二、实验内容 (1)声明一个基类BaseClass,有整数类型成员变量Number,构造基派生类DerivedClass,实现其构造函数和析构函数,完善类的功能与结构。 (2)声明一个基类SHAPE,在此基础上派生出Rectangle和Circle,二者都有GetArea()函数计算对象的面积。 使用Rectangle类创建一个派生类Square;完善类的功能与结构。 三、主要仪器设备 计算机,MicrosoftVisualStudio2010 四、操作方法与实验步骤 (1) #include"iostream" usingnamespacestd; classBaseClass { private: intNumber; public: BaseClass() { cout<<"BaseClassConstruction! "< } ~BaseClass() { cout<<"BaseClassDeconstruction! "< } }; classDerivedClass: publicBaseClass { public: DerivedClass() { cout<<"DerivedlassConstruction! "< } ~DerivedClass() { cout<<"DerivedClassDeconstruction! "< } }; intmain() { DerivedClassd; return0; } (2)#include"iostream" usingnamespacestd; constdoublePI=3.14; classShape { public: Shape(doublex0=0.0,doubley0=0.0); voidset(doublex0,doubley0); doublegetX(); doublegetY(); virtualdoublegetArea()=0; ~Shape(); private: doublex; doubley; }; Shape: : Shape(doublex0,doubley0) { x=x0; y=y0; } voidShape: : set(doublex0,doubley0) { x=x0; y=y0; } doubleShape: : getX() { returnx; } doubleShape: : getY() { returny; } Shape: : ~Shape(){} classRectangle: publicShape { public: Rectangle(doublex0=0.0,doubley0=0.0,doublew=0.0,doubleh=0.0); voidset(doublex0,doubley0,doublew,doubleh); doublegetWidth(); doublegetHeight(); doublegetArea(); ~Rectangle(); private: doublewidth; doubleheight; }; Rectangle: : Rectangle(doublex0,doubley0,doublew,doubleh): Shape(x0,y0) { width=w; height=h; } voidRectangle: : set(doublex0,doubley0,doublew,doubleh) { Shape: : set(x0,y0); width=w; height=h; } doubleRectangle: : getWidth() { returnwidth; } doubleRectangle: : getHeight() { returnheight; } doubleRectangle: : getArea() { returnwidth*height; } Rectangle: : ~Rectangle(){} classCircle: publicShape { public: Circle(doublex0=0.0,doubley0=0.0,doubler=0.0); voidset(doublex0,doubley0,doubler); doublegetRadius(); doublegetArea(); ~Circle(); private: doubleradius; }; Circle: : Circle(doublex0,doubley0,doubler): Shape(x0,y0) { radius=r; } voidCircle: : set(doublex0,doubley0,doubler) { Shape: : set(x0,y0); radius=r; } doubleCircle: : getRadius() { returnradius; } doubleCircle: : getArea() { returnPI*radius*radius; } Circle: : ~Circle(){} classSquare: publicRectangle { public: Square(doublex0=0.0,doubley0=0.0,doublelen=0.0); voidset(doublex0,doubley0,doublelen); doublegetLen(); }; Square: : Square(doublex0,doubley0,doublelen): Rectangle(x0,y0,len,len) {} voidSquare: : set(doublex0,doubley0,doublelen) { Rectangle: : set(x0,y0,len,len); } doubleSquare: : getLen() { returnRectangle: : getHeight(); } intmain() { Rectanglerectl(1,2,4,5); cout<<"矩形面积为: "< cout< Squaresqua(1,1,4); cout<<"正方形面积为: "< cout< Circlecir(5,6,2); cout<<"圆形的面积为: "< cout< return0; } 五、实验数据记录和处理 运行结果: (1) (2) 六、实验结果与分析 (1)构造派生类,实现类的构造函数和析构函数。 (2)创建派生类,并对其功能、结构予以完善。 七、讨论、心得 通过本次实验,我了解了类的继承和派生过程,明确了类的构造函数和析构函数的应用,以及调用顺序,现已基本可以编写正确的程序了。 实验四多态性 一、实验目的和要求 (1)掌握抽象类的定义与使用; (2)掌握虚函数的定义与使用; (3)掌握虚函数实现动态多态性; (4)学习使用VISUALC++调试虚函数。 二、实验内容 (1)编写一个抽象类SHAPE,在此基础上派生出Rectangle和Circle,二者都有GetArea()函数计算对象的面积。 计算周长的函数GetPerim();完善类的功能与结构。 (2)声明一个车(Vehicle)基类,有Run、Stop等成员函数,由此派生出自行车(Bicycle)类、汽车(Motorcar)类,从(Bicycle)和(Motorcar)派生出摩托车(Motorcycle)类,它们都有Run、Stop等成员函数。 利用虚函数解决问题。 三、主要仪器设备 计算机,MicrosoftVisualStudio2010 四、操作方法与实验步骤 (1)#include #definePI3.1415926; usingnamespacestd; classShape { public: virtualdoubleGetArea()=0; virtualdoubleGetPerim()=0; }; classRectangle: publicShape { public: Rectangle(doubleaa,doublebb) { a=aa; b=bb;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+实验报告模板 太原理工大学软件学院 C+ 实验 报告 模板 太原 理工大学 软件 学院