Author: hackett
Wechat official account: Overtime Ape

Memory distribution model

  1. Heap area: new/delete, allocated and released by the programmer, if the programmer does not release, the end of the program by the operating system recycling

  2. Stack area: store function parameter values, local variables, etc. Automatically allocated by the compiler

  3. Static storage: stores global static variables, local static variables, global variables, and virtual function tables

  4. Constant store: global constants, function Pointers, constant arrays, Pointers to functions

  5. Code area: The binary code that holds the function body

reference

  1. The value must be initialized and cannot be changed after initialization

  2. Constant references modify parameters

  3. It’s essentially a pointer constant

function

  1. Default argument (syntax: return value type Function name (argument = default) {})

  2. Function placeholder parameter (syntax: return value type Function name (data type))

  3. Function overloading:

  • In the same scope

  • Same function name

  • Function parameters vary in type/number/order

The constructor

You can have arguments, so overloading can happen

The construct is called automatically when the object is called, not manually, and only once

Call the rules

If the user defines a parameter constructor, c++ no longer provides a default no-parameter constructor, but a default copy constructor is provided

If the user defines a copy constructor, c++ does not provide another constructor

Virtual and pure virtual destructions

A virtual destructor

Syntax: virtual ~ class name (){}

Pure virtual destructor

Syntax: Virtual ~ class name () = 0; Abstract class that cannot instantiate objects

In common

Resolves the superclass pointer to release the subclass object

They all need to be implemented in a specific way

Deep/shallow copy

Shallow copy: A simple copy operation

Deep copy: Reapply for memory in the heap for copy operations. Property is open in the heap, be sure to provide your own copy constructor, to prevent shallow copy caused by duplicate release heap problem

Initialization list

Syntax: Constructor () : Attribute 1 (value 1), attribute 2 (value 2), Attribute 3 (value 3)…

Static members

Static member variable

All objects share the same data

Memory is allocated at compile time

Class declaration, class initialization

Static member function

All objects share a function

Static member functions can only access static member variables

This pointer

define

The this pointer points to the object that the called member function belongs to

The this pointer is a pointer that implies each nonstatic member function

use

A parameter and a member variable with the same name can be distinguished by the this pointer

To return the object itself from a nonstatic member function of a class, use return *this

const

Often function

Const after member function: void func() const{}

Member attributes cannot be modified within a constant function

Member attribute declarations are preceded by the mutable keyword, which can still be modified in constant functions

Often the object

Const before the object is declared

A constant object can only call a constant function

A friend friend

Allows a class or function to access a private member of another class

Global function

​ 类

A member function

Operator overloading

A plus sign

Add two custom data types

Shift to the left

Can output custom data types (with friends)

increasing

Implement your own integer data (pre-increment return reference, post-increment return value)

The assignment

Class has attributes that point to the heap, which can also cause depth copy problems when doing assignments

Relationship between

Compare two objects of a custom data type

A function call

The way of use is very much like the call of a function, so it is called a function, there is no fixed writing, very flexible

inheritance

Inheritance way

​ public

Inherited member attributes remain unchanged

​ protected

Inherited member attributes become protected

​ private

Inherited member attributes become private

The object model

Private superclass members are inherited by subclasses and hidden from the compiler

Structure and destruction sequence

The constructor of the parent class is called before the constructor of the child class is called. The destructor of the child class is called before the destructor of the parent class is called

Member with the same name/static member processing

Access subclass: Direct access

Access parent class: add scope

polymorphism

Meet the conditions

Have a hereditary relationship

A subclass overrides a virtual function in its parent class

Static polymorphism

Function overloading and operator overloading are statically polymorphic and reuse function names

Function address early binding – The function address is determined at compile time

Dynamic polymorphism

Derived classes and virtual functions implement runtime polymorphism

Function address late binding – The runtime determines the function address

Conditions of use

Use superclass Pointers/references to subclass objects, not objects

Pure virtual functions and abstract classes

Syntax: Virtual return value type Function name (argument list) = 0

The characteristics of

Object cannot be instantiated

A subclass must override a pure virtual function in an abstract class, otherwise it also belongs to an abstract class

file

Text file

Write file: ofstream or fstream class

Read files: ifstream or fstream class

Binary file

Ostream & write(const char * buffer,int len);

Read file: istream& read(char *buffer,int len);

The template

A function template

Purpose: Create a generic function whose return value and parameter types can be represented by dummy types

Syntax: Template function declaration or definition

Call the rules

If both function templates and normal functions can be implemented, the normal function is called first

Call the function template first if it can produce a better match

Function templates can be forced through an empty template argument list

Function templates can also be overloaded

Class template

Purpose: To create a generic function in which the member data types of a class can be represented by dummy types

Syntax: Template class

Create time

Member functions in ordinary classes can be created from the start

Member functions in a class template are created when called

If you think it’s good, “Like” it.

Follow my wechat official account [Overtime Ape] to get more content