1. Static parent class Method
Static methods cannot be rewritten in C ++. In fact, subclass and parent classes have static methods with the same name. They are two independent methods. Static Methods belong to classes, and are called through class names rather than objects. Although static methods can also be called through objects, it is equivalent to class: function, however, when calling a static method, C ++ does not care about what the object actually is, but only about the type at compile time. The result is as follows:
/*************************************** * ********** Author: zhou Xiang * E-mail: 604487178@qq.com * blog: Drawing super_hpp # define super_hpp # include <iostream>/*** @ brief the super class parent class */class super {public: super () {} virtual ~ Super () {} virtual void fun (const STD: string & Arg = "super") const {STD: cout <Arg <'\ n ';} static void staticfun () {STD: cout <"Super static function! \ N ";}}; # endif // super_hpp /********************************** * **************** Author: zhou Xiang * E-mail: 604487178@qq.com * blog: Author sub_hpp # define sub_hpp # include "super. HPP "/*** @ brief the sub class subclass */class sub: Public super {public: Sub (): Super () {} virtual void fun (const STD :: string & Arg = "sub") const {STD: cout <ARG <'\ N';} static void staticfun () {STD: cout <"sub static function! \ N ";}};# endif // sub_hpp # include" sub. HPP "int main () {super: staticfun (); Sub: staticfun (); sub Su; Super & s = Su; S. staticfun (); Return 0 ;}
Therefore, do not override the static method of the subclass. It is a new method, and the static method is called by the class name.
2. The parent method is overloaded.
When you specify a name and a set of parameters to override a method, the compiler implicitly hides all other instances of the method with the same name in the parent class. It means that if you overwrite one of them, you should also overwrite all the reload, because the method name is the same and the function should be the same, it's just that the parameters are different. If you change a method, it means all the functions need to be changed.
/*************************************** * ********** Author: zhou Xiang * E-mail: 604487178@qq.com * blog: Drawing super_hpp # define super_hpp # include <iostream>/*** @ brief the super class parent class */class super {public: super () {} virtual ~ Super () {} virtual void fun (const STD: string & Arg = "super") const {STD: cout <Arg <'\ n ';} static void staticfun () {STD: cout <"Super static function! \ N ";}// the following are two overloaded functions: Virtual void overload () const {STD: cout <" Super overload () \ n ";} virtual void overload (INT t) const {STD: cout <"Super overload (INT) and Arg is" <t <'\ n ';}}; # endif // super_hpp /********************************** * **************** Author: zhou Xiang * E-mail: 604487178@qq.com * blog: Awesome sub_hpp # define Sub_hpp # include "super. HPP "/*** @ brief the sub class subclass */class sub: Public super {public: Sub (): Super () {} virtual void fun (const STD :: string & Arg = "sub") const {STD: cout <Arg <'\ n';} static void staticfun () {STD :: cout <"sub static function! \ N ";}// sub overrides one of the virtual void overload () const {STD: cout <" sub overload () \ n ";}}; # endif // sub_hpp # include "sub. HPP "int main () {sub Su; Su. overload (8); Return 0 ;}
A sub object calls the int function. If the compilation times no matching method of 'overload (INT) ', it indicates that, the parent class overload method in the subclass has been implicitly hidden.
But you really need these questions, and you don't need to rewrite them. You can use the using statement to add a using
Super: overload, indicating that I accept all the overload functions of the parent class.
3. Override the private and protected parent class methods
The access qualifier only limits access and rewriting. In fact, this method of rewriting private and protected is a design pattern called a template. During Android development, the activity class has protected.
The void oncreate method needs to be rewritten, that is, in a class inheritance system, these operations are fixed and the behavior without subclass implementation is different. For another example, a subclass of qwidget in QT can process some events, but each subclass may have different responses to events.
4. The parent method has the default parameter value.
In C ++, the default parameter value is resolved by binding the default parameter based on the expression type of the description object, rather than binding the parameter based on the actual object type. This is very similar to the first point mentioned above.
#include "sub.hpp"int main(){ Sub su; Super &s = su; s.fun(); return 0;}
5. Subclass functions have different access levels
There are only two basic changes to access, either enhanced or relaxed. You can reduce the access level so that the subclass can provide a public modifier for the access level. In addition, you can directly rewrite the method and modify the access qualifier in the subclass. To increase the access level, you can only modify the access qualifier in the subclass, but sometimes it cannot be completely improved.
/*************************************** * ********** Author: zhou Xiang * E-mail: 604487178@qq.com * blog: Drawing super_hpp # define super_hpp # include <iostream>/*** @ brief the super class parent class */class super {public: super () {} virtual ~ Super () {}// public access level function virtual void fun () const {STD: cout <"public" <'\ n ';} static void staticfun () {STD: cout <"Super static function! \ N ";}// the following are two overloaded functions: Virtual void overload () const {STD: cout <" Super overload () \ n ";} virtual void overload (INT t) const {STD: cout <"Super overload (INT) and Arg is" <t <'\ n ';} virtual void overload (double T) const {STD: cout <"Super overload (double) and Arg is" <t <'\ n ';}}; # endif // super_hpp /********************************** * **************** Author: zhou Xiang * E-mail: 604487178@qq.com * B Log: Define sub_hpp # define sub_hpp # include "super. HPP "/*** @ brief the sub class subclass */class sub: Public super {public: Sub (): Super () {} static void staticfun () {STD :: cout <"sub static function! \ N ";}// sub overrides one of the virtual void overload () const {STD: cout <" sub overload () \ n ";}using super :: overload; protected: Private: // Private virtual void fun () const {STD: cout <"private" <'\ n ';}}; # endif // sub_hpp # include "sub. HPP "int main () {sub Su; // Su. fun (); Super & s = Su; S. fun (); Return 0 ;}