1, Function overloading
1. Definitions
Definition: the same function name, different parameter sequences (including different number of parameters or different types of parameters).
2. Basic conditions
Basic conditions:
the function name must be the same;
function parameters must be different, which can be different parameter types or number of parameters;
the return value of the function can be the same or different. (Note: however, if the name and parameters of the function are exactly the same, but the return value types are different, function overloading cannot be carried out.)
3. Code (chongzai class)
(1)chongzai.h
#ifndef CHONGZAI_H #define CHONGZAI_H class chongzai { public: chongzai(); int compare(int a,int b); int compare(int c); double compare(double a, double b); }; #endif // CHONGZAI_H
(2)chongzai.cpp
#include "chongzai.h" chongzai::chongzai() { } int chongzai::compare(int a, int b) { return a>b?a:b; } int chongzai::compare(int c) { int b=2*c-1; return b>c?b:c; } double chongzai::compare(double c, double d) { return c>d?c:d; }
(3)main.cpp
#include "widget.h" #include <QApplication> #include <QDebug> #include "chongzai.h" int main(int argc, char *argv[]) { // QApplication a(argc, argv); // Widget w; // w.show(); chongzai *aa4=new chongzai; int a1=22,b=41; double c=2.1,d=1.1; int res=aa4->compare(a1,b); qDebug()<<"res="<<res<<endl; double res1=aa4->compare(c,d); qDebug()<<"res1="<<res1<<endl; int res2=aa4->compare(a1); qDebug()<<"res2="<<res2<<endl; //return a.exec(); }
4. Operation results
2, Function rewriting
1. Definitions
Definition: the subclass redefines the virtual function with the same name and parameters in the parent class, which mainly appears in the inheritance relationship
2. Basic conditions
Basic conditions:
both the rewritten function and the rewritten function must be virtual functions and located in the base class and derived class respectively;
for rewritten functions and rewritten functions, the function name and function parameters must be completely consistent;
the rewritten function and the rewritten function have the same return value, or return pointer or reference, and the type of pointer or reference returned by the derived virtual function is the type of pointer or reference returned by the replaced virtual function in the base class or its subtype (derived type).
3. Code (chongxie class)
(1)chongxie.h
#ifndef CHONGXIE_H #define CHONGXIE_H class chongxieA { public: chongxieA(); virtual void show(); }; class chongxieB:public chongxieA { public: chongxieB(); void show(); }; class chongxieC:public chongxieA { public: chongxieC(); // void show(); }; #endif // CHONGXIE_H
(2)chongxie.cpp
#include "chongxie.h" #include <QDebug> chongxieA::chongxieA() { } chongxieB::chongxieB() { } chongxieC::chongxieC() { } void chongxieA::show() { qDebug()<<"Parent class A"<<endl; } void chongxieB::show() { qDebug()<<"Subclass B"<<endl; }
(3)main.cpp
chongxieA* b=new chongxieB;//Overriding: subclass functions b->show(); chongxieA* c=new chongxieC;//Not overridden: parent function c->show();
4. Operation results
3, Function overloading
1. Definitions
Function redefinition: non virtual functions with the same name in parent and child classes can have different return value types and parameter lists. When the subclass object calls the method with the same name, the subclass method will override the parent method
2. Basic conditions
the return value type and parameter list can be different.
when the subclass object calls the method with the same name, the subclass method will override the parent method
3. Code (chongdingyi class)
(1)chongdingyi.h
#ifndef CHONGDINGYI_H #define CHONGDINGYI_H class chongdingyiA { public: chongdingyiA(); void show(); }; class chongdingyiB:public chongdingyiA { public: chongdingyiB(); void show(); }; #endif // CHONGDINGYI_H
(2)chongdingyi.cpp
#include "chongdingyi.h" #include <QDebug> chongdingyiA::chongdingyiA() { } chongdingyiB::chongdingyiB() { } void chongdingyiA::show() { qDebug()<<"Function redefinition: parent class"<<endl; } void chongdingyiB::show() { qDebug()<<"Function redefinition: subclasses"<<endl; }
(3)main.cpp
chongdingyiA* a=new chongdingyiB; a->show(); chongdingyiB* b=new chongdingyiB; b->show();
4. Operation results