Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.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+中实现接口+;?_C++_Inheritance - Fatal编程技术网

C++ 在c+中实现接口+;?

C++ 在c+中实现接口+;?,c++,inheritance,C++,Inheritance,所以我看到了这个帖子:。这涉及到声明父级,但将其初始化为子级。有可能做同样的事情,但是用C++吗?例如:我有一个界面形状,可以实现为三角形或正方形 我尝试执行以下操作,但我的程序未编译: Shape*myShape=new Square(); myShape->uniquetoSquare(); 类形状{ 公众: Shape(){}; 内部高度; 整数宽度; 字符串颜色; void sayHello(){ coutShape没有名为uniqueToSquare的函数。请记住,如果您使用的是Sh

所以我看到了这个帖子:。这涉及到声明父级,但将其初始化为子级。有可能做同样的事情,但是用C++吗?例如:我有一个界面形状,可以实现为三角形或正方形

我尝试执行以下操作,但我的程序未编译:

Shape*myShape=new Square();
myShape->uniquetoSquare();
类形状{
公众:
Shape(){};
内部高度;
整数宽度;
字符串颜色;
void sayHello(){

cout
Shape
没有名为
uniqueToSquare
的函数。请记住,如果您使用的是
Shape
,则只能使用类似Shape的方法

如果要将其用作
正方形
,请执行以下操作:

Square* mySquare = dynamic_cast<Square*>(myShape);
if (mySquare != nullptr) mySquare->uniqueToSquare();
Square*mySquare=dynamic_cast(myShape);
如果(mySquare!=nullptr)mySquare->uniqueToSquare();

<代码> >这是一个简单的多态性例子。是的,C++是可能的,实际上是面向对象设计

的好处所在。

用C++来做它并不像你希望的那么简单。你需要虚拟函数来做你想做的事。修改你的例子是“好”C++。


这是可以的,但请记住在myShape超出范围之前删除它。
。有关析构函数的使用,请参阅RAII。

您的变量
myShape
是指向
形状的指针。您的类
形状
没有名为
uniqueToSquare()
的方法。因此,您有一个错误

解决方案是通过
Square myShape=Square();


请记住,保留对象的真实类型总是更好的,这对性能更好,并且基本上为编译器提供了更多信息。只有在确实需要时(即,将多态对象存储在数组中)才求助于动态多态性.

对于指针,您使用
->
而不是
。因此,
myShape.uniqueToSquare()
将是
myShape->uniqueToSquare()
。如果您需要更多帮助,您可能应该发布来自编译器的错误消息。@ChrisMM谢谢,刚刚更新了错误消息。为了“干净”接口make shape只有所有纯虚拟方法和虚拟析构函数。
sayHello()
getArea()需要在代码< >代码> />代码>中声明“代码>虚拟<代码> >,以便三角形和在C++中覆盖大量的时间,可以使用模板和类型演绎来避免编译时多态性。(
std::vector
std::list
std::deque
,等等)。它们有公共接口,但不共享基类,但在许多情况下它们可以互换使用。你说使用动态转换访问
uniquetoSquare()
,但实际上并不显示动态转换(即
动态转换(myShape)
)。您展示的是C风格的强制转换,最好使用
静态强制转换(myShape)
来处理。请注意
动态强制转换
是运行时强制转换,如果强制转换失败,将返回
NULL
/
nullptr
,但
静态强制转换
是编译时强制转换,不会失败(但如果
myShape
没有指向
Square
对象,则会导致未定义的行为)。知道类型后,使用
static\u cast
,否则使用
dynamic\u cast
来发现类型。我将最后一句话改为“当您知道类型时,使用
static\u cast
,当您的设计被破坏时,使用
dynamic\u cast
”我认为这项任务的重点是使用多态性。目前还不清楚op是否真的想在这里使用多态性,因为他清楚地访问了Square特定的方法……实现基类接口和指定派生类应该具有的方法,以及实际使用可以悬挂式。
Square* mySquare = dynamic_cast<Square*>(myShape);
if (mySquare != nullptr) mySquare->uniqueToSquare();
class Shape {
  public:
    Shape(){}

    int height;
    int width;
    string color;

    virtual void sayHello() const { 
       cout << "Hello!";
    }
    virtual int getArea() const {
      return 0;
    }
    //virtual destructor, no need to declare this on the derived types
    virtual ~Shape() {}
}
class Triangle : public Shape {
  public:
    bool obtuse;
    
    Triangle() {
      obtuse = false;
    }
    void sayHello() const { 
       cout << "Hello I'm a triangle!";
    }
    int getArea() const {
      return height*width / 2;
    }
}
class Rectangle : public Shape {
  public:
    bool square;
    Rectangle() {
      square = false;
    }
    void sayHello() const { 
       cout << "Hello I'm a rectangle!";
    }
    int getArea() const {
      return height*width;
    }
    void uniqueToRectangle() const {
      cout << "This func is only in rectangle!";
    }
}
Shape* myShape = new Rectangle();
((Rectangle*)myShape)->uniquetoRectangle();