C + + reverse -- basic syntax

C + + reverse -- basic syntax (I)

  • The source file extension of C + + is cpp (short for C plus).
  • The entry of C + + program is the main function.
  • C + + is fully compatible with the syntax of C language.

cin/cout syntax

  • C + + often uses cin to control input and cout to control output.

cout syntax

code

cout << "Hello";  //< < operator overload
cout << endl;  //It is equivalent to line feed, without double quotation marks here
cout << "World";

Operation results

Another implementation method

cout << "Hello" << endl << "World";

Operation results

cin syntax

code

int age;
cin >> age;  //Assign the input value to the age variable
cout << "age is " << age << endl;

Operation results

Supplementary knowledge

The getchar() function is to wait for keyboard input (if you press enter, the keyboard input will be read, so that you will not exit the main function. Only when you enter something can you exit. In this way, we can use F5 to execute the program, and then we can stop, otherwise we will flash directly.)

Function Overload

What is function overloading?

  • Same function name
  • The order, type, content and number of parameters are different.
  • The return value type has nothing to do with function overloading and cannot constitute overloading.
  • C language does not support function overloading
  • There is duality in implicit transformation.

code

#include <iostream>
using namespace std;

int sum(int v1,int v2) {
		return v1 + v2;
	}	
int sum(int v1,int v2,int v3) {
	return v1 + v2 + v3;
}
void func(int v1, double v2) {
	cout << "func(int v1, double v2)" << endl;
}
void func(double v1, int v2) {
	cout << "func(double v1, int v2)" << endl;
}
int main() {
	/*
	    C Language does not support function overloading
		C++Support function overloading
	*/
	
	cout << sum(10,20) << endl;
	cout << sum(10, 20, 30) << endl;
	func(10, 1.5);
	func(1.5, 10);
	getchar();
	return 0;
}

Operation results

Why does C + + support function overloading?

  • C + + adopts name mangling or name decoration technology
  • During overloading, multiple functions with different function names are generated, such as deploy(int a) and deploy(double a). These two functions are overloaded and may be compiled into deploy by the compiler respectively_ Int (int a) and deploy_double(double a)
  • However, different compilers have different overloading methods for C + +.

Understand function overloading from the perspective of assembly

  • j enter the debugging state and insert the breakpoint, so that the disassembly button can appear
  • F5 run, and then right-click the disassembly button
  • Comparing the differences between func(int v1,double v2) and func(double v1,int v2), it is obvious that the memory address converted by the function is different, so the function is also different.

Supplementary knowledge

Memory address and machine code

00F428B4 6A 0A   
00F428B6 83 EC 08
  • The two memory addresses are continuous. 00F428B4 is followed by two bytes, so 00F428B6 is followed.

Relationship between hexadecimal and byte

  • Eight binaries represent two hexadecimals.
  • Two hexadecimals represent two bytes.
  • Example: the machine code 6A 0A takes up 2 bytes

Analyze function overloading from the perspective of IDA

  • Drag the exe file generated by C + + operation into IDA, which is generated in Debug mode and contains a lot of debugging information.
  • Set the mode to release mode, generate exe and drag it into IDA, but it is optimized in release mode
    • Cancel optimization in release mode
    • Click F5 to run, regenerate the exe file, drag it into IDA, and find that the function name is different.

Supplementary knowledge

  • Debug mode: a lot of debugging information
  • Release mode: a lot of debugging information is removed, and the generated executable file is relatively concise and efficient. The compiler optimizes the function.

Default parameters

C + + allows functions to set default parameters, and the actual parameters can be omitted when calling

  • The default parameters can only be in right to left order.
  • If the function has both declaration and implementation, the default parameters can only be placed in the function declaration.
  • The default parameter values can be variables, global symbols (global variables, function names).

Case code

#include <iostream>
using namespace std;

int sum(int v1, int v2) {
	return v1 + v2;
}
int main() {
	cout << sum(10, 20) << endl;
	getchar();
	return 0;
}

Default parameter modification

#include <iostream>
using namespace std;

int sum(int v1=5, int v2=6) {
	return v1 + v2;
}
int main() {
	cout << sum() << endl;
	cout << sum(10) << endl;
	cout << sum(10, 20) << endl;
	getchar();
	return 0;
}
  • cout << sum() << endl; Represents the use of default parameters, v1=5,v2=6
  • cout << sum(10) << endl; Represents v1=10, v2 uses the default parameter 6
  • cout << sum(10, 20) << endl; Represents v1=10, v2=20

Parameter transfer rules

  • Parameters can only be passed from left to right.
  • You must ensure that the rightmost parameter has a default value or that there are passed arguments.
  • However, the default parameters must start from the far right.

The default function produces ambiguity

  • Function overloading and default parameters may cause conflict and ambiguity (default parameters are preferred)

Understand the principle of default parameters from the perspective of assembly

Program code 1:

#include <iostream>
using namespace std;

int sum(int v1, int v2) {
	return v1 + v2;
}
int main() {
	sum(1, 4);
	sum(2, 5);
	getchar();
	return 0;
}

Assembly: the addresses of the two functions are the same

Program code 2 (with default parameters):

#include <iostream>
using namespace std;

int sum(int v1, int v2=5) {
	return v1 + v2;
}
int main() {
	sum(1);
	sum(1, 5);
	getchar();
	return 0;
}

Assembly: assembly is the same for both calls

extern "C"

  • The code modified by extern "C" will be compiled in the way of C language.
  • However, C language does not support function overloading.

extern "C" case

Program code:

We use extern "C" to execute function overloaded functions

#include <iostream>
using namespace std;

extern "C" void func(){
}
extern "C" void func(int v1) {

}
int main() {

	getchar();
	return 0;
}

Operation results:

As shown in the figure, the function is compiled in C language, and overloading is not supported.

extern "C" purpose

  • Due to the different Compilation Rules of C and C + +, it is mostly used for mixed development of C and C + +.
  • Use third-party frameworks \ libraries during development, which may be written in C language.

C language library

int sum(int v1, int v2)
{
	return v1 + v2;
}


int delta(int v1, int v2)
{
	return v1 - v2;
}

C + + code implementation 1:

#include <iostream>
using namespace std;

extern "C" {    //Using extern "C" tells the compiler to compile using the compilation method of C language
	int sum(int v1, int v2);
	int delta(int v1, int v2);
}
int main() {
	cout << sum(20, 10) << endl;
	cout << delta(20, 10) << endl;
	getchar();
	return 0;
}

C + + code implementation 2:

  • . cpp files are C + + files
  • . C file is a third-party C language library
  • . h is the header file to store the function declaration, which can make it easier for the third-party library to call.
#include <iostream>
using namespace std;
 #include"math.h"
int main() {
	cout << sum(20, 10) << endl;
	cout << delta(20, 10) << endl;
	getchar();
	return 0;
}

Header file implementation:

// header file: stores the declaration of the function
extern "C"{
int sum(int v1, int v2);
int delta(int v1, int v2);
}

Macro definition #define_ cplusplus

  • Macro definition #define exists in C + + by default_ cplusplus
  • By judging the macro definition, we can let the C language code call the header file with extern "C".

Header file code at this time:

// header file: stores the declaration of the function
#ifdef _cplusplus
extern "C"
#endif // _cplusplus

int sum(int v1, int v2);
int delta(int v1, int v2);

#ifdef _cplusplus
   }
#endif // _cplusplus

Macro definition #define_ cplusplus

  • Macro definition #define exists in C + + by default_ cplusplus
  • By judging the macro definition, we can let the C language code call the header file with extern "C".

Header file code at this time:

// header file: stores the declaration of the function
#ifdef _cplusplus
extern "C"
#endif // _cplusplus

int sum(int v1, int v2);
int delta(int v1, int v2);

#ifdef _cplusplus
   }
#endif // _cplusplus

Added by Mesden on Sat, 25 Dec 2021 06:17:41 +0200