C + + Override Summary

Source: Internet
Author: User

C + + override is the key to the realization of object-oriented thinking. override refers to overriding the abstract method of the parent class so that the child class has its own implementation of the same interface for the parent class, that is, polymorphic. In C + + to achieve true polymorphism can only be overridden by the parent virtual method, in general should also add the override keyword, so that the compiler helps us check whether the rewrite is correct. A non-virtual method for a parent class should not be overridden if the non-virtual method of the parent class is overridden, which is actually overwrite (hide)andcannot constitute an override

eg

Class Base{public:   virtual void Say () const   {       cout<< "I ' m Base" <<endl;   }   void run () const   {       cout<< "Base run" <<endl;   }}; Class Derived:public Base{public:    void Say () const override    {        cout<< "I ' m Derived" <<endl;    }    void Run () const///  add overrride after compiler error    {        cout<< "Derived run" <<endl;    }};D Erived D; Base &b = D;b.say ();  Output:i ' m derivedb.run ();  Output:base Run

When C + + constitutes An override, the overriding function declaration in the subclass and the virtual function in the parent class must be strictly consistent that is, the number of arguments to the function,theconst property, The return value type is exactly the same, and for the return value type, if the parent class returns a pointer or reference type for the parent class, the subclass can return a pointer and reference to the child class type

eg

Class Base{public:    virtual base* clone () const    {        return new Base (*this);    }}; Class Derived:public Base{public:    derived* Clone () const override final///ok,final keyword description if Derived has sub-class,    {                                    / Then its subclass cannot rewrite the clone function,        return new Derived (*this);    }}; Class Basewrap{public:    virtual base* clone () const    {        return b.clone ();    } Private:    Base b;}; Class Derivedwrap:public Basewrap{public:    derived* Clone () const override final///ok    {        return D.clone ( );    } Private:    Derived D;};

The virtual functions of the parent class can also be declared as pure virtual functions, classes with pure virtual functions become abstract classes, abstract classes cannot create objects, but pointers and references to abstract class types can be defined, pure virtual functions can be implemented only outside of the class, and even if pure virtual functions have implementations, the classes containing them are abstract classes

Class Base{public:    virtual void Say () const  = 0;}; void Base::say () const{    cout<< "Base say" <<ENDL;} Class Derived:public Base{public:    void Say () const override    {        base::say ();        cout<< "Derived say" <<endl;    }; Base b;  Error, abstract class cannot create object derived D; Base &b = D;b.say ();///output:///base say///derived say

What happens when multiple inheritance and function rewriting are encountered in C + +?

eg

Class Basex{public:    virtual void Say () const    {        cout<< "Basex say" <<endl;    }}; Class Basey{public:    virtual void Say () const    {        cout<< "Basey say" <<endl;    }}; Class Derived:public basex,public basey{public:    void Say () const override final    {        cout<< "Derived Say "<<endl;    }};D Erived D; Basex &bx = D; Basey &by = D;bx.say ();   Output:derived Sayby.say ();   Output:derived say

If there is a virtual inheritance, then what is the overload?

eg

Class base{public:virtual void Say () const {cout<< "Base say" <<endl;    }};class basex:public Virtual base{public:void say () const override {cout<< "Basex say" <<endl;    }};class basey:public Virtual base{public:void say () const override {cout<< "Basey say" <<endl; }};class basez:public Virtual base{};///If the virtual function f in the virtual base class is overridden by its different subclasses, and these subclasses are also inherited by the same subclass D, then the class D must override the virtual function f inherited from the virtual base class, otherwise it will produce ambiguous class Deriveda:public basex,public Basey///Error, virtual function say in virtual base class base is overridden by two {///different subclass Basex,basey, so you need to Deri Ved rewrite};class derivedb:public basex,public basey{public:void Say () const override final {cout<< "Derived    B say "<<endl; }};///if the virtual base class is on the inheriting route of its descendant class D, only one of the subclasses of DX overrides its virtual function f///then D does not have to rewrite the virtual function f, and the implementation of F in D is the same as the implementation of the DX class Derivedc:public Basex,public basez{}; Derivedb DB; DERIVEDC DC; Base &AMP;BB = DB,&AMP;BC = DC; Basex &AMP;BBX = DB,&AMP;BCX = DC; Basey &bby = db; Basez &bcz = DC;   Bb.say (); Output:dErivedb say Bbx.say ();  Output:derivedb say Bby.say ();   Output:derivedb say Bc.say ();  Output:derivedb say Bcx.say ();  Output:derivedb say Bcz.say (); Output:derivedb say




















C + + Override Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.