C++ 虚函数

北战南征 提交于 2019-12-04 16:24:52

父类函数不加virtual关键词,子类继承后,当父类指针指向子类指针,同样的函数,会执行父类的函数。子类的函数实际是被隐藏了,如果用子类的指针指向自己的话,是能够执行的。

#include <iostream>
/**
 * C++多态 虚函数
 */

using namespace std;

class Shape {
public:
    Shape();

    ~Shape();

    double calcArea();

};


class Circle : public Shape {
public:
    Circle(double r);

    ~Circle();

    double calcArea();

protected:
    double m_dR;

protected:
    string m_strName;
};


class Rect : public Shape {
public:
    Rect(double width, double height);

    ~Rect();

    double calcArea();

protected:
    double m_dWidth;
    double m_dHeight;
};

Rect::Rect(double width, double height) {
    m_dHeight = height;
    m_dWidth = width;
    cout << "Rect::Rect()" << endl;
}

double Rect::calcArea() {

    cout << "Rect::calcArea()" << endl;
    return m_dWidth*m_dHeight;
}

Rect::~Rect() {
    cout << "~Rect()" << endl;
}

Circle::Circle(double r) {
    m_dR = r;
    cout << "Circle()" << endl;
}

double Circle::calcArea() {
    cout << "Circle::calcArea()" << endl;
    return 3.14 * m_dR * m_dR;
}

Circle::~Circle() {
    cout << "~Circle()" << endl;
}

Shape::Shape() {
    cout << "Shape()" << endl;
}

Shape::~Shape() {
    cout << "~Shape()" << endl;
}

double Shape::calcArea() {
    cout << "Shape::clacArea()" << endl;
}


int main() {
    Shape *shape=new Rect(3,6);
    Shape *shape1=new Circle(5);
    shape->calcArea();
    shape1->calcArea();
    delete(shape);
    delete(shape1);
    return 0;
}

运行的结果

Shape()
Rect::Rect()
Shape()
Circle()
Shape::clacArea() //父类的方法
Shape::clacArea() //父类的方法
~Shape()
~Shape()

加上virtual关键词后

#include <iostream>
/**
 * C++多态 虚函数
 */

using namespace std;

class Shape {
public:
    Shape();

    ~Shape();

    virtual double calcArea();

};

//虚继承
class Circle : public Shape {
public:
    Circle(double r);

    ~Circle();

    double calcArea();

protected:
    double m_dR;

protected:
    string m_strName;
};

//虚继承
class Rect : public Shape {
public:
    Rect(double width, double height);

    ~Rect();

    double calcArea();

protected:
    double m_dWidth;
    double m_dHeight;
};

Rect::Rect(double width, double height) {
    m_dHeight = height;
    m_dWidth = width;
    cout << "Rect::Rect()" << endl;
}

double Rect::calcArea() {

    cout << "Rect::calcArea()" << endl;
    return m_dWidth*m_dHeight;
}

Rect::~Rect() {
    cout << "~Rect()" << endl;
}

Circle::Circle(double r) {
    m_dR = r;
    cout << "Circle()" << endl;
}

double Circle::calcArea() {
    cout << "Circle::calcArea()" << endl;
    return 3.14 * m_dR * m_dR;
}

Circle::~Circle() {
    cout << "~Circle()" << endl;
}

Shape::Shape() {
    cout << "Shape()" << endl;
}

Shape::~Shape() {
    cout << "~Shape()" << endl;
}

double Shape::calcArea() {
    cout << "Shape::clacArea()" << endl;
}


int main() {
    Shape *shape=new Rect(3,6);
    Shape *shape1=new Circle(5);
    shape->calcArea();
    shape1->calcArea();
    delete(shape);
    delete(shape1);
    return 0;
}

运行结果

Shape()
Rect::Rect()
Shape()
Circle()
Rect::calcArea() //子类自己的函数
Circle::calcArea()//子类自己的函数
~Shape()
~Shape()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!