首页 > 代码库 > 十一周 项目4 类族的设计 完整版

十一周 项目4 类族的设计 完整版

#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
    Point(double x=0,double y=0);
    void setPoint(double,double);
    double getx()
    {
        return x;
    }
    double gety()
    {
        return y;
    }
    void display();
protected:
    double x,y;
};

Point::Point(double a,double b)
{
    x=a;
    y=b;
}
void Point::setPoint(double a,double b)
{
    x=a;
    y=b;
}
void Point::display()
{
    cout<<"( "<<getx()<<","<<gety()<<")"<<endl;
}
class Circle:public Point
{
public:
    Circle(double x=0,double y=0,double R=0);
    void setR(double,double,double);//半径
    double getR()
    {
        return R;
    }
    double areas();//圆面积
protected:
    double R;
};
Circle::Circle(double a,double b,double r):Point(a,b),R(r) {}
void Circle::setR(double a,double b,double r)
{
    x=a;
    y=b;
    R=r;
}
double Circle::areas()
{
    return 3.14*R*R;
}
class Cylinder:public Circle
{
public:
    Cylinder(double x=0,double y=0,double R=0,double H=0);
    double getH()
    {
        return H;
    }
    void setH(double,double,double,double);
    double tiji();
    double mianji();
private:
    double H;
};
Cylinder::Cylinder(double a,double b,double r,double h):Circle(a,b,r),H(h) {}
void Cylinder::setH(double a,double b,double r,double h)
{
    x=a;
    y=b;
    R=r;
    H=h;
}
double Cylinder::tiji()
{
    return 3.14*R*R*H;
}
double Cylinder::mianji()
{
    return 3.14*R*R*2+H*(3.14*R*2);
}
int main()
{
    Point p1(3,3),p2(8,8);
    Circle c(2,2,4);
    Cylinder cy(3,4,5,6);
    cout<<"第一点";
    p1.display();
    cout<<"第二点";
    p2.display();
    cout<<"圆心为:"<<"( "<<c.getx()<<","<<c.gety()<<")"<<" 半径为:"<<c.getR()<<" 的圆的面积为:"<<c.areas()<<endl;
    cout<<"圆心为:"<<"( "<<cy.getx()<<","<<cy.gety()<<")"<<" 半径为:"<<cy.getR()<<" 高为:"<<cy.getH()<<" 的圆柱体积为:"<<cy.tiji()<<" 面积为:"<<cy.mianji()<<endl;
    return 0;
}

感悟:慢慢地似乎会些了,懂了其中的注意事项