c ++ base (seven) - Object-Oriented Programming

  OOP (Object-oriented programming) is the core idea of ​​data abstraction, inheritance, and dynamic binding.

1. Inheritance

  In the C ++ language, the base class type of function associated with the derived class do nothing to change the direct successor of the function treated differently. For some functions, it is desired base class derived class definitions for each version of itself, at this time will the base class virtual function declared as these functions (Virtual function) .

1 class Quote {
2 public:
3     std::string isbn() const;
4     virtual double net_price(std::size_t n) const;
5 };

  Derived classes must clearly indicate that it is the successor of which (which) comes from the base class by using the derivation list (class derivation list):

  as follows:

1 class Bulk_quote : public Quote {
2 public:
3     double net_price(std::size_t) const override;
4 };

  Derived classes must be in its internal declare all virtual functions redefined . A derived class can add the virtual keyword before such a function, but does not have to do so. And, C ++ standard allows new derived classes. 11 explicitly indicate which it uses functions rewritten member base class virtual function, specific measures are changed after the parameter list functions to increase a key override.

1.1 Access Control and inheritance

  

 1 #include<iostream>
 2 class A {
 3 public: 
 4     int p = 0;
 5     virtual void test();
 6 private: int p2 = 1;
 7 protected: int p3 = 2;
 8 };
 9 
10 void A::test()
11 {
12     std::cout << this->p << this->p2 << this->p3 << std::endl;
13 is  }
 14  
15  class B: public A {
 16  public :
 . 17      int B = . 3 ;
 18 is      void Test () {
 . 19          STD :: << COUT the this -> << B the this -> B2 << the this -> << B3 :: endl STD;
 20 is      }
 21 is  
22 is      void test2 () {
 23 is          STD :: << COUT the this -> P3 :: STD << endl; // derived classes can access and protect public base classes 
24      }
 25  
26 is      Friendvoid test3() {
27         std::cout << this-> << std::endl;
28     }
29 private: int b2 = 4;
30 protected: int b3 = 5;
31 };
32 
33 int main()
34 {
35     A a;
36     std::cout << a.p << std::endl;// 只能访问自己的public
37     a.test();
38 
39     B b;
40     std::cout << b.b << b.p << std::endl;// derived classes can only access their own public and puiblic base class 
41 is      b.test ();
 42 is  
43 is }
View Code

 

2. Dynamic Binding

  In the C ++ language, when we use the base class reference (or pointer) to call dynamic binding occurs when a virtual function. Clear call to the derived class to call in the end who print method.

  such as:

 1 #include<iostream>
 2 class A {
 3 public:
 4     A() = default;
 5     virtual void print() {
 6         std::cout << "a" << std::endl;
 7     }
 8     virtual ~A() {
 9         std::cout << "destroy A" << std::endl;
10     };
11 };
12 
13 class B :public A {
14 public:
15     B() = default;
16     void print() {
17         std::cout << "b" << std::endl;
18     }
19     ~B() {
20         std::cout << "destroy B" << std::endl;
21     }
22 
23 };
24 
25 int main()
26 {
27     A a;
28     B b;
29     a.print();
30     b.print ();
 31 is      
32      /// dynamic binding if the print method of the base class virtual function is not, then the following results were A. 
33 is      A = A2 * & A ;
 34 is      A * = B2 & B;
 35      A2-> Print (); // A 
36      B2-> Print (); // B 
37 [  
38 is      A & A3 = A;
 39      A & B3 = B;
 40      a3.print (); // A 
41 is      b3.print (); // B 
42 is  
43 is      /// forcibly calls the base class 
44 is      // bA :: Print (); // A
 45      //b2->A::print(); // a
46     //b3.A::print(); // a
47 
48 }

 

  

Guess you like

Origin www.cnblogs.com/KongHuZi/p/11530992.html