C++ object-oriented programming - basic introduction (super detailed)

Table of contents

1. Overview of c++

2. Getting to know c++ for the first time

1. The first c++ program

 2. The three major characteristics of c++ object-oriented (important)

Third, the scope operator::

1. Create a namespace using the keyword namespace

2. Namespaces can only be defined globally

3. Namespace nesting

 4. Add new members to the namespace at any time

5. The declaration and implementation of functions in the namespace are separated 

 6. Anonymous Namespace

 7. Namespace aliasing

8. Use using to declare that certain members in the namespace are available 

Problem 1 that arises: Duplicate definitions

Encountered function overloading (same function name, different formal parameters):

9. Using declares the entire namespace

 5. Type Enhancement

1. Enhanced global variable detection

2. C++ function parameters must have a type

3. If the function in c++ has no parameters, you must write void

4. Stricter type conversion

5. Structure type enhancement (important)

 6. C++ adds bool type

7, ternary operator increase

Six, const in c++

7. Citation

1. Definition of reference

2. References as arguments to functions 

3. Reference as the return value type of a function 

4. Often cited

 Eight, inline inline function

1. Overview of inline functions

2. The difference between macro functions and inline functions

3. Inline function conditions

4. Precautions for inline functions

Nine, function overloading

1. The concept of function overloading

2. Conditions for function overloading

10. Default functions

11. Placeholder parameters


1. Overview of c++

       C++ is an extension of C. Any valid C program is a valid C++ program, and C++ programs can use existing C program libraries. The C++ language adds support for object-oriented programming and generic programming
on the basis of the C language . C++ combines three different programming methods: the procedural language represented by C language, the object-oriented language represented by classes added by C++ on the basis of C language, and the generic programming supported by C++ templates. We often refer to these two languages ​​collectively as "C/C++".

c programming ideas: process -oriented

C++ programming ideas: object -oriented , generic programming

Process-oriented (c): By analyzing the steps needed to solve the problem, and then use functions to implement these steps step by step and call them.

Object-oriented (c++): Object-oriented programming, referred to as oop technology. Algorithms and data structures are regarded as a whole (object), program = object + object + object + object        

c++ standard: c++98, c++11

2. Getting to know c++ for the first time

1. The first c++ program

        #include: Precompiled directives . Introduce the header file iostream. using namespace std: Use the standard namespace, and the namespace is defined with the keyword namespace. A namespace is a mechanism of C++.. used to combine a large number of logically linked program entities under a single identifier. This identifier serves as the group's name. cout<<"hello world"< endl; Same function as printf, output string "hello world".

      Extensions: C++ header files have no extension . But some c language header files are converted to c++ header files, these files are renamed and lose the extension . ) . For example, the c++ version of math, h . is cmath

 2. The three major characteristics of c++ object-oriented (important)

  • Encapsulation : Encapsulate data and methods of the same attribute together, and differentiate permissions. Users can only operate private data with public methods .
  • Inheritance : It is reflected in the relationship between classes and classes. If class A inherits from class B, then class A directly owns the data and methods of class B.
  • Polymorphism : One interface (function), multiple functions .

Third, the scope operator::

        :: Solve the problem of duplicate names between local variables and global variables 4. Namespace 

        Creating a name is one of the most basic activities in the programming process. When a project is large, it will inevitably contain a large number of names. The name (name) can be symbolic constants, variables, functions, structures, enumerations, classes and object etc. C++ allows us to control the generation of names and the visibility of names . When we were learning C language before, we could use the static keyword to make the name visible only in this compilation unit. In C++, we will use a namespace to control the scope of the name . The essence of namespace: encapsulation of symbolic constants, variables, functions, structures, enumerations, classes and objects, etc.

1. Create a namespace using the keyword namespace

2. Namespaces can only be defined globally

3. Namespace nesting

 4. Add new members to the namespace at any time

5. The declaration and implementation of functions in the namespace are separated 

 6. Anonymous Namespace

Identifiers in no namespace can only be accessed in this file , which is equivalent to adding static to this identifier .

 7. Namespace aliasing

8. Use using to declare that certain members in the namespace are available 

Problem 1 that arises: Duplicate definitions

 If using A::num is placed externally as a global variable, it will not be defined repeatedly.

Encountered function overloading (same function name, different formal parameters):

namespace A {
	void fun()
	{
		cout << "A中fun void" << endl;
	}
	void fun(int a)
	{
		cout << "A中fun  int" << endl;
	}
	void fun(int a,int b)
	{
		cout << "A中fun int int" << endl;
	}
}
void test()
{	
	//函数重载,命名空间中所有同名函数都被声明可用
	using A::fun();
	fun();
	fun(10);
	fun(10, 10);
}

9. Using declares the entire namespace

Add scope to resolve conflicts:

 5. Type Enhancement

1. Enhanced global variable detection

int a;//No assignment, as a statement

int a=10;//assignment, as definition

This code fails to compile in c++, but passes in c.

2. C++ function parameters must have a type

C language: Allows function parameters to be untyped (arbitrary parameters can be passed), but C++ does not allow it.

 //i没有写类型,可以是任意类型
int fun1(i)
{
printf("%d\n", i);
return 0;
}
//i没有写类型,可以是任意类型
int fun2(i)
{
printf("%s\n", i);
return 0;
 }

This code fails to compile in c++, but passes in c.

3. If the function in c++ has no parameters, you must write void

        In C language , int fun() means a function whose return value is int and accepts any parameters , and int fun(void) means a function without parameters whose return value is int . In C++, ..int fun( and int fun(void) have the same meaning. They both represent a parameterless function whose return value is int .

/没有写参数,代表可以传任何类型的实参
int fun()
{
    printf("fun函数\n");
    return 0;
}
int main(int argc, char *argv[])
  {
    fun(10);
    fun(10, 20);
    fun("hello");
    return 0;
  }

This code fails to compile in c++, but passes in c. If void is added, c does not pass. Therefore, it is recommended to write void if there is no parameter. 

4. Stricter type conversion

In C language, enumeration variables (the bottom layer is a number) allow to assign values ​​​​of other int types, but C++ does not allow

5. Structure type enhancement (important)

 6. C++ adds bool type

        The standard C++ bool type has two built-in constants true (converted to an integer 1) and false (converted to an integer 0) to represent the state. All three names are keywords . The bool type has only two values, true (1 value), false (0 value). The bool type occupies 1 byte. When assigning a value to the bool type, a non-zero value will be automatically converted to true (1) , and a 0 value will be automatically converted to false (0)

7, ternary operator increase

Six, const in c++

1. The const in c++ and c both modify variables as read-only.

2. The C language strictly follows that the const modification is a read-only variable , but its value can be modified through a pointer.

 3. The const of c++ will optimize the variable , and its value cannot be modified through the pointer.

        If you initialize a const-modified variable with a constant , the compiler will put the value of the variable into the symbol constant table , and will not immediately open up space for the variable. Only when the address of a is taken , the compiler will open up space for a (read-only variable)

Access the space content through the pointer variable p *p takes the value of the space. The value in the symbol constant table is accessed through the variable name a.

 4. If you initialize a const-modified read-only variable with a variable , and there is no symbol constant table, open up space immediately, and you can modify its value through a pointer.

 5. If the variable modified by const is a self-defined type, there will be no symbol constant table, and the space will be opened immediately, and its value can be modified through the pointer.

 6. Try to use const instead of define in c++

//#define A 10
const int A=10;
  • const has a type, which can be checked by the compiler for type safety. #define has no type, it is not convenient for type checking
  • const has scope, and #define does not pay attention to scope. Macros cannot be used as members of namespaces, structures, and classes, while const can

7. Citation

1. Definition of reference

        References are an important extension of C++ to C. The role of pointers in C/C++ is basically the same, but C++ adds another way to pass addresses to functions, which is pass-by-reference.

The essence of quoting : aliasing         variable names

Reference steps : T & alias = variable name; (T is any type)

Ordinary variable references:

 Array reference:

Pointer variable references:

Function reference:

 Note: reference (alias) only once

2. References as arguments to functions 

        To save space, external variables can be manipulated by reference inside the function.

3. Reference as the return value type of a function 

 Take chain operations as an example:

4. Often cited

        alias the constant

Example:

Constant references as function parameters: prevent internal functions from modifying external values:

 

 Eight, inline inline function

1. Overview of inline functions

        Inline function: In the compilation stage, the function body in the inline function is replaced at the function call. Avoid the overhead of function calls.

Note: Inline functions must be modified with the keyword inline when they are defined, and inline cannot be used when they are declared

2. The difference between macro functions and inline functions

Both macro functions and inline functions are expanded in place to avoid function call overhead.

The parameters of the macro function have no type, and the integrity of the parameters cannot be guaranteed.

The parameters of the inline function have types, which can ensure the integrity of the parameters.

Macro functions are expanded during the preprocessing phase

Inline functions are expanded at compile time

Macro functions have no scope restrictions and cannot be used as members of namespaces, structures, and classes

Inline functions have limited scope and can be used as members of namespaces, structures, and classes

3. Inline function conditions

There cannot be loop statements of any kind

There cannot be too many conditional judgment statements

The function body should not be too large

cannot take the address of a function

4. Precautions for inline functions

  • Add inline modification when inline function definition
  • The member functions in the class are all inline functions by default (not adding inline is also an inline function)
  • Sometimes adding inline is not necessarily an inline function, and it may be an inline function without adding inline modification. Determined by the compiler based on inlining conditions .

Nine, function overloading

1. The concept of function overloading

        In the traditional C language, the function name must be unique, and functions with the same name are not allowed in the program. Functions with the same name are allowed in C++, this phenomenon is called function overloading. The purpose of function overloading is to facilitate the use of function names.

        Function overloading: use the same function name to represent different function functions.

Function overloading is a feature of static polymorphism         in C++ .

2. Conditions for function overloading

In the same scope, functions with different parameter types, numbers, and orders can be overloaded

Note: The return value type cannot be used as a condition for overloading

In C++, the function name cannot be directly used as the entry address of the function: the function name and parameters together determine the entry address of the function

10. Default functions

C++ can specify default (default) parameter values         ​​for one or more parameters when declaring the function prototype. If this value is not specified when the function is called, the compiler will automatically replace it with the default value.

 Note: If a parameter of the function is set as a default parameter, then all parameters to the right of this parameter must be default parameters.

11. Placeholder parameters

        Overloading the ++ operator is suitable for overloading with identical function names and parameters.

 Placeholder parameters can also be default parameters (default parameters), but it is easy to cause ambiguity when using

 

Guess you like

Origin blog.csdn.net/m0_75045191/article/details/131864071