第11周项目4-类族的设计

/*
*copyright (c)2015,烟台大学计算机学院
*All rights reserved
*文件名称:project.cpp
*作者:孙春红
*完成日期:2015年5月20日
*版本号:v1.0
*
*问题描述:
按以下的提示,由基类的设计和测试开始,逐渐地完成各个类的设计,求出圆格柱体的表面积、体积并输出并且完成要求的计算任务:
    (1)先建立一个Point(点)类,包含数据成员x,y(坐标点),实现需要的成员函数,并设计main函数完成测试;
    (2)以Point为基类,派生出一个Circle(圆)类,增加数据成员r(半径),以及求面积的成员函数area,实现其他需要的成员函数,设计main函数完成测试
    (3)再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高),,以及求圆柱表面积的成员函数area和求圆柱体积的成员函数volume,实现需要的成员函数,并设计main函数完成测试。
要求编写程序,设计出各类中“需要的成员函数”,包括构造函数、析构函数、修改数据成员和获取数据成员的公共接口、用于输出的重载运算符“<<”函数等。
   (提示:此任务可以分为三个子任务分成若干步骤进行。先声明基类,再声明派生类,逐级进行,分步调试。——这种方法适用于做任何的项目)
*输入描述:略。
*程序输出:略。
*/
    按以下的提示,由基类的设计和测试开始,逐渐地完成各个类的设计,求出圆格柱体的表面积、体积并输出并且完成要求的计算任务:
    (1)先建立一个Point(点)类,包含数据成员x,y(坐标点),实现需要的成员函数,并设计main函数完成测试;
#include <iostream>
#include <cmath>
using namespace std;
class Point //定义坐标点类
{
public:
    Point():x(0),y(0) {}
    Point(double x0, double y0):x(x0), y(y0) {}
    friend ostream &operator <<(ostream &out,Point &p);
    double get_x();
    double get_y();
protected:
    double x,y;   //点的横坐标和纵坐标
};
ostream &operator <<(ostream &out,Point &p)
{
    cout<<"point:("<<p.x<<","<<p.y<<")"<<endl;
    return out;
}
double Point::get_x()
{
    return x;
}
double Point::get_y()
{
    return y;
}
int main ()
{
    Point p(1,2);
    cout<<p;
    return 0;
}

    (2)以Point为基类,派生出一个Circle(圆)类,增加数据成员r(半径),以及求面积的成员函数area,实现其他需要的成员函数,设计main函数完成测试;
 #include <iostream>
#include <cmath>
using namespace std;
class Point //定义坐标点类
{
public:
    Point(double a=0, double b=0):x(a), y(b) {}
    void setpoints(double a,double b);
    friend ostream &operator <<(ostream &out,Point &p);
    double get_x()
    {
        return x;
    }
    double get_y()
    {
        return y;
    }
protected:
    double x,y;   //点的横坐标和纵坐标
};
class Circle:public Point
{
public :
    Circle(double x,double y,double r):Point (x,y)
    {
        c_r=r;
    }
    void setcircle(double r);
    double area();
    double get_r()
    {
        return c_r;
    }
    friend ostream &operator <<(ostream &out,Point &c);
private:
    double c_r;
};
void Point::setpoints(double a,double b)
{
    x=a;
    y=b;
}
ostream &operator <<(ostream &out,Point &p)
{
    cout<<"the centre of the circle is :("<<p.x<<","<<p.y<<")"<<endl;
    return out;
}
double Circle::area()
{
    return 3.14*c_r*c_r;
}
ostream &operator <<(ostream &out,Circle &c)
{
    cout<<"the centre of the circle is :("<<c.get_x()<<","<<c.get_y()<<")"<<endl;
    cout<<"the circle's radius is:" <<c.get_r()<<endl;
    return out;
}
int main ()
{
    Circle p(1,1,1);
    cout<<p;
    cout<<"the area of the circle is :"<<p.area()<<endl;
    return 0;
}


运行结果1:

    (3)再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高),,以及求圆柱表面积的成员函数area和求圆柱体积的成员函数volume,实现需要的成员函数,并设计main函数完成测试。
     #include <iostream>
#include <cmath>
using namespace std;
class Point //定义坐标点类
{
public:
    Point(double a=0, double b=0):x(a), y(b) {}
    void setpoints(double a,double b);
    friend ostream &operator <<(ostream &out,Point &p);
    double get_x()
    {
        return x;
    }
    double get_y()
    {
        return y;
    }
protected:
    double x,y;   //点的横坐标和纵坐标
};
class Circle:public Point
{
public :
    Circle(double x,double y,double r):Point (x,y)
    {
        c_r=r;
    }
    void setcircle(double r);
    double area();
    double get_r()
    {
        return c_r;
    }
    friend ostream &operator <<(ostream &out,Point &c);
private:
    double c_r;
};
class Cylinder:public Circle
{
public:
    Cylinder(double x=0,double y=0,double r=0,double h=0);
    void setcylinder(double h);
    double area1();
    double volume();
    double get_h();
    friend ostream &operator <<(ostream &out,Cylinder &y);
private:
    double high;
};
void Point::setpoints(double a,double b)
{
    x=a;
    y=b;
}
ostream &operator <<(ostream &out,Point &p)
{
    cout<<"the centre of the circle is :("<<p.x<<","<<p.y<<")"<<endl;
    return out;
}
double Circle::area()
{
    return 3.14*c_r*c_r;
}
ostream &operator <<(ostream &out,Circle &c)
{
    cout<<"the centre of the circle is :("<<c.get_x()<<","<<c.get_y()<<")"<<endl;
    cout<<"the circle's radius is:" <<c.get_r()<<endl;
    return out;
}
Cylinder::Cylinder(double x,double y,double r,double h):Circle(x,y,r)
{
    high=h;
}
void Cylinder::setcylinder(double h)
{
    high=h;
}
double Cylinder::get_h()
{
    return high;
}
double Cylinder::area1()
{
    return high*3.14*2*get_r();
}
double Cylinder::volume()
{
    return 3.14*get_r()*get_r()*high;
}
ostream &operator <<(ostream &out,Cylinder &y)
{
    cout<<"the centre of the circle is :("<<y.get_x()<<","<<y.get_y()<<")"<<endl;
    cout<<"the circle's radius is:" <<y.get_r()<<endl;
    cout<<"the cylinder's high is "<<y.get_h()<<endl;
    return out;
}
int main ()
{
    Cylinder p(1,1,1,1);
    cout<<p;
    cout<<"the area of the circle is :"<<p.area()<<endl;
    cout<<"the area of the cylinder is :"<<p.area1()<<endl;
    cout<<"the volume of the cylinder is :"<<p.volume()<<endl;
    return 0;
}


运行结果:

知识点总结:

学会声明多层派生类的构造函数以及他们的使用

学习心得:

按照要求,一点一点的做,在原有的程序上加加减减,就很容易做出来了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值