C++ 虚函数

时间:2019-11-16
本文章向大家介绍C++ 虚函数,主要包括C++ 虚函数使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

父类函数不加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()

原文地址:https://www.cnblogs.com/wuyanzu/p/11874063.html