1、 do sum+=i; i+; while(i=10); coutsum=sumendl;(2)#include1计算圆面积endl x; double area; while(x!=4) if(x=1) double r;半径:r; area=3.14*r*r;面积为: areab; area=a*b;面积为 else if(x=3) double a;边长:a; area=a*a; else输入有误,请重新输入!程序代码: return 0;五、实验数据记录和处理运行结果:(1)(2)6、实验结果与分析(1)实验应用循环语句,实现累加运算,输出结构。(2)实验通过调用面积公式输出相应的结果
2、。七、讨论、心得 通过本次实验,我学会了简单应用C+程序,学会了运算符与表达式的应用,对VISUAL C+开发环境也有了一定了解。实验二 类与对象程序设计(1)掌握类的定义和使用、成员函数的定义和使用;(2)掌握类的私有成员、公有成员和保护成员的区别;(3)掌握对象的定义、类成员的访问;(4)掌握类中构造函数与析构函数的应用。(5)掌握重载函数的定义和使用。(1)设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角的两个点的坐标,能计算矩形的面积。要求定义构造函数与析构函数。(2)编写重载函数MAX1可分别求取2个整数、3个整数、2个双精度数、3个双精度数的最大值。 源程
3、序:(1)#include using namespace std; class Rectangle public: Rectangle (int top, int left, int bottom, int right); Rectangle () int GetTop() const return itsTop; int GetLeft() const return itsLeft; int GetBottom() const return itsBottom; int GetRight() const return itsRight; void SetTop(int top) itsTo
4、p = top; void SetLeft (int left) itsLeft = left; void SetBottom (int bottom) itsBottom = bottom; void SetRight (int right) itsRight = right; int GetArea() const; private: int itsTop; int itsLeft; int itsBottom; int itsRight; ; Rectangle:Rectangle(int top, int left, int bottom, int right) itsTop = to
5、p; itsLeft = left; itsBottom = bottom; itsRight = right; int Rectangle:GetArea() const int Width = itsRight-itsLeft; int Height = itsTop - itsBottom; return (Width * Height); int main() Rectangle MyRectangle (100, 20, 50, 80 ); int Area = MyRectangle.GetArea(); cout Area: Area b?a:b);int max(int a,i
6、nt b,int c) return max(a,max(b,c);double max(double a,double b)double max(double a,double b,double c) int a,b,c; float d,e,f;请输入三个整数c;请输入三个双精度数d;e;f;a,b中较大的是:max(a,b)d,e中较大的是:max(d,e)最大的整数是:max(a,b,c)最大的双精度数是:max(d,e,f)六、实验结果与分析(1)对于构造函数、析构函数的次序问题,先构造的后析构。(2)通过调用重载函数输出所需结果。 通过本次实验,我明确了构造函数和析构函数的应用,也
7、对函数重载有了更深刻的理解,掌握了类的私有成员、公有成员和保护成员的区别,对编写程序的整体思维方法是也有了更清晰的理解。实验三 继承与派生(1)掌握类的继承和派生过程;(2)掌握派生类的定义与使用;(3)掌握派生类的构造函数与析构函数的应用及调用顺序;(4)学习使用VISUAL C+调试类的继承关系的应用。(1)声明一个基类BaseClass,有整数类型成员变量Number,构造基派生类DerivedClass,实现其构造函数和析构函数,完善类的功能与结构。(2)声明一个基类SHAPE,在此基础上派生出Rectangle和Circle,二者都有GetArea()函数计算对象的面积。使用Rect
8、angle类创建一个派生类Square;完善类的功能与结构。#include class BaseClassprivate: int Number;public: BaseClass()BaseClass Construction! BaseClass()BaseClass Deconstruction!;class DerivedClass:public BaseClass DerivedClass()Derivedlass Construction! DerivedClass()DerivedClass Deconstruction! DerivedClass d;const double
9、 PI=3.14;class Shape Shape(double x0=0.0,double y0=0.0); void set(double x0,double y0); double getX(); double getY(); virtual double getArea()=0; Shape(); double x; double y;Shape:Shape(double x0,double y0) x=x0; y=y0;void Shape:set(double x0,double y0)double Shape:getX() return x;getY() return y;Sh
10、ape()class Rectangle:public Shape Rectangle(double x0=0.0,double y0=0.0,double w=0.0,double h=0.0); void set(double x0,double y0,double w,double h); double getWidth(); double getHeight(); double getArea(); Rectangle(); double width; double height;Rectangle:Rectangle(double x0,double y0,double w,doub
11、le h):Shape(x0,y0) width=w; height=h;void Rectangle:set(double x0,double y0,double w,double h) Shape:set(x0,y0);double Rectangle:getWidth() return width;getHeight() return height;getArea() return width*height;Rectangle()class Circle: Circle(double x0=0.0,double y0=0.0,double r=0.0); void set(double
12、x0,double y0,double r); double getRadius(); Circle(); double radius;Circle:Circle(double x0,double y0,double r): radius=r;void Circle:set(double x0,double y0,double r)double Circle:getRadius() return radius; return PI*radius*radius;Circle()class Square:public Rectangle Square(double x0=0.0,double y0
13、=0.0,double len=0.0); void set(double x0,double y0,double len); double getLen();Square:Square(double x0,double y0,double len):Rectangle(x0,y0,len,len)void Square:set(double x0,double y0,double len)set(x0,y0,len,len);double Square:getLen() return Rectangle:getHeight(); Rectangle rectl(1,2,4,5);矩形面积为:
14、rectl.getArea() Square squa(1,1,4);正方形面积为:squa.getArea() Circle cir(5,6,2);圆形的面积为:cir.getArea()(1)构造派生类,实现类的构造函数和析构函数。(2)创建派生类,并对其功能、结构予以完善。 通过本次实验,我了解了类的继承和派生过程,明确了类的构造函数和析构函数的应用,以及调用顺序,现已基本可以编写正确的程序了。实验四 多态性(1)掌握抽象类的定义与使用;(2)掌握虚函数的定义与使用;(3)掌握虚函数实现动态多态性;(4)学习使用VISUAL C+调试虚函数。(1)编写一个抽象类SHAPE,在此基础上派生
15、出Rectangle和Circle,二者都有GetArea()函数计算对象的面积。计算周长的函数GetPerim();(2)声明一个车(Vehicle)基类,有Run、Stop等成员函数,由此派生出自行车(Bicycle)类、汽车(Motorcar)类,从(Bicycle)和(Motorcar)派生出摩托车(Motorcycle)类,它们都有Run、Stop等成员函数。利用虚函数解决问题。(1)#include#define PI 3.1415926;class Shape virtual double GetArea() = 0; virtual double GetPerim() = 0;
16、class Rectangle : public Shape Rectangle(double aa, double bb) a=aa; b=bb;长a宽b virtual double GetArea() return a * b; virtual double GetPerim() return 2*( a + b ); double b;class Circle : Circle(double rr) r=rr;半径r length width; Rectangle rect(length, width);面积是: rect.GetArea() endl周长是:rect.GetPerim
17、() double rr;输入半径: rr; Circle cir(rr);cir.GetArea() cir.GetPerim()#includeclass Vehiclevirtual void Run()coutVehicle:Run callednvirtual void Stop()coutStop callednclass motorcar:public Vehicle void Run()coutmotorcar:class bicycle :bicycle: void Stop()coutRun();Stop();pclass=&bi;mo;(1)通过抽象类派生出的矩形、圆形,计算周长和面积。(2)利用虚函数调用派生类的成员。 通过本次实验,我对抽象类有了基本了解,也掌握了虚函数的定义、应用、以及多态性,对VISUAL C+的使用更加熟练了。
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2