C++:多继承习题5

题目内容:

先建立一个Point(点)类,包含数据成员x,y(坐标点)。以它为基类,派生出一个Circle(圆)类,增加数据成员r(半径),再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高)。要求编写程序,重载运算符“<<”和“>>”,使之能用于输出以上类对象。

输出样例如下:
C++:多继承习题5_第1张图片

代码如下:

#include 
using namespace std;

class Point
{
protected:
    double x;
    double y;

public:
    Point(double x = 0, double y = 0) : x(x), y(y) {}
    friend ostream& operator<<(ostream& os, const Point& p)
    {
        os << "(" << p.x << ", " << p.y << ")";
        return os;
    }

    friend istream& operator>>(istream& is, Point& p)
    {
        is >> p.x >> p.y;
        return is;
    }
};

class Circle : public Point
{
protected:
    double r;

public:
    Circle(double r = 0) : r(r) {}

    friend ostream& operator<<(ostream& os, const Circle& c)
    {
        os << *static_cast(&c) << " 半径: " << c.r;
        return os;
    }

    friend istream& operator>>(istream& is, Circle& c)
    {
        is >> *static_cast(&c) >> c.r;
        return is;
    }
};

class Cylinder : public Circle
{
private:
    double h;

public:
    Cylinder(double h = 0) : h(h) {}

    friend ostream& operator<<(ostream& os, const Cylinder& cyl)
    {
        os << *static_cast(&cyl) << " 高: " << cyl.h;
        return os;
    }

    friend istream& operator>>(istream& is, Cylinder& cyl)
    {
        is >> *static_cast(&cyl) >> cyl.h;
        return is;
    }
};

int main()
{
    Point point(1.0, 2.0);
    Circle circle(3.0);
    Cylinder cylinder(4.0);

    cout << "Point: " << point << endl;
    cout << "circle: " << "圆心" << circle << endl;
    cout << "Cylinder: " << cylinder << endl;

    cin >> point >> circle >> cylinder;

    cout << "Read Point: " << point << endl;
    cout << "Read Circle: " << circle << endl;
    cout << "Read Cylinder: " << cylinder << endl;

    return 0;
}

这段 C++ 代码定义了三个类:Point(点)、Circle(圆)和Cylinder(圆柱体),它们之间存在继承关系,并且重载了输入输出运算符,方便对象的输入和输出操作。以下是对代码的详细解释:

1. Point类的定义

  • 成员变量
    • x 和 y:分别表示点的横坐标和纵坐标,使用protected访问修饰符,允许派生类直接访问。
  • 构造函数
    • Point(double x = 0, double y = 0):带默认参数的构造函数,用于初始化点的坐标。
  • 重载输出运算符 <<
    • 以 (x, y) 的格式输出点的坐标。
  • 重载输入运算符 >>
    • 从输入流中读取两个double类型的值,分别赋值给 x 和 y

2. Circle类的定义

  • 继承关系
    • Circle 类继承自 Point 类,意味着 Circle 类拥有 Point 类的所有成员(x 和 y),可以将圆心看作一个点。
  • 成员变量
    • r:表示圆的半径,使用protected访问修饰符。
  • 构造函数
    • Circle(double r = 0):带默认参数的构造函数,用于初始化圆的半径。
  • 重载输出运算符 <<
    • 先输出圆心的坐标(通过 static_cast 将 Circle 对象转换为 Point 对象),然后输出圆的半径。
  • 重载输入运算符 >>
    • 先从输入流中读取圆心的坐标(通过 static_cast 将 Circle 对象转换为 Point 对象),然后读取圆的半径。

 3. Cylinder类的定义

  • 继承关系
    • Cylinder 类继承自 Circle 类,意味着 Cylinder 类拥有 Circle 类的所有成员(xy 和 r)。
  • 成员变量
    • h:表示圆柱体的高,使用private访问修饰符。
  • 构造函数
    • Cylinder(double h = 0):带默认参数的构造函数,用于初始化圆柱体的高。
  • 重载输出运算符 <<
    • 先输出圆的信息(通过 static_cast 将 Cylinder 对象转换为 Circle 对象),然后输出圆柱体的高。
  • 重载输入运算符 >>
    • 先从输入流中读取圆的信息(通过 static_cast 将 Cylinder 对象转换为 Circle 对象),然后读取圆柱体的高。

 4. 主函数

  • 创建 PointCircle 和 Cylinder 类的对象,并初始化它们的值。
  • 输出这些对象的初始值。
  • 从标准输入读取新的值,分别赋值给 pointcircle 和 cylinder 对象。
  • 输出这些对象的新值。

这段代码通过继承关系构建了一个类层次结构,并且重载了输入输出运算符,使得对象的输入和输出更加方便和直观。

觉得有帮助就给博主点个关注叭~~

有问题的可以私信或者在评论区一起交流

友友们一起加油叭QAQ

你可能感兴趣的:(c++,开发语言)