在C ++中,我可以像Java一样使用接口变量吗?

时间:2016-03-29 00:19:44

标签: java c++ interface

在Java中,我们可以声明一个没有实例化的接口变量。这可以帮助我们处理一些抽象的概念。在以下Java示例中,我定义了一个接口UShape和两个UShape类,RectangleTriangle。在测试类桶中,我能够定义一个名为myshape的私有接口变量,并使用此变量执行操作。

interface UShape {
    void setwidth(int i);
    void setheight(int i);
    int getarea();
}

class Rectangle implements UShape{
    private int w, h;

    @Override
    public void setwidth(int i) {
        this.w = i;
    }

    @Override
    public void setheight(int i) {
        this.h = i;
    }

    @Override
    public int getarea() {
        return this.w * this.h;
    }
}

class Triangle implements UShape{
    private int w, h;

    @Override
    public void setwidth(int i) {
        this.w = i;
    }

    @Override
    public void setheight(int i) {
        this.h = i;
    }

    @Override
    public int getarea() {
        return this.w * this.h / 2;
    }
}

class bucket{
    private UShape myshape;

    //do something with myshape
    public void defineShape(int i){
        if (i == 1){
            myshape = new Rectangle();
            myshape.setwidth(5);
            myshape.setheight(5);
        }
    }

    public void printArea(){
        System.out.println(myshape.getarea());
    }

}


public class TestShape {
    public static void main(String[] args){
        bucket b = new bucket();
        b.defineShape(1);
        b.printArea();
    }
}

我现在的问题是,在C ++中,我们如何实现这个程序?我检查过并发现C ++中的抽象类不能用于声明像UShape myshape这样的变量。我们可以使用其他方法来实现类似带有接口变量的类吗?

3 个答案:

答案 0 :(得分:3)

也许我误解了你的问题,但没有理由不能声明抽象类类型的私有成员。以下内容应与您的Java示例类似:

class UShape {
  public:
     virtual void setWidth (int w) =0;
     virtual void setHeight (int h) =0;
     virtual int getArea() =0;
     virtual ~UShape() =0;
};

class Rectangle: public UShape {
  private:
     int width;
     int height;
  public:
     void setWidth (int w) { this.width = w; }
     void setHeight (int h) { this.height = h; }
     int getArea (void) { return (width * height); }
};

class Triangle: public UShape {
  private:
     int width;
     int height;
  public:
     void setWidth (int w) { this.width = w; }
     void setHeight (int h) { this.height = h; }
     int getArea (void) { return (width * height) / 2; }
};

class bucket{
  private: 
    std::unique_ptr<UShape> myshape;

  public:

    //do something with myshape
    void defineShape(int i){
       if (i == 1){
          myshape = std::make_unique<Rectangle>();
          myshape->setWidth(5);
          myshape->setHeight(5);
       }
    }
    void printArea(){
        cout << myshape ? myshape->getArea() : 0;
    }

}

int main () {
   bucket b();
   b.defineShape(1);
   b.printArea();
}

答案 1 :(得分:1)

我将使用与Mike相同的想法,而是使用C ++ 11和C ++ 14原语进行内存管理,即。清理分配给指针的内存,而不显式调用delete操作符。

int main () {
    std::unique_ptr<bucket> b(new bucket()); //c++11
    b->defineShape(1);
    b->printArea();
}

}

df = sqlContext.sql('select shubiru, date from thebigtable bt where bt.num > 10 ')
df.show() # here the query is processed and the results shown

答案 2 :(得分:0)

在C ++中,您必须使用指向基类的指针,或更好的智能指针,如shared_ptrunique_ptr

class bucket{
private:
    std::unique_ptr<UShape> myshape;

public:
    //do something with myshape
    void defineShape(int i){
        if (i == 1){
            myshape = std::make_unique<Rectangle>();
            myshape->setwidth(5);
            myshape->setHeight(5);
        }
    }

    void printArea(){
        std::cout << myshape->getarea() << std::endl;
    }
};

int main() {
    bucket b;
    b.defineShape(1);
    b.printArea();
}

在某些情况下,参考也很有帮助:

Rectangle r;
UShape& shape = r;
std::cout << shape.getarea() << std::endl;