2016-04-22 21 views
-8

私は彼らがLitterarly同じことを行う...多型と継承の間diffrenceを理解しない...多型対継承。ディフレンス?多型の

簡単な例:

class shape { 
public: 
    void setValues(int height_, int width_) { 
     height = height_, width = width_; 
    } 
protected: 
    int height, width; 

private: 

}; 

class rectangle :public shape, public ThreeDView{ 
public: 
    int area() { 
     return(shape::height*shape::width); 
    } 
    float threeDArea() { 
     return(((shape::height*shape::width)/2)*(std::cos(Z_LENGTH))); 
    } 
}; 

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

int main(){ 
rectangle rect2; 
triangle trng2; 

    rect2.setValues(2,3); 
    trng2.setValues(5,4); 
    std::cout << "AREA : " << trng1.area() << "AREA RECT : \n" <<rect1.area() << std::endl; 
} 
:継承に翻訳された上記の例

class shape { 
    public: 
     void setValues(int height_, int width_) { 
      height = height_, width = width_; 
     } 
    protected: 
     int height, width; 

    private: 

    }; 

    class rectangle :public shape, public ThreeDView{ 
    public: 
     int area() { 
      return(shape::height*shape::width); 
     } 
     float threeDArea() { 
      return(((shape::height*shape::width)/2)*(std::cos(Z_LENGTH))); 
     } 
    }; 

class ThreeDView{ 
public: 
    void setZLength(int value) { 
     Z_LENGTH = value; 
    } 

    int setCompact(bool ans) { 
     compact = ans; 
    } 

    float getZLength() { 
     return Z_LENGTH; 
    } 

    bool getCOMPACT() { 
     return compact; 
    } 
protected: 
    float Z_LENGTH; 
    bool compact; 

private: 
    unsigned char ZCHAR = 'Z'; 
}; 


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

    int main(){ 
    rectangle rect2; 
     triangle trng2; 
     shape *poly = &rect2; 
     shape *poly2 = &trng2; 

     poly->setValues(2,3); 
     poly2->setValues(5,4); 
     std::cout << "AREA : " << trng1.area() << "AREA RECT : \n" <<rect1.area() << std::endl; 
    } 

私にdiffrenceを教えてください。正直なところ、私は多態性の使用を見ていません!助けてくれてありがとう!ここで

+1

多型と継承は2つの直交する概念であり、それらの間には単に類似性はありません。 –

+2

また、あなたの例に欠陥があります。最初は多型を一切使用しません。 –

+1

'ThreeDView'とは何ですか?それが仮想関数を持っていない限り、*多型*型はありません。それは混乱の原因になる可能性があります。 – Bathsheba

答えて

4

は、実際に多型を使用していることを、あなたの最初の例のバージョンです:

#include <iostream> 
#include <string> 

class shape 
{ 
public: 
    void setValues(int height_, int width_) 
    { 
     height = height_; 
     width = width_; 
    } 

    virtual int area() = 0; // This is needed for polymorphism to work 

    virtual std::string name() = 0; 

protected: 
    int height; 
    int width; 
}; 

class rectangle : public shape 
{ 
public: 
    int area() 
    { 
     return height * width; 
    } 

    std::string name() 
    { 
     return "Rectangle"; 
    } 
}; 

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

    std::string name() 
    { 
     return "Triangle"; 
    } 
}; 

void print_area(shape& poly) 
{ 
    std::cout << poly.name() << ' ' << poly.area() << '\n'; 
} 

int main() 
{ 
    rectangle rect; 
    triangle trng; 

    rect.setValues(2, 3); 
    trng.setValues(5, 4); 

    print_area(rect); 
    print_area(trng); 
} 

最初の大きな変化は、私がshapeクラスでvirtual機能areaを宣言することです。多形性が機能するには、基本クラスで関数をvirtualとして宣言する必要があります。 0への "代入"は、それが抽象関数であることをコンパイラに伝えており、子クラスはその関数をオーバーライドする必要があります。

2番目の大きな変更点は、領域を印刷する機能を使用していることです.1つは、shapeベースの参照のみを取ります。実際のオブジェクトを直接あなたの例のように使用するのではなく、ポリミズムが動作するには基本クラスへの参照またはポインタを使用する必要があります。

This works as expected