P242

OOP: Object-oriented programming is characterized by inheritance and dynamic binding. C++ supports inheritance through the derivation of classes and dynamic binding through virtual functions. Virtual functions provide a way to encapsulate the implementation details of a class architecture.

P243

Abstraction: It is the process of removing unimportant details from an object, leaving only those key points that describe the essential characteristics of the object. Abstraction is a design activity, and all other concepts provide abstract OOP features.

In software, abstraction is very useful because it allows a program to achieve the following goals:

  • Hide irrelevant details and focus on essential features.
  • Provide a “black box” interface to the outside world. An interface identifies the set of valid operations imposed on an object, but it does not indicate how the object implements them internally.
  • The decomposition of a complex system into its independent components. Avoid irregular interactions between components.
  • Reuse and share code.

Abstract establishes an abstract data type, which C++ implements using the Class feature.

P245

Encapsulation: Grouping related types, data, and functions together.

When you tie abstract data types to their operations, you are “encapsulating” them. Non-00p languages do not have a complete mechanism to implement encapsulation. There is no way to tell the C editor, “These three functions are only valid for this particular structure,” or to prevent a program from defining a new function that accesses this structure in unchecked and inconsistent ways.

P246

A class is a user-defined type plus all the operations performed on that class. Classes are often implemented as a structure containing multiple pieces of data, plus Pointers to functions that operate on those pieces of data. The compiler enforces strong typing — ensuring that these functions are called only by objects of that class, and that objects of that class cannot call other functions than themselves.

P247

Access control is a keyword that specifies who can access the data or function that is subsequently declared. Access control can be one of the following three:

  • public: belong topublicThe declaration is visible outside the class and is set as needed.
  • Protected: Can only be used by functions of the class itself and functions of classes derived from it.
  • private:Can only be used by member functions of this class.privateDeclarations are visible outside the class, but not accessible.

    Other than that. There arefriendandvirtual. These two keywords can only be used for one declaration at a time, and each of the above keywords can be followed by a long list of declarations. The other difference is,friendandvirtualThese two keywords are not followed by a colon.
  • friend:Belong tofriendIs not a member function of a class, but can be accessed like a member functionpeotectedandprivate.friendIt can be a function or a class.
  • Virtual: P258 about the knowledge of polymorphism.

P248

When a member function is implemented outside a class, it must be preceded by a prefix ::. :: is called the “global scope resolver”. The identifier that follows it is the scope of the lookup. If :: is not preceded by an identifier, the search scope is global.

P250

This: Each member function has a this pointer argument, which is implicitly assigned to the function and allows the object to reference the object itself within the member function.

Constructors and destructors

  • Most classes have at least one constructor. The constructor is implicitly called when an object of the class is created and is responsible for the initialization of the object.
  • You can declare multiple constructors in a class, distinguishing them by parameters.
  • The constructor name is always the same as the class name. Ex. :
// Constructor initializes Fruit::Fruit(int I, int j){... }; // Fruit melon(4, 5);Copy the code
  • Classes also have a cleanup function called a destructor. When an object is destroyed (out of survival scope or proceedingdeleteAction, reclaim the heap memory it uses), the destructor is automatically called.

Inheritance – Reuse of defined operations

Derive one class from another so that all the characteristics of the former are automatically available in the latter. It can declare types that share some or all of the previously declared types. It can also share some characteristics from more than one base type.

Use examples from the book to illustrate how C++ inherits.

class Fruit { public: peel(); slice(); juice(); private: int weight, calories_per_oz; }; // The base class is Fruit, and the derived class is Apple. class Apple : public Fruit { public: void make_candy_apple(float weight);
      void bob_for(int tub_id, int number_of_attempts);
};

Apple teachers;
Copy the code

Multiple inheritance: Derivation from two or more base classes.

Multiple inheritance seems difficult and is an error-prone feature in both implementation and use. Some argue that there is as yet no convincing case for a design that requires multiple inheritance.

P254

Overloading: The same operation on different types has the same name.

  • Overload is simply reusing an existing name but making it operate on a different type.
  • Overloads are always resolved at compile time.
class Fruit { public: peel(); slice(); juice(); //operator is a keyword, followed by "+" to overload "+". int operator+(Fruit &f); // Override the "+" operator private: int weight, calories_per_oz; }; Int Fruit::operator+(Fruit &f){// Each member function has an implicit this pointer, which allows us to reference the left-hand operand of the operator, and the following"+"The weight before the sign is equal to this.weightreturn weight + f.weight; 
}

Apple apple;
Fruit orange;
int ounces = apple + orange;
Copy the code

C++ input/output (I/O)

C ++ uses the << and >> operators in place of functions such as putchar and getchar in C.

Using operators instead of functions to manipulate I/O has several advantages:

  • Operators can be defined for any type. This eliminates the need to prepare a separate function or string formatting qualifier for each type such as%d.
  • Compared to functions, using operators to manipulate I/O is conceptually convenient when you output multiple messages.

    cout<<"the value is" << i << endl
  • It provides an additional layer that simplifies similarscanf()Format control and usage of such functions.

P258

Polymorphism: The ability for a function or operator to have a single name but to be used for several different derived types.

#include <stdio.h>
    class Fruit
  {
    public:
        void peel() {"peeling a base class fruit\n"};
        slice();
        juice();
    private:
        int weight, calories_per_oz;
  };
  
    class Apple : public Fruit 
  {
    public:
        void peel() {printf("peeling an apple\n"); }; void make_candy_apple(float weight);
  };
  
  Fruit banana;
  banana.peel();
  Fruit *p;
  p = new Apple;
  p->peel();
Copy the code

Running the above program yields the following results

peeling a bass class fruit;
peeling a bass class fruit;
Copy the code

The reason for this is that there is no explicit way to tell the compiler which member functions need to be polymorphic, instead it does so by preloading the base member functions that might be replacedvirtualThe keyword.