《C++面向对象程序设计》由会员分享,可在线阅读,更多相关《C++面向对象程序设计(11页珍藏版)》请在金锄头文库上搜索。
1、2011-2012学年 第一学期面向对象程序设计课 程 设 计专 业: 班 级: 姓 名: 2011年11月29日1设计目的 创建一个点类Point,其中有X、Y 轴坐标作为数据成员,同时拥有构造函数、设置和访问X、Y 轴坐标的成员函数、求面积的虚函数Area、求体积的虚函数Volume。以Point类为直接或间接基类派生出圆类Circle、矩形类Rectangle、正方形类Square、圆柱类Cylinder、长方体类Cuboid、正方体类Cube,自行定义这些类的继承关系、类中的数据成员和其他内容,并改写Area 和Volume 函数正确求出各几何对象的面积和体积。编写测试代码,创建上述类
2、的对象,并通过指针或引用来使用Area 和Volume 函数计算面积或体积。2 设计内容与分析 #include为载入头文件,使用命名空间std。创建一个点类Point,定义PI为3.14。基类Point拥有坐标x,坐标y为数据成员函数,设置Get函数访问x,y的值。在基类Point中将GetArea()、GetvVolume()声明为虚函数。以Point类为直接基类派生出Circle圆类(公有继承)、Rectangle矩形类(私有继承)、正方形类Square(公有继承)、圆柱类Cylinder(公有继承)、长方体类Cuboid(公有继承)、正方体类Cube(公有继承).派生类Circle集成
3、了Point类的除构造函数外的全部成员。3源程序#includeusing namespace std;#define PI 3.14class Point public:Point()Point(double x_value,double y_value)this-x_value = x_value;this-y_value = y_value;Point()double Get_X_Value()return x_value;double Get_Y_Value()return y_value;virtual double GetArea()return 0;virtual double
4、GetVolume()return 0;private: double x_value; double y_value;class Circle : public Pointpublic: Circle()Circle(double radius)this-radius = radius;Circle()double GetRadius()return radius; double GetArea()return PI * radius * radius; double GetVolume()return 0;private:double radius;class Rectange :publ
5、ic Pointpublic:Rectange()Rectange(double weight,double height)this-weight = weight;this-height = height;Rectange() double GetWeight()return weight;double GetHeight()return height;double GetArea()return weight*height;double GetVolume()return 0;private: double weight; double height;class Square : publ
6、ic Rectangepublic:Square()Square(double BianChang)this-BianChang = BianChang;Square()double GetBianChang()return BianChang;double GetArea()return BianChang*BianChang;double GetVolume()return 0;private :double BianChang;class Cylinder :public Circlepublic:Cylinder()Cylinder(double radius,double heigh
7、t)this-radius = radius;this-height = height;Cylinder()double GetRadius()return radius;double GetArea()return PI * radius * radius + 2 * PI * radius * height;double GetVolume()return PI * radius * radius * height;private:double radius;double height;class Cuboid : public Rectangepublic:Cuboid()Cuboid(
8、double weight,double height,double gao)this-weight = weight;this-height = height;this-gao = gao;Cuboid()double GetWeight()return weight;double GetHeight()return height;double GetGao()return gao;double GetArea()return 2*(weight*height + height*gao + weight*gao);double GetVolume()return weight*height*
9、gao;private:double weight;double height;double gao;class Cube : public Squarepublic: Cube() Cube(double BianChang)this-BianChang = BianChang;Cube()double GetBianChang()return BianChang;double GetArea()return 6*BianChang*BianChang;double GetVolume()return BianChang*BianChang*BianChang;private: double
10、 BianChang;int main()Circle circle(5);Rectange ractange (6,7);Square square (8);Cylinder cylinder (3,4);Cuboid cuboid (4,6,8);Cube cube (9);cout圆形的面积为: circle.GetArea() n; cout矩形的面积为: ractange.GetArea() n;cout正方形的面积为: square.GetArea() n;cout圆柱体的面积为: cylinder.GetArea() n;cout圆柱体的体积为: cylinder.GetVolume() n; cout长方体的面积为: cuboid.GetArea() n;cout长方体的体积为: cuboid.GetVolume() n; cout正方体的面积为: cube.GetArea() n;cout正方体的体积为: cube.GetVolume() n; return system(pause);4 实验结果5总结