Sun Xin VC ++ lecture notes-(2) c ++

Source: Internet
Author: User
1. in C language, struct does not include functions, while struct in C ++ can include functions.
2. struct and class can be used in C ++. The difference mainly lies in the access control: the default value in struct is public, and the default value in class is private.
3. The most important role of constructor is to create the object itself. In C ++, each class can have multiple constructor, but there must be at least one constructor, when no constructor is explicitly provided in a class, the C ++ Editor automatically provides a default constructor without parameters. This default constructor is only responsible for constructing objects, no Initialization is required. However, as long as you define a constructor in a class, the editor no longer provides default constructor without parameters, regardless of the parameters. The constructor does not return values.
4. The Destructor is called at the end of an object's lifecycle to reclaim the memory space occupied by the object. Only one destructor is required for a class. The Destructor does not return values or contain parameters.
5. The role of the Destructor is opposite to that of the constructor. When the memory space of the object exceeds the effective range, the object's destructor is called.
6. Function overload condition: function overload can be formed only when the parameter types and numbers of functions are different. The overload occurs in the same class.
7. Classes are abstract and occupy no specific physical memory. Only objects are instantiated and occupy specific physical memory.
8. This pointer is an implicit pointer pointing to the object itself (this pointer is not pointing to the class), representing the address of the object. All member functions called by objects are in the same code segment, but each object has its own data member. When an object accesses its data members by calling its member functions, the member functions receive the object address in addition to the real parameters, this address is obtained by a hidden parameter "this". With this pointer, you can access the data members and member functions of the object.
9. The members of the public attribute in the object can be accessed in both the external and sub-classes; members of the protected attribute cannot be accessed outside and can be accessed in the sub-classes; private attributes cannot be accessed both in the subclass and outside.
10. inherited access features of classes: (public, protected, private)
A) The Private Attribute member in the base class. The subclass cannot be accessed regardless of the inheritance method used.
B) use public inheritance. The access characteristics of members of the public and protected attributes in the base class are still consistent in the subclass.
C) use protected inheritance. The access attribute of the public and protected attribute members in the base class changes to protected in the subclass.
D) Use provate to inherit. The access feature of members in the public and protected attributes of the base class is changed to provate In the subclass.
11. Sequence of calling constructors or destructor of subclass and base classes:
When a subclass constructor is called, the base class constructor is called first (if not specified, the base class is called but the constructor without parameters is saved; if you want to specify it, add ": Base Class Name (parameter)" after the subclass constructor name )"). On the contrary, the Destructor calls the subclass destructor first, and then the base class destructor.
12. Function coverage:
Function override occurs between the parent class and the subclass. (The function overload occurs in the same class)
When some member functions of the parent class are rewritten in the subclass, the member functions in the subclass overwrite the corresponding member functions of the parent class with the same name.
13. When you use a parent class pointer to access a member of a subclass object, you can only access the part that the subclass inherits from the parent class. (At this time, the protection and private part of the parent class cannot be accessed externally, and the private part of the parent class cannot be accessed in the Child class .)
14. Polymorphism: add the signed URL before the member function of the base class to become a virtual function. When the member function of this function is called by the subclass object, some subclasses call the subclass, the base class is called if the child class does not exist.
When the C ++ compiler is compiling, it finds that the called member functions define virtual functions in the base class, at this time, C ++ will adopt the late binding technology (late binding). At runtime, it determines which function to call based on the object type. The subclass has a subclass to call, the base class is called if the child class does not exist.
If the member function in the base class is not a virtual function, the binding at this time is an early binding. during compilation, you have determined which function to call.
15. Pure virtual function: When defined in the class, eg: Virtual void F1 () = 0;
Pure virtual functions do not have function bodies. Classes containing pure virtual functions are called abstract classes. abstract classes cannot instantiate objects. When a subclass is derived from the base class of an abstract class, if pure virtual functions in the base class are not implemented, the subclass is also an abstract class and cannot be instantiated.
The pure virtual function is labeled as a non-specific virtual member function. The pure virtual function allows the class to have only the name of the operation but not the content of the specific operation, let the derived class give a specific definition when inheriting. If the derived class does not provide a specific definition of the pure virtual function of the base class, the derived class is also an abstract class and cannot be instantiated.
16. Reference: alias of a variable. You need to use a variable or object to initialize yourself when defining a reference. Once the reference is initialized during definition, it is maintained on a specific variable or object.
The reference does not occupy physical memory (it shares the same memory as the target of the definition reference ). Pointer variables need to occupy physical memory to store addresses.
1. in C language, struct does not include functions, while struct in C ++ can include functions.
2. struct and class can be used in C ++. The difference mainly lies in the access control: the default value in struct is public, and the default value in class is private.
3. The most important role of constructor is to create the object itself. In C ++, each class can have multiple constructor, but there must be at least one constructor, when no constructor is explicitly provided in a class, the C ++ Editor automatically provides a default constructor without parameters. This default constructor is only responsible for constructing objects, no Initialization is required. However, as long as you define a constructor in a class, the editor no longer provides default constructor without parameters, regardless of the parameters. The constructor does not return values.
4. The Destructor is called at the end of an object's lifecycle to reclaim the memory space occupied by the object. Only one destructor is required for a class. The Destructor does not return values or contain parameters.
5. The role of the Destructor is opposite to that of the constructor. When the memory space of the object exceeds the effective range, the object's destructor is called.
6. Function overload condition: function overload can be formed only when the parameter types and numbers of functions are different. The overload occurs in the same class.
7. Classes are abstract and occupy no specific physical memory. Only objects are instantiated and occupy specific physical memory.
8. This pointer is an implicit pointer pointing to the object itself (this pointer is not pointing to the class), representing the address of the object. All member functions called by objects are in the same code segment, but each object has its own data member. When an object accesses its data members by calling its member functions, the member functions receive the object address in addition to the real parameters, this address is obtained by a hidden parameter "this". With this pointer, you can access the data members and member functions of the object.
9. The members of the public attribute in the object can be accessed in both the external and sub-classes; members of the protected attribute cannot be accessed outside and can be accessed in the sub-classes; private attributes cannot be accessed both in the subclass and outside.
10. inherited access features of classes: (public, protected, private)
A) The Private Attribute member in the base class. The subclass cannot be accessed regardless of the inheritance method used.
B) use public inheritance. The access characteristics of members of the public and protected attributes in the base class are still consistent in the subclass.
C) use protected inheritance. The access attribute of the public and protected attribute members in the base class changes to protected in the subclass.
D) Use provate to inherit. The access feature of members in the public and protected attributes of the base class is changed to provate In the subclass.
11. Sequence of calling constructors or destructor of subclass and base classes:
When a subclass constructor is called, the base class constructor is called first (if not specified, the base class is called but the constructor without parameters is saved; if you want to specify it, add ": Base Class Name (parameter)" after the subclass constructor name )"). On the contrary, the Destructor calls the subclass destructor first, and then the base class destructor.
12. Function coverage:
Function override occurs between the parent class and the subclass. (The function overload occurs in the same class)
When some member functions of the parent class are rewritten in the subclass, the member functions in the subclass overwrite the corresponding member functions of the parent class with the same name.
13. When you use a parent class pointer to access a member of a subclass object, you can only access the part that the subclass inherits from the parent class. (At this time, the protection and private part of the parent class cannot be accessed externally, and the private part of the parent class cannot be accessed in the Child class .)
14. Polymorphism: add the signed URL before the member function of the base class to become a virtual function. When the member function of this function is called by the subclass object, some subclasses call the subclass, the base class is called if the child class does not exist.
When the C ++ compiler is compiling, it finds that the called member functions define virtual functions in the base class, at this time, C ++ will adopt the late binding technology (late binding). At runtime, it determines which function to call based on the object type. The subclass has a subclass to call, the base class is called if the child class does not exist.
If the member function in the base class is not a virtual function, the binding at this time is an early binding. during compilation, you have determined which function to call.
15. Pure virtual function: When defined in the class, eg: Virtual void F1 () = 0;
Pure virtual functions do not have function bodies. Classes containing pure virtual functions are called abstract classes. abstract classes cannot instantiate objects. When a subclass is derived from the base class of an abstract class, if pure virtual functions in the base class are not implemented, the subclass is also an abstract class and cannot be instantiated.
The pure virtual function is labeled as a non-specific virtual member function. The pure virtual function allows the class to have only the name of the operation but not the content of the specific operation, let the derived class give a specific definition when inheriting. If the derived class does not provide a specific definition of the pure virtual function of the base class, the derived class is also an abstract class and cannot be instantiated.
16. Reference: alias of a variable. You need to use a variable or object to initialize yourself when defining a reference. Once the reference is initialized during definition, it is maintained on a specific variable or object.
The reference does not occupy physical memory (it shares the same memory as the target of the definition reference ). Pointer variables need to occupy physical memory to store addresses.
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.