April 8, 2021 - Mr. Hou Jie

C + + object-oriented advanced programming

First 2 sections

1. Class is to put functions and data packages together. There can be many copies of data, but there is only one function

2. The size of the object of the string class. In fact, there is only one pointer in each object, but this pointer points to the address where a specific character is stored

3. Be very careful when designing classes that have pointers

4. The header files that refer to the standard library use angle brackets, and the header files that refer to their own writing use double quotation marks

5. The in C is basically similar to that in C + +

6. There is a defensive declaration in the header file you write

#ifndef _COMPLEX_
#DEFINE _complex_


#endif

7. Header file layout: pre declaration first, declaration in class, and finally class definition

The current class definition is generally written as a separate source file

8. A class contains class head, class body,

Some functions are defined directly in the class body, while others are defined outside the body

Section 3

1. Inline function

1) The function defined in the body is inline function, but I don't know whether it really turns into inline function in the end. Generally speaking, complex function compilers can't turn it into inline, and simple ones are generally OK.

2) Add an inline keyword in front of the function definition outside the body to tell the compiler that you should try to turn it into inline

2. Constructor

1) C + + says that if you want to create an object, a function will be called automatically, which is the constructor.

2) A constructor does not need to have a return value because it was invented to create objects.

3) Constructors have a special use called initial columns or initial value columns

For example, you can write in the constructor of a complex class as follows:

//The first method: initialization
public:
	complex(double r=0,double i=0) : re(r),im(i)//Initialization mode
	{   }

private:
	double re,im;

Comparing the differences between these two writing methods, the final results of these two methods are the same. In short, they are

There are two stages for setting the value of a variable, one is initialization and the other is assignment. If the first method is not used, but the efficiency is relatively low. The syntax is to write after the colon,

The second method: assignment
public:
	complex(double r=0,double i=0) 
	{
		re=r;//Method of assignment
		im=i;  
	}

private:
	double re,im;

3. About default arguments

Default means: if you don't specify it when you create it, use the default value. If you specify it, use your default value.

All functions can write out their default values.

4. For destructors, classes without pointers probably don't need to write destructors.

Section 4

01. Constructor and private

If the paparazzi function is placed private So it can't be called during creation. Once the object is created, an error will be reported. What's the use of this class?

Singleton pattern in design pattern( singleton)In this way.

The outside world cannot create such a class. The object can be created only through a member function

1. For parameter passing, try to use reference

2. Try to pass the return value by reference

3. Friend functions (friends) take data directly, which will transfer faster than non friend functions

4. Objects of the same class are friends with each other

5. When writing a class:

1)Try to put the data in private

2)Try to pass parameters by reference. Do you want to add const It depends on the specific situation

3)The return value should be referenced as much as possible. First of all, if not, it should be discarded

4)In class body Should be added in const If you don't, you will report an error

5)The special initialization form of constructor is faster than ordinary initialization. Try to use it as much as possible. This is the special usage of constructor (can't be passed local Variable or object (that is, the variable created in this function), because when this function ends, the object or variable created by the function itself will be destroyed, and the reference transfer will be in error)

6)That is, when the return type is local object Cannot pass reference when

Section 5

1. All member functions must have a hidden this parameter. You can't write this without writing it. It's wrong to write it, but you can use this in the function. This is a pointer,

2. About < < output stream output operator

It must be on the left. Why should it be overloaded << Because in the operator written many years ago, you certainly don't know your newly written class. When you want to output, you must overload it.

We know that there are two ways to write operators, one is member function and the other is non member function. Generally speaking, both can be considered.
But this << For operators, this overload must be written as a global function and must not be written as a member function.

On the right is a complex number and on the left is the receiver, cout It's a object,His type is ostream,yes ostream Class,

For the return type, if there is only one output at a time, the return value type can be set to void,However, if there is serial output, the return value type needs to be able to continue to receive<<The value passed, so the return value type should be designed as ostream

ostream& operator << (ostream& os,const complex& x)
{
	return os << '(' << real(x) << imag(x) << ')';
}

Section 6

About < < operator overloading; I started talking at 21:00 in the video. I was still confused after listening to it several times

Why not?
cout << c1;	(1)
c1 << cout;	(2)

First of all, we understand that our purpose is to give c1 to cout. The second point is that the < < operator must act on the left side, so it cannot be written as a member function. In order to adapt to everyone's writing habits, we need to write with (1)

Section 7

string class

Copy construction copy ctor & copy assignment copy op=

The compiler itself has a set of copy construction and copy assignment, which is copied bit by bit. If the set given by the compiler is enough, it does not need to be rewritten. For the complex number, it is not necessary to write. According to the set of the compiler itself, the real and imaginary parts will be copied one by one. But for the string class, if there is a pointer in it, it can't.

So if you write a class with pointers, you must not use the default version of the compiler, but define it yourself.

Section 8

Scope	   scope	Objects within the scope are also called auto object,It is automatically cleaned up at the end of the scope
 Stack		stake	 Objects created in the stack area are called stack object,End at the end of scope
 heap		heap	 It is created by a programmer and must be released by the programmer himself, otherwise there will be a memory leak
 static state	   static    Static objects defined anywhere remain after the scope ends until the program ends
 overall situation	   global	 Objects defined outside all scopes are called global objects, which can be regarded as a kind of static objects,
				  The life cycle also ends after the end of the whole program.

Section 10

1)static

* Static member functions have no  this point,Only static data can be processed
* There is only one copy of static data
* If there is a declaration of static data in the class, you must class External definition (some people call it declaration and set initial value)
* What is definition: writing a line of code will make the variables in it get memory. This is called definition.
* There are two ways to call static functions:①adopt object Call,②adopt class name call
* Note that there will be a hidden operation when calling a non static function. The address of this object will be placed in the formal parameter class table this point,
* For static functions, the compiler will not have such an operation, that is, No this point
* Typical applications: singleton Singleton mode, put the constructor in private In, only one object can be created through the function

2)cout

*Why? cout Can print all kinds of things?
*because cout Belong to ostream A parameter in a subclass of this parent class, in ostream I've done a lot<<Overloading of this operator
*therefore cout It can accept so many different types of data and print it out.

3)class template & function template

*class template grammar		Write at the beginning of the class template<typename T>
*function template grammar	Write at the beginning of the function template<class T>

*class template usage		The type must be specified when using, such as complex Class: complex<int> c1;
*function template usage	You do not need to specify the type when using,The compiler will deduce the parameters of the function template

4)namespace

*Everything in the standard library is included std Inside, it doesn't have to be written very long. It can be written in sections and will be combined after doing it
*There are three uses:
*① Full open with using namespace std
*② One way, for example, only cout,Just add it at the beginning using std::cout
*③ Do not open, write one by one, to avoid conflict, when using std::cout

Three kinds of object-oriented relationships (relationship between classes)

There are three main types:

1,Composition reunite with

* such as A For the queue, B Is a double ended array.
* A class A There is another class in B Object of has a primer plus,B Can meet A Various functions in, B The function of is relatively powerful, so B Refit it and it becomes another one, A Represents a container.
* This is called an adapter( adapter),Here, A(Queues) are adapters. Not all composites have adapters.

A yes container		B yes component
 Structure from inside to outside
 From outside to inside

2,Delegation Entrust( composition by reference)

Why not by pointer,But call by reference And,
Because that's the rule. No matter what the situation is, we only talk about it by reference. 

pointer to implimplementation	(pimpl)
Handle/Body
* And Composition It also has a certain relationship
* A Class has a pointer to B Class, this relationship is a little empty, in A Want to call B When you're ready, you're ready A need B When you need it, create it again B,Then you can call this pointer to delegate.
* The connection between two classes by pointer is the relationship of delegation. If they are connected by pointer, their lifespan will be inconsistent~The life of the composite relationship is consistent
* It has certain elasticity, A Is unchanged, can only change B To achieve different functions. Sometimes it is also called compilation firewall

3,Inheritance inherit

The most valuable thing is to use it with virtual functions

The object of the subclass, which contains the components of the parent class( part)on the inside

The compiler has helped us realize this order
 When constructing, first call the construction of the parent class, and then call the construction of the child class
 When destructing, first deal with the subclass itself, and then call the parent class

C + + object-oriented advanced programming

Section 2 conversion function##

The conversion function has no return value type, because in order to avoid contradiction with the following
operate  double() const
{
	return(double)(molecule/denominator)
}

Section III

Argument argument

Keywords: C++

Added by camdagr81 on Tue, 08 Mar 2022 01:16:23 +0200