#ifndef VIRTUALFUNCTION_H #define VIRTUALFUNCTION_H /* *覆盖虚函数的返回值,可以是本类类型的指针或者引用,且父类可转为子类。其他覆盖虚函数,返回类型必须相同,参数类型必须相同,参数类型的顺序必须相同。 */ class virtualFunction { public: virtualFunction(); virtual virtualFunction * own(); virtual float defaultChange(); virtual void param(int, double, float); }; class parentFunction: public virtualFunction { public: parentFunction * own() override; //正确 //double defaultChange() override; //报错 conflicting return type float defaultChange() override; //正确 //void param(double, float, int) override; //报错 maked override, but does not override void param(int, double, float) override; //正确 }; #endif // VIRTUALFUNCTION_H cpp---------------------------------- #include "virtualfunction.h" #include <QDebug> virtualFunction::virtualFunction() { } virtualFunction *virtualFunction::own() { qDebug()<<"i am virualFunction son..."; } float virtualFunction::defaultChange() { qDebug()<<"i am son float..."; } void virtualFunction::param(int, double, float) { qDebug()<<"i am son , my param order is int - double - float..."; } parentFunction *parentFunction::own() { qDebug()<<"i am parentFunction father..."; } void parentFunction::param(int, double, float) { qDebug()<<"i am father, my param order is int - double - float..."; } float parentFunction::defaultChange() { qDebug()<<"i am father float..."; } main------------------------- #include "virtualfunction.h" int main(void) { virtualFunction *pvf; pvf = new parentFunction(); pvf->own(); pvf->defaultChange(); pvf->param(0,0,0); // system("pause"); return 0; }
打印结果--------------------- i am parentFunction father... i am father double... i am father, my param order is int - double - float... D:\A_XBS\Qt_project\InheritanceClassPractice\build-inhe
原文地址:https://www.cnblogs.com/azbane/p/11031202.html
时间: 2024-10-29 19:09:50