0

抽象クラスは基本クラスと同じですか?

ベースクラスという言葉をたまに見かけますが、調べてみると「抽象クラス」が飛び交っています。

基本的に同じことを意味する2つの単語ですか?

4

2 に答える 2

2

これは、典型的な基本クラスの Polygon です。

class Polygon {
  protected:
    int width, height;
  public:
    void set_values (int a, int b)
      { width=a; height=b; }
    virtual int area ()
      { return 0; }
};

class Rectangle: public Polygon {
  public:
    int area ()
      { return width * height; }
};

class Triangle: public Polygon {
  public:
    int area ()
      { return (width * height / 2); }
};

抽象基本クラスは、前の例の Polygon クラスに非常に似ています。これらは基本クラスとしてのみ使用できる (インスタンス化できない) クラスであるため、定義なしで仮想メンバー関数 (純粋仮想関数と呼ばれる) を持つことができます。構文は、定義を =0 (および等号とゼロ) に置き換えることです。

抽象基本 Polygon クラスは次のようになります。

// abstract class CPolygon
class Polygon {
  protected:
    int width, height;
  public:
    void set_values (int a, int b)
      { width=a; height=b; }
    virtual int area () =0;
};
于 2015-01-29T04:34:56.693 に答える