#include #include 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类和Circle类中重载运算符<<和>>,实现文件读写操作。

Point类重载运算符<<和>>:

ostream & operator << (ostream &out, const Point &p) //运算符<<重载 { out << p.x << ' ' << p.y << endl; return out; }

istream & operator >> (istream &in, Point &p) //运算符>>重载 { in >> p.x >> p.y; return in; }

Circle类重载运算符<<和>>:

ostream & operator << (ostream &out, const Circle &c) //运算符<<重载 { out << c.getX() << ' ' << c.getY() << ' ' << c.getR() << endl; return out; }

istream & operator >> (istream &in, Circle &c) //运算符>>重载 { double x, y, r; in >> x >> y >> r; c.setX(x); c.setY(y); c.setR(r); return in; }

完整代码如下:

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

ostream & operator << (ostream &out, const Point &p) //运算符<<重载
{
    out << p.x << ' ' << p.y << endl;
    return out;
}

istream & operator >> (istream &in, Point &p) //运算符>>重载
{
    in >> p.x >> p.y;
    return in;
}

ostream & operator << (ostream &out, const Circle &c) //运算符<<重载
{
    out << c.getX() << ' ' << c.getY() << ' ' << c.getR() << endl;
    return out;
}

istream & operator >> (istream &in, Circle &c) //运算符>>重载
{
    double x, y, r;
    in >> x >> y >> r;
    c.setX(x);
    c.setY(y);
    c.setR(r);
    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;
    cout<<p<<endl;
    cin>>c;
    cout<<c<<endl;
    f(c);
    return 0;
}
C++ 面向对象编程:设计圆类并重载运算符实现文件读写

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

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