C ++ Note 1

Source: Internet
Author: User

 

1. Override)

As long as the member function name in the subclass is the same as that in the parent class, it is called overwrite and has nothing to do with the return type and parameter list.

 

2. Overload)

The number of letters in the same class is the same, but the parameter list is different.

 

3. Inheritance

To inherit a subclass from a parent class, pay attention to the following points:

(1) The order of constructor is the first parent class and the second child class. The order of the Destructor is the first child class and the last child class.

(2). the constructor is defined in the parent class, and must also be defined in the subclass. If no default constructor exists for the parent class at this time, no subclass exists.

(3). When calling the constructor for initialization, you must first initialize the parent class constructor. For example:

Class Ca <br/>{< br/> Public: <br/> Ca (int n) {cout <n <Endl ;}< br/> }; <br/> class CB: public CA <br/> {<br/> Public: <br/> CB (INT m) {cout <m <Endl ;} <br/>}; <br/> void main () <br/>{< br/> cb B (3); <br/>} 

(4) If the base class has a custom copy constructor and the subclass can be defined or not, the default copy constructor of the system will be executed if it is not defined.

4. Protection of constructor and private constructor

Classes with this feature are called abstract classes, which protect constructors from being accessible by quilt classes, while private constructors can only use static declarations in this class to make the entire class have only one instance, called singleon )/

Single-piece example:

// Singleon <br/> class Ca <br/>{< br/> PRIVATE: <br/> Ca (int n) {cout <n <Endl ;} <br/> Public: <br/> static Ca * fungetinstance () <br/> {<br/> return New CA (3 ); <br/>}< br/> static void funrealseinstance (Ca * P) <br/>{< br/> If (P! = NULL) <br/>{< br/> Delete P; <br/>}< br/> P = NULL; <br/>}< br/> void F (INT m) <br/>{< br/> cout <m <Endl; <br/>}< br/>}; <br/> void main () <br/>{< br/> Ca * B = Ca: fungetinstance (); <br/> B-> F (4); <br/> Ca: funrealseinstance (B); <br/>} 

 

5. Multi-Inheritance

Avoid multiple functions as much as possible, which may easily lead to ambiguity and waste of memory space and efficiency. We recommend that you use the idea of inheriting a specific program using a single program. Please join the ccar project in G :.

 

6. Virtual inheritance

Virtual inheritance guarantees that when multiple inheritance occurs, if the virtual base class is defined repeatedly, only one base class is maintained in the memory.

Definition:

Class A: verius pulic CB 

 

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.