11-polymorphism and inheritance (I)

Source: Internet
Author: User
I. Function Rewriting

*: Define the same function as the original type in the parent class in the subclass.

*: Function rewriting only occurs between the parent class and subclass.

class Parent{public:void print(){cout<<"parent"<<endl;}};class Child:public Parent{public:void print(){cout<<"child"<<endl;}};

Note: Print () is the rewrite function.

*: The override function in the parent class will still inherit from the subclass.

*: By default, the function rewritten by the subclass hides the function in the parent class.

*: Uses the scope identifier: To access hidden functions in the parent class.

child.print();child.Parent::print();


Compatibility principle when function rewriting encounters a value assignment


void run(){Child child;Parent* pp = &child;Parent& rp =child;child.print();pp->print();rp.print();}


Problem:
(1) c ++ is the same as C and is a static compiling language.

(2) during compilation, the compiler automatically determines the object to which it points based on the pointer type.

(3) the compiler considers that the parent class Pointer Points to a parent class object.

(4) because the program is not running, it is impossible to know whether the parent class Pointer Points to a parent class object or a subclass object.

(5) from the perspective of program security, the compiler assumes that the parent class pointer only points to the parent class object, so the compilation result is to call the member functions of the parent class.

void func(Parent* p){p->print();}

Analysis: when compiling this function, the compiler cannot know what P points to, but there is no reason to report an error, so the compiler thinks

The safest way is to compile the print function of the parent class.

II. The nature of Polymorphism

Determines the specific call target of the function call statement based on the actual object type.

*: If the parent class Pointer Points to a parent class object, the function defined in the parent class is called.

*: If the parent class Pointer Points to a subclass object, the system calls the rewrite function defined in the subclass.

Polymorphism: The same invocation statement has many different manifestations.

Support for Polymorphism in C ++

*: Supports polymorphism through virtual keywords in C ++

*: After a function declared by virtual is overwritten, the result shows polymorphism.

Add virtual to the parent Function

class Parent{public:virtual void print(){cout<<"parent"<<endl;}};



Summary:
(1) function rewriting can only happen between the parent class and the subclass.

(2) the virtual keyword is the only method supporting polymorphism in C ++.

(3) The rewritten virtual function shows polymorphism.




11-polymorphism and inheritance (I)

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.