#include <iostream>
#include <fstream>
using namespace std;
//点类Point
class Point
{
private:
    double x;
    double y;
public:
    Point(double xv=0,double yv=0);
    /*构造函数*/
    void setX(double xv);
    /*设置X坐标*/
    void setY(double yv);
    /*设置Y坐标*/
    double getX() const;
    /*获取X坐标*/
    double getY() const;
    /*获取Y坐标*/
    friend ostream & operator << (ostream &out, const Point &p); //运算符<<重载
    friend istream & operator >> (istream &in, Point &p); //运算符>>重载
};
class Plane /*平面图形基类*/
{
public:
    virtual double length()const=0;
    /*周长*/
    virtual double area()const=0;
    /*面积*/
};
//圆类Circle
class Circle:public Point,public Plane
{
private:
    double radius;
protected:
    static const double PI;
public:
    Circle(double xv=0,double yv=0,double r=0);
    /*构造函数*/
    void setR(double r);
    /*设置半径*/
    double getR() const;
    /*获取半径*/
    double area()const;
    /*面积*/
    double length()const;
    /*周长*/
    friend ostream & operator << (ostream &out, const Circle &c); //运算符<<重载
    friend istream & operator >> (istream &in, Circle &c); //运算符>>重载
};

/*请在这里填写第一部分答案*/

void f(Plane  const &p)
{
    cout<<p.area()<<endl;
    cout<<p.length()<<endl;
}
//主函数
int main(void)
{
    double d1,d2;
    cin>>d1>>d2;
    Point p(d1,d2);
    Circle c;
    
/*请在这里填写第二部分答案*/

    f(c);
    return 0;
}

###输入样例:

1.0 2.1
###输出样例:

30.1907
19.4779

###参考答案

Point类:

```c++
Point::Point(double xv,double yv)
{
    x=xv;
    y=yv;
}
void Point::setX(double xv)
{
    x=xv;
}
void Point::setY(double yv)
{
    y=yv;
}
double Point::getX() const
{
    return x;
}
double Point::getY() const
{
    return y;
}
ostream & operator << (ostream &out, const Point &p)
{
    out<<'('<<p.x<<','<<p.y<<')';
    return out;
}
istream & operator >> (istream &in, Point &p)
{
    char ch1,ch2,ch3;
    in>>ch1>>ch2>>p.x>>ch3>>p.y>>ch1;
    return in;
}

Circle类:

const double Circle::PI=3.14159;
Circle::Circle(double xv,double yv,double r):Point(xv,yv)
{
    radius=r;
}
void Circle::setR(double r)
{
    radius=r;
}
double Circle::getR() const
{
    return radius;
}
double Circle::area()const
{
    return PI*radius*radius;
}
double Circle::length()const
{
    return 2*PI*radius;
}
ostream & operator << (ostream &out, const Circle &c)
{
    out<<'('<<c.getX()<<','<<c.getY()<<')'<<','<<c.radius;
    return out;
}
istream & operator >> (istream &in, Circle &c)
{
    char ch1,ch2,ch3,ch4;
    double x,y,r;
    in>>ch1>>ch2>>x>>ch3>>y>>ch4>>r>>ch1;
    c.setX(y);
    c.setY(x);
    c.setR(x+y);
    return in;
}

main(void)函数:

int main(void)
{
    double d1,d2;
    cin>>d1>>d2;
    Point p(d1,d2);
    Circle c;
    ofstream ofs("s3.txt");
    /*创建文本文件*/
    if(!ofs)
    {
        cout<<"File create error!"<<endl;
        return 0;
    }
    ofs<<p<<endl;
    /*将Point对象写入文本文件*/
    ofs.close();
    ifstream ifs1("s3.txt");
    /*打开文本文件*/
    if(!ifs1)
    {
        cout<<"File open error!"<<endl;
        return 0;
    }
    ifs1>>p;
    /*将Point对象读入*/
    ifs1.close();
    c.setX(p.getY());
    /*设置Circle对象*/
    c.setY(p.getX());
    c.setR(p.getX()+p.getY());
    ofstream ofs1("b3.dat",ios::binary);
    /*创建二进制文件*/
    if(!ofs1)
    {
        cout<<"File create error!"<<endl;
        return 0;
    }
    ofs1.write((char *)&c,sizeof(c));
    /*将Circle对象写入二进制文件*/
    ofs1.close();
    ifstream ifs2("b3.dat",ios::binary);
    /*打开二进制文件*/
    if(!ifs2)
    {
        cout<<"File open error!"<<endl;
        return 0;
    }
    ifs2.read((char *)&c,sizeof(c));
    /*将Circle对象读入*/
    ifs2.close();
    f(c);
    /*调用f函数*/
    return 0;
}
C++ 面向对象程序设计:圆类设计与文件读写操作

原文地址: https://www.cveoy.top/t/topic/okCR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录