类族的设计
来源:互联网 发布:cpu降温软件 编辑:程序博客网 时间:2024/06/04 18:41
/** 程序的版权和版本声明部分* Copyright (c)2014, 烟台大学计算机学院学生* All rightsreserved.* 文件名称:a.cpp* 作 者:孔云* 完成日期:2014年5月17日* 版 本 号: v1.0* 输入描述:主函数中给出。* 问题描述:(1)先建立一个Point(点)类,包含数据成员x,y(坐标点),实现需要的成员函数,并设计main函数完成测试; (2)以Point为基类,派生出一个Circle(圆)类,增加数据成员r(半径),以及求面积的成员函数area,实现其他需要的成员函数,设计main函数完成测试; (3)再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高),,以及求圆柱表面积的成员函数area和求圆柱体积的成员函数volume,实现需要的成员函数,并设计main函数完成测试。 要求编写程序,设计出各类中“需要的成员函数”,包括构造函数、析构函数、修改数据成员和获取数据成员的公共接口、用于输出的重载运算符“<<”函数等。* 输出描述:点,圆,圆柱的信息。*/#include<iostream>#include<Cmath>using namespace std;const double pi=3.1415926;class Point //定义坐标点类{public: Point():x(0),y(0) {}//赋值初始化 Point(double x0, double y0):x(x0), y(y0) {}//设值 ~Point(); double getx();//接口函数 double gety(); friend ostream&operator<<(ostream&,Point&);//运算符重载输出点的信息protected: double x,y; //点的横坐标和纵坐标};double Point::getx(){ return x;}double Point::gety(){ return y;}Point::~Point() {}ostream&operator<<(ostream &out,Point&c){ out<<"Point: ("<<c.getx()<<","<<c.gety()<<")"; return out;}class Circle: public Point //利用坐标点类定义圆类, 其基类的数据成员表示圆的半径{public: Circle(double,double,double); //构造函数 void set_r(double);//设置半径 double area(); //计算并返回圆面积 ~Circle(); double getr();//接口函数 friend ostream&operator<<(ostream &,Circle&);//运算符重载输出圆的信息private: double r;};class Cylinder:public Circle{public: Cylinder(double,double,double,double); void set_h(double);//设置圆柱高 double area1(); //计算并返回圆柱面积 double volume();//计算并返回圆柱体积 ~Cylinder(); friend ostream&operator<<(ostream&,Circle&);//运算符重载输出圆柱的信息 double geth();//接口函数private: double h;};Circle::Circle(double p1,double p2,double p3):Point(p1,p2){ r=p3;}void Circle::set_r(double t){ r=t;}Circle::~Circle() {}ostream&operator<<(ostream &out,Circle&c){ out<<"半径:"<<c.getr()<<endl<<"圆面积:"<<c.area()<<endl; return out;}double Cylinder::geth(){ return h;}double Circle::area(){ return(2*pi*r*r);}double Circle::getr(){ return r;}Cylinder::Cylinder(double a,double b,double c,double d):Circle(a,b,c){ h=d;}Cylinder::~Cylinder() {}ostream&operator<<(ostream &out,Cylinder&c){ out<<"圆柱底面半径:"<<c.getr()<<endl<<"圆柱高:"<<c.geth()<<endl<<"圆柱面积"<<c.area1()<<endl<<"圆柱体积:"<<c.volume()<<endl; return out;}void Cylinder::set_h(double t){ h=t;}double Cylinder::area1(){ return (2*pi*getr()*getr()+2*pi*getr()*h);}double Cylinder::volume(){ return(pi*getr()*getr()*h);}int main(){ Point ps(-2,5),pe(7,9); Circle t(1,4,7); Cylinder k(1,4,6,9); cout<<"About the Point: "<<endl; cout<<ps<<endl<<pe<<endl; cout<<"About the Circle:"<<endl; cout<<t<<endl; cout<<"About the Cylinder:"<<endl; cout<<k<<endl; return 0;}
心得体会:此程序有点水平、、、关于类中用到的函数包括实现各功能的函数、构造函数、析构函数、修改数据成员、获取私有(保护)成员的接口等函数,此外输出时,运用重载运算符“<<”函数
0 0
- 类族的设计
- 类族的设计
- 类族的设计
- 类族的设计
- 类族的设计
- 类族的设计
- 11.4类族的设计
- 项目4 - 类族的设计
- 项目4—类族的设计
- 第十一周(类族的设计)
- 2-11-4 类族的设计
- 第十一周-类族的设计
- 点-圆-圆柱类族的设计
- 点-圆-圆柱类族的设计
- 点-圆-圆柱类族的设计
- 点-园-圆柱类族的设计
- 点-圆-圆柱类族的设计
- 圆柱类族的设计3
- 职员有薪水了(拓展)
- ZOJ-2492
- hdu2050-折线分割平面 (递推求解)
- Oracle数据库报ORA-01078和LRM-00109错误解决方法
- MI Report
- 类族的设计
- android使用已安装程序实现分享功能
- MFC不会再打开 已经打开的文件
- Flash Image Editor 基于web在线图像/照片编辑控件详细介绍教程
- 百度云盘资料
- android 横竖屏幕切换注意事项
- C#创建xml文件
- flash火焰燃烧的文字效果
- CMP比较指令详解