简体   繁体   English

多态与继承。 差异?

[英]Polymorphism vs Inheritance. Diffrence?

I dont understand the diffrence between Polymorphism and Inheritance... They Litterarly do the same thing...我不明白多态性和继承之间的区别......他们几乎都在做同样的事情......

Simple Example Of 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;
    }

Above example translated to Inheritance:上面的例子转换为继承:

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

Please tell me diffrence.请告诉我不同​​之处。 Honestly i dont even see the use of Polymorphism!老实说,我什至没有看到多态的使用! Thanks for helping!感谢您的帮助!

Here's a version of your first example, that actually uses polymorphism:这是您的第一个示例的一个版本,它实际上使用了多态:

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

The first big change is that I declare the virtual function area in the shape class.第一个大的变化是我在shape类中声明了virtual函数area For polymorphism to work, the functions must be declared in the base class as virtual .要使多态起作用,必须在基类中将函数声明为virtual The "assignment" to 0 is simply telling the compiler that it's an abstract function, and the child-classes must override that function.0的“赋值”只是告诉编译器它是一个抽象函数,子类必须覆盖该函数。

The second big change is that I use a function to print the area, one that only takes a reference to the base shape class.第二个大变化是我使用一个函数来打印区域,一个只引用基本shape类的函数。 You must use references or pointers to the base class for polymrphism to work, not use the actual objects directly like in your example.您必须使用指向基类的引用或指针才能使多态性工作,而不是像示例中那样直接使用实际对象。

This works as expected .这按预期工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM