多态性与遗传。 Diffrence?

时间:2016-04-22 12:12:13

标签: c++ inheritance polymorphism

我不理解多态性和继承之间的差异......他们同时做同样的事情......

多态性的简单例子:

    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;
    }

以上示例已转换为继承:

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;
}

请告诉我差异。老实说,我甚至没有看到多态性的使用!谢谢你的帮助!

1 个答案:

答案 0 :(得分: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);
}

第一个重大变化是我在virtual类中声明了area函数shape。要使多态性起作用,必须在基类中将函数声明为virtual0的“赋值”只是告诉编译器它是抽象函数,而子类必须覆盖该函数。

第二个重大变化是我使用一个函数来打印该区域,该区域仅引用基础shape类。您必须使用基类的引用或指针才能使用polymrphism,而不是像在示例中那样直接使用实际对象。

This works as expected