#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); //运算符>>重载
};

const double Circle::PI = 3.14159;

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 ch;
    in >> ch; // 读取'('
    in >> p.x;
    in >> ch; // 读取','
    in >> p.y;
    in >> ch; // 读取')'
    return in;
}

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 ch;
    in >> ch; // 读取'(' 
    in >> c.x;
    in >> ch; // 读取','
    in >> c.y;
    in >> ch; // 读取')'
    in >> ch; // 读取','
    in >> c.radius;
    in >> ch; // 读取')'
    return in;
}

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;
    ofstream outfile("s3.txt");
    outfile << p;
    outfile.close();
    ifstream infile("s3.txt");
    infile >> p;
    infile.close();
    c.setX(p.getY());
    c.setY(p.getX());
    c.setR(p.getX() + p.getY());
    ofstream outfile1("b3.dat", ios::binary);
    outfile1 << c;
    outfile1.close();
    ifstream infile1("b3.dat", ios::binary);
    infile1 >> c;
    infile1.close();
    f(c);
    return 0;
}
C++ 面向对象编程:基于Point类和Plane类设计圆类Circle,并实现运算符重载和文件读写操作

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

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