Summary of object-oriented programming in C + +

Source: Internet
Author: User

1. Three basic concepts of object-oriented programming: Data abstraction (Class), Inheritance (class inheritance), and dynamic binding (the runtime determines whether a base class function or a derived class function is used). The key idea of object-oriented programming is polymorphism.

2. A derived class can inherit a member defined in a base class, a derived class can call a base class function, a derived class can redefine a function of a base class, and a derived class can define a new data member and a function member. 3. The base class uses the keyword virtual to indicate the function (virtual function) that you want the derived class to redefine. A function that the base class expects derived classes to inherit cannot use the virtual keyword. 4. Dynamic binding allows us to use any type of object in the inheritance hierarchy, without caring about the object's specific type. Programs that use these classes do not need to differentiate whether a function is defined in a base class or in a subclass. 5. In the inheritance hierarchy, the root class is generally defined as a virtual destructor 6. The purpose of virtual is to enable dynamic binding. 7. Calls to non-virtual functions are determined at compile time. 8. In addition to the constructor, any non-static function can be a virtual function. 9. The virtual keyword can only be used when a member function is declared within a class, and can no longer be defined in an out-of-body class using 10. A base class function should generally define any function that a derived class function needs to be redefined as a virtual function 11. User code can access the public members of the class and cannot access private and protected member 12. Derived classes can access the public members and protected members of the base class and cannot access private member 13. Private members can only be accessed by the current class or friend Class 14. In C + +, virtual functions can be called through a reference to a class or a pointer. That is, you can call a function of a derived class using a reference to a base class or a pointer. 15. There is also a very important property about the protected tag: derived classes can access only the protected members of their base classes through derived class objects, and derived classes do not have special access rights to protected members of other objects of their base class type. For example, the following code: Class Base_item {protected:    int price;} ; class Bulk_item:public Base_item {public:    void MEMFCN (const bulk_item& D, const base_item& b) {&nbs P          int p = Price;  //correct            p = D.price;    /correct            p = b.price;    //incorrect    }};16. Use the class derivation list to specify the base class in the following format:      class Classname:access-label base-class    & nbsp Where Access-label is Public,private or protected.17. In general, derived classes define only those aspects that are different from the base class or extend the behavior of the base class 18. A derived class typically re-defines a virtual function that uses version 19 defined in the base class if the derived class does not redefine a virtual function. The virtual function declaration of a derived class must exactly match the way it is defined in the base class, with one exception: a virtual function that returns a reference (or pointer) to a base type, in which a derived class reference (or pointer) can be returned. For example, b inherits a, a virtual function with a return type of a * in the Class B, You can return a * or b*20. Derived class object = non-static member defined in the derived class itself + non-static member 21 defined in the base class. The C + + language does not require days to arrange the base and derived class parts of an object in succession. 22. Derived classes access the public and protected members of the base class in the same way as members of the access itself. 23. A class that is defined can be used as a base class, and only declared classes cannot be used as base classes. This is because each derived class contains and can access the members of its base class, and in order to use those members, the derived class must know what they are. 24. According to the rules in the 23 above, a class cannot derive a new class from itself. The base class itself can be a derived class. So there will beDirect base class(Immediate-base) andindirect base classThese two concepts. 25. If you need to declare a derived class, you do not need to declare it with a derived list.       class B:public A;  //error, should not include declaration list       class B; Class A;  //correct 26. A function call in C + + does not use dynamic binding by default. To trigger a dynamic binding, the two conditions must be met: 1. The function must be a virtual function. 2. A function call must be made through a reference or pointer to a base class type. 27. A reference or pointer to a base class type can refer to a base class type object, or it can be applied to a derived type object, regardless of the actual object type, which the compiler considers as a base class type object. This practice is safe because each derived class object has a base class child object. and derived classes inherit the operations of the base class, and any operations that can be performed on the base class object can also be used by derived class objects. 28. The key points for accumulating type references and pointers are the differences between static types (static type, compile-time-determined type) and dynamic-type (type at run-time determination). 29. When a function is called by a base class reference or pointer, the compiler generates additional code that, at run time, determines which function is called and which is called the function that corresponds to the dynamic type. 30. The static and dynamic types of references and pointers can be different, which is the cornerstone of C + + to support polymorphism. On the other hand, the object is non-polymorphic, and its type is always known and unchanging. The function that is run is determined by the type of the object. b inherited from A,a defines a non-virtual function f, which can be redefined in B, and the compiler always determines the execution function based on the actual pointer or type of reference when calling F. So the non-virtual function is determined at compile time. 32. The way to override the virtual function mechanism is to redefine the function with the operator (B integrated self-a,a defines the virtual function F,b):      A * a = new B ();      INT ret = a-> A::f (); 33. A is defined in the virtual function f,b inherits from a does not implement F, at this time if a * a = new B (); The implementation of a is output when you use a call F. Likewise b *b = ne w b (); The implementation of a is also output when calling B->f. 34. When a derived class virtual function calls the base class version, the domain operator must be used explicitly. 35. Virtual function f in derived class B when invoking the version of F in a, you must add the scope operator, which is responsible for causing infinite recursion. 36. Virtual functions can define default arguments. When a function is called, the value of the default parameter is determined at compile time. FunctionThe default value used is determined by the pointer that invokes the function or the referenced object, regardless of the dynamic type of the object. Therefore, it is almost always an error to specify a default parameter for a virtual function because the default parameter uses a static type, whereas a virtual function call uses a dynamic type. 37. In the case of public integration, the base class members maintain their own access levels, and if protected inheritance, the integrated public and protected members are protected members in the derived class, and in the case of private integration, all members of the base class are private members in the derived class. 38. Regardless of the access designator used in the derived list, the derived class has the same level of access to the members of the base class as the base class member itself. Derived access labels are used to control the access of derived class users to inherited base members. The most common form of inheritance is public.39. Public is derived as an interface inheritance, and protected and private are implemented as inheritance. 40. Derived classes can restore the access level of an inherited member, but they cannot make the access level more restrictive or more lenient than the originally established level in the base class. The method of recovery is to use a using declaration. For example:      class Base {       public:            Std::size_ T size () const {return n;}        protected:             std::size_t n;      &N BSP;};       class Derived:private Base {      public:           usi ng base::size;      protected:           using base::n;     } ;      This allows you to access the size members of the base class in your user code, and you can access N in the derived class of derived. 41. If the derived access designator is not applicable, there will be a default inheritance protection level. Depending on whether class or struct is used to define this basic, if class is the access designator to private, and if it is a struct, the access designator is public.42. struct and class have no other difference than the default member protection level and the default derivation protection level. 43. Friends can access the private and protected data of the class, noting that the friend relationship cannot be inherited. Example:      class Base {           friend class frnd;      Protec ted:          int i;     };      class D1:public Base  {&nbs P     protected:          int j;       };      &NBSP;C Lass frnd {       public:            int mem (Base b) {return B.I;} &nbsp ;  //correct             int mem (D1 d) {return D.I;}    //error       &NB Sp };        class D2:public frnd {        public:        &NBSP ;    int Mem (Base b) {return B.I;}    //error     &NBsp  };44. Static members have only one such member throughout the inheritance hierarchy. Static members follow regular access control. 45. There is a reference to a derived class type or a pointer to a reference to a base class type or a pointer conversion, or vice versa. This is because the base class can be part of a derived class, or it may not be part of it. 46. The base class type object can generally be initialized and assigned with objects of the derived class type, but there is no direct conversion from the derived class type object to the base class type object. Reference conversions are different from object conversions, such as when a function passes a reference only, and the copy process occurs. 47. The previously mentioned use of the base class object for initialization and assignment is actually called the function: the constructor is called when initializing, and the assignment operator is called when the assignment is made. 48. In the case of public inheritance, both user code and descendant classes can use derived classes to convert to base classes, and user code cannot convert derived type objects to base class objects if the class is derived using private or protected inheritance. In the case of private integration, a class derived from the private integration class cannot be converted to a base class. In the case of protected integration, members of subsequent derived classes can be converted to base class types. 49. There are no conversions from base class pointers, references, objects to derived class pointers, references, objects. And when a base-class pointer or reference is actually bound to a derived class object, the conversion from the base class to the derived class also has a limit. Example:      Bulk_item bulk;      item_base * ItemP = & bulk;      Bulk_item * Bulkp = ItemP;  //error, cannot convert from base class to derived class. 50. The base class of a non-derived class whose constructors and assignment control functions are largely unaffected by inheritance. 51. The constructor of a derived class initializes the base class, in addition to initializing its own data members. 52. The default constructor for a derived class first executes the default constructor for the base class, initializing its own members. 53. You can override the constructor of a derived class, as follows:      class Bulk_item:public item_base {      public:    &NBS P       Bulk_item (): Min_qty (0), Discount (0.0) {}      };      The default constructor for the base class is still executed first, and then the current member is initialized. 54. The derived class constructor introduces the initialization of an inherited member by including the base class in the constructor initialization list. As shown below:      CALSS bulk_item:public item_base {      public:        &NBS P Bulk_item (const std::string& book, double Sales_price, std::size_t qty=0, double disc_rate=0.0):               item_base (Book,sales_price), Min_qty (qty), Discount (disc_rate) {}      };      The previous example also shows that you can use default parameters in constructors. 55. A class can only initialize its own direct base class. For example C Inherits B,b inheritance a. C cannot initialize a directly, assuming that the author of b has specified how to construct and initialize the type B object. 56. Derived classes can use synthetic copy control members, which are copied, assigned, or revoked by the members of the base class part of the object that is connected to the derived part, using the copy constructor, assignment operator, or destructor of the base class to copy, assign, or revoke the base class part. 57. Contains only the class type or the built-in type of the member, the class does not contain pointers can generally use the composition operation. 58. If a derived class defines its own copy construct, the assignment constructor should generally explicitly use the base class assignment constructor to initialize the base class portion of the object. 59. If a derived class defines its own assignment operator, the operator must explicitly assign a value to the base-class part. For example:      Derived & derived::operator= (const Derived & RHS) {          if (t His! = &AMP;RHS) {             &NBSP Base::operator= (RHS)          }          return * this;    &NB Sp }60. Unlike the destructor and assignment operators, and the copy constructor, the derived class destructor is not responsible for revoking the members of the base class object. The compiler always calls the destructor of the base-class part of the derived class object explicitly. Each destructor is only responsible for the clarity of its own members. The object's destructor order and construction order are reversed: the derived class destructor is run first, and then the destructor is called up by the inheritance hierarchy. 61. A destructor should be defined as a virtual function because it is possible to delete a pointer to a base class type that points to a derived class object. If it is not defined as a virtual function, the destructor of the base class is called, and no members in the derived class are refactored. 62. If a function of the root class in the hierarchy is a virtual function, the function in the derived class will also be a virtual function. Regardless of whether the virtual keyword is added to the derived class. 63. Constructors cannot be defined as virtual functions. If a virtual function is called in a constructor or destructor, the version of the virtual function is called with the same type of object as the current constructor or destructor. For example:      class A {      public:          A () {Show ();}           ~a () {Show ();}           virtual void show () {cout<< "A";}      };      class B:public A {      public:        &N Bsp B () {Show ();}           ~b () {Show ();}           virtual void show () {cout<< "B";}      };    &NBSp int main ()       {          B * b = new B ();          del Ete b;          A * a = new A ();          Delete a;          return 0;     }      output is: A b b a b b A64. A pointer (reference or object) of a base class type can only access the base class part of the object 65. A member of a derived class that has the same name as a base class member will mask direct access to the base class member and can access the masked base class member with the operator. 66. As with data members, function members of derived classes also overwrite function members of the base class. Functions in a base class are overwritten as long as the function name in the derived class is the same as the function name in the base class. 67. A function in a derived class can overload 0 or more versions of a derived class, whether it is a virtual function or a non-virtual function. 68. Rules 66 and 67 cause if the base class defines a series of overloaded functions, if you want to use these overloaded functions in a derived class, you will have to define them again. You can use a using declaration to introduce the function name of a base class, and then define some functions in the derived class that require overloading. 69. Consider the following example:     class Base {     public:         virtual int FCN ( );     };     class d1:public Base {     public:         int FCN (int);     };     class d2:public d1{     public:  &nbsp ;      int FCN (int);         int FCN ();     };     base bobj; D1 D1obj; D2 d2obj;     base * dp1 = & bobj, * bp2 = &d1obj,*bp3=&d2obj;    &NBSP;DP1-&GT;FCN () ;    //correct, call base::fcn    &NBSP;DP2-&GT;FCN ();    //correct, call base::fcn    &NBSP;DP3-&GT;FCN ();    //correct, call d2::fcn70. A pure virtual function that adds = 0 after a function declaration, so that a class that does not override the function cannot create an object. A class that functions one or more pure virtual functions is an abstract base class and cannot create an object of an abstract type except as part of an object that is a derived class of an abstract base class. 71. When saving a base class or derived class object using a container, it is best to use the base class type pointer when specifying the type. 72. Because polymorphism can only be manifested by object pointers or references, the function of a handle is to manipulate polymorphism by encapsulating pointers using objects.
Reference: Primer, 4th ed, Chapter 15

Summary of object-oriented programming in C + +

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.