Basics of C++ programming [2]

1. The concept of class

1. Sources of classes and instances

A type is an abstraction, and instances of a type are concrete entities

1. Attributes

Attributes are characteristics of the instance we are interested in

2. Behavior

Behavior is what we assume an instance can do to itself

2. Classes and objects in the program

Class corresponds to type, object corresponds to instance

1. Data member

The data members of an object are variables whose values ​​represent properties
In object-oriented programming, the properties of an object are simulated using data members

2. Member functions

In object-oriented programming, the use of member functions is to simulate the behavior of objects

Two, class

C++ program in object-oriented mode consists of class definition, member function definition and application program

1. Class definition

A class definition consists of three parts: class header, class body, and semicolon. The
class header consists of the reserved word class and the name specified by the designer.
Class names begin with an uppercase letter to distinguish them from classes in the library that begin with a lowercase letter A class
body is a statement block that contains declarations of data members and member functions
Finally, there is a semicolon after the closing parenthesis

1. Declare data members

Declare built-in classes or other previously defined class types for variable or constant
data members in a class that cannot depend on each other

2. Declare member functions

Declare all functions used to simulate the behavior of the class
Functions decorated with const cannot change anything

3. Access modifiers

It is used to determine the accessibility of the class, divided into private, protected and public .
The default private
member can only be accessed through member functions when the member is private; when the member is protected, it can be accessed through subclasses

4. Access modifiers for data members

Data members of a class are usually set to private

5. Access modifiers for member functions

The instance member function of a class is usually set to public
, but if it is used to assist other member functions, but does not allow function calls outside the class, it must be set to private

6. Group access modifiers

Only one private keyword and one public keyword are used in the entire class definition, followed by colons for grouping

2. Member function definition

Class scope symbol::
When the value of the object is not changed, add const after the parentheses and before the curly braces

3. Inline functions

When the execution time of a function call is greater than the execution time of the code within the function, in order to improve program performance, the function can be declared as an inline function to instruct the compiler to replace the function call with the actual code in the function

1. Implicit inline functions

When replacing a function declaration with its definition, the function is defined as implicitly inlined (not recommended)

2. Explicit inline functions

Fill in the keyword inline before the function definition, and the function is defined as an explicit inline function

4. Application

1. Object instantiation

2. Operation object

3. Member selection

The point between an object and a member function is called the member selection operator

5. Structure

In a struct all members are public by default while in a class all members are private by default

3. Constructor and destructor

There are 6 member functions in an empty class: constructor and destructor (initialization and cleanup), copy copy and assignment overload (copy and copy), ordinary object address and const object address (address overload )

1. Constructor

A constructor is a special member function used to create and initialize an object, usually public
In a class, it can contain three types of constructors: parameter, default and copy

1. Parameter constructor

Class parametric constructors can be overloaded

2. Default constructor

The default constructor of a class cannot be overloaded

3. Copy constructor

The copy function of the class cannot be overloaded

2. Destructor

The tilde before the destructor is also part of the name
A destructor is a special member function with no parameters that cleans up and recycles objects

3. Create and destroy objects

4. Required member functions

There must be one parameter/default constructor; there must be a copy constructor and a destructor, and what the system provides us is synthetic***

4. Instance members

1. Instance data members and instance member functions

Instance data members of a class are usually private and only accessed through instance member functions An instance member function
of a class must be public so that it can be accessed from outside the class
Member selection operators . and -> (of the indirect operator and member selector combination *xxx.)

1. Lock and unlock

The member function is used by the object pointed to by this pointer

2. Hidden parameters

3. Explicitly use this pointer

4. Host object

The host object is the object that the instance member function operates on at a given moment (the object pointed to by the this pointer)

5. Accessor member functions

An accessor instance function cannot change the state of the host object, it requires the const modifier

6. Changer member function

The mutator instance function changes the state of the host object, it cannot have the const modifier

2. Class invariants

An invariant is one or more conditions that must be imposed on some or all class data members

5. Static members

Static members can be used for data sharing between multiple objects of a class.
Static data members belong to the data members of all instances and must be qualified with the keyword static. Static
member functions have no host object and cannot be
accessed with the const qualifier. Instance data member, because it does not have this pointer parameter.
Instance member functions should access instance data members, and static member functions should access static data members.

6. Object-oriented programming

1. Independent file

1. Interface file

An interface file is a file that includes a class definition
Interface file *.h for a class definition

2. Implementation file

The implementation file contains the definition of the member function.
The implementation file *.cpp is used for the definition of the member function.

3. Application files

The application file includes the main function that instantiates the objects and lets each object perform its own operations. The
application uses the application file app.cpp

2. Independent compilation

The process of compiling the above three files to create an executable file is called independent compilation.
The steps of the compilation process are 5 steps

1. Create an interface file containing only the class definition

2. Create the implementation file

A successful compilation will generate a .o object file

3. Create the application file

A successful compilation will generate a .o object file

4. Link the two object files to create an executable

5. run

3. Prevent multiple inclusions

#ifndef, #define, #endif
prevent repeated definition, if ifndef is already defined, the preprocessor will jump directly to endif

4. Packing

1. Class Design

The design of the class is developed by the developer

2. Use of classes

The use of the class is that the user uses the

3. The effect of encapsulation

Encapsulation can protect interface files and implementation files from being changed

4. Public interface

The public interface is a text file based on the function declaration, which is used to inform the user how to use the member function.
The API document format is as follows

xxx function
A::B()
xxx function, used to return xxx

Guess you like

Origin blog.csdn.net/qq_37249793/article/details/130956784