From c to C + + (bottom)

Source: Internet
Author: User
Tags function prototype

Inherited

Derive from one class to another class so that all features of the former are actively available to them in the latter.

He is able to declare some types that share some or all of the previously declared types. It can also share some of the attributes from more than one base class.

C + + is support for multiple inheritance.

After the inheritance modifier for the class:
With private inheritance, all methods of the parent class become private in subclasses;
With protected inheritance, the protected and public methods of the parent class change to Protected,private method in the subclass;
Using public inheritance, the method attribute in the parent class does not change;

Public Protected Private
Public inheritance Public Protected ---
Protected inheritance Protected Protected ---
Private inheritance Private Private ---

Class Fruit{public:peel (); slice (); juice ();p rivate:int weight;int height;}; Class Apple:public fruit{public:void make_candy_apple (float weight); void Bob_fro (int tub_id, int num_of_attempts);}
Do not confuse a class's inner nesting with a class that is inherited.

The nesting of classes does not have special permissions. There is no special relationship with nested classes.

Nesting is often used to implement container classes (classes that implement some data structures, such as linked lists, queues, and so on).

C + + added templates (template) that are used to implement container classes.

Inheritance indicates that an object is a special type of a more generic parent object.

—————————————————————————————————————————————————————————————————————————————

Multiple inheritance

Multiple inheritance agrees to combine two classes into one. This results in a class object behaving like any of these two classes of objects.

It turns the tree-like system into a lattice.

—————————————————————————————————————————————————————————————————————————————

Polymorphic--binding at execution time

Polymorphic, supporting related objects have different member functions (but the prototype is the same), which is bound when the object is executed with the appropriate member function. C + + supports such a mechanism through overrides --all polymorphic member functions have the same name, which is most appropriate for the execution system to infer.

This mechanism is used when inheriting: Sometimes you cannot tell at compile time whether the object you have is a base class object or a derived class object. This process of inferring and invoking the correct function is called late binding. The member function plus Virtualkeyword tells the compiler that the member function is polymorphic.

In a normal compile-time overload , the prototype of the function must be significantly different. In this way, the compiler can infer the need to invoke the function by looking at the type of erase, but in the virtual function. The prototype of the function must be the same, with which function the system is called to parse at execution time. Such a process is called overlay .

Overloading occurs between different functions in the same class. Overrides occur in multiple class inheritance.

Polymorphism refers to the ability of a function or operator to have only one name, but it can be used for several different derived types.

Each object implements a variant of the operation that behaves best for itself. It starts with overwriting a name and reusing the same name, allowing it to behave differently.

—————————————————————————————————————————————————————————————————————————————

C + + pure virtual function
First, the definition
A pure virtual function is a virtual function declared in a base class. It is undefined in the base class, but requires that whatever derived class defines its own implementation method. The method of implementing a pure virtual function in a base class is to add "= 0" after the function prototype
virtual void funtion () =0
Second, the reasons for the introduction
1, in order to facilitate the use of polymorphic characteristics. We often need to define virtual functions in the base class.
2, in very many cases, the base class itself generates objects that are unreasonable. For example, animals as a base class can be derived from tigers, peacocks and other sub-categories. But the animals themselves generate objects that are obviously irrational.
In order to solve the above problems, the concept of pure virtual function is introduced, and the function is defined as pure virtual function (method: Virtual ReturnType functions () = 0;). The compiler requires that it be overridden in a derived class to achieve polymorphism.

Classes containing pure virtual functions at the same time are called abstract classes. It cannot generate objects. This is a great way to overcome both of these problems.


From c to C + + (bottom)

Related Article

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.