Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 实施运营商<&书信电报;对于派生类_C++_Class_Operators_Derived - Fatal编程技术网

C++ 实施运营商<&书信电报;对于派生类

C++ 实施运营商<&书信电报;对于派生类,c++,class,operators,derived,C++,Class,Operators,Derived,所以我有一个基类(Shape)和三个派生类,Circle、Rectangle和Square(Square是从Rectangle派生的),我试图实现操作符您调用的display如下: s.display( out ); 但是display定义为: vritual void display() = 0; 函数的声明和定义不带任何参数。它应该引用std::ostream作为参数: virtual void display(std::ostream &) = 0; 当您通过操作符传递con

所以我有一个基类(Shape)和三个派生类,Circle、Rectangle和Square(Square是从Rectangle派生的),我试图实现操作符您调用的
display
如下:

s.display( out );
但是
display
定义为:

vritual void display() = 0;
函数的声明和定义不带任何参数。它应该引用
std::ostream
作为参数:

virtual void display(std::ostream &) = 0;

当您通过
操作符传递
const
对象时,它也应该是一个
const
方法您几乎正确了,下面是工作解决方案:

#include <iostream>

using std::cout;
using std::endl;
using std::ostream;

class Shape
{
     public:
     Shape(double w = 0, double h = 0, double r = 0)
     {
          width = w;
          height = h;
          radius = r;
     }

     virtual ~Shape() {} // Recommended!

     virtual double area() const = 0;
     virtual void display(ostream & out) const = 0;

     protected:
     double width;
     double height;
     double radius;
};

ostream & operator<<(ostream & out, const Shape & s)
{
      // Since `s` is `const`, then `display` method should be `const` too.
      s.display(out);
      return out;
}

class Rectangle : public Shape
{
     public:
     Rectangle(double w, double h) : Shape(w, h)
     {
     }

     virtual double area() const { return width * height; }
     virtual void display(ostream & out)  const
     {
        // Since `display` method is `const`, then `area` method should be
        // `const` too.
        out << "Width of rectangle: " << width << endl;
        out << "Height of rectangle: " << height << endl;
        out << "Area of rectangle: " << this->area() << endl;
     }
};


void main() {
    Rectangle r(1, 2);

    cout << r << endl;
}
#包括
使用std::cout;
使用std::endl;
使用std::ostream;
阶级形态
{
公众:
形状(双w=0,双h=0,双r=0)
{
宽度=w;
高度=h;
半径=r;
}
虚拟~Shape(){}//推荐!
虚拟双面积()常数=0;
虚拟空隙显示(ostream&out)常数=0;
受保护的:
双倍宽度;
双高;
双半径;
};

ostream&operator这里有很多问题。首先,让我们来处理打印问题:

ostream & operator<<(ostream & out, const Shape & s)
您还遇到了一些其他问题—一个没有声明虚拟析构函数的基类。如果您计划以多态方式使用类,则它必须具有虚拟析构函数:

virtual ~Shape() { }
您还需要修改派生类中的方法:

 double area() const { return width * height; }

 void display(ostream& out) const
 {
    out << "Width of rectangle: " << width << endl;
    out << "Height of rectangle: " << height << endl;
    out << "Area of rectangle: " << area() << endl;
 }
double area()常量{返回宽度*高度;}
无效显示(ostream&out)常数
{
out显然,
display()
应该将
ostream&out
作为参数,并写入
out
,而不是
cout
 virtual double area() const = 0;
 virtual void display(ostream& out) const = 0;
virtual ~Shape() { }
 double area() const { return width * height; }

 void display(ostream& out) const
 {
    out << "Width of rectangle: " << width << endl;
    out << "Height of rectangle: " << height << endl;
    out << "Area of rectangle: " << area() << endl;
 }