preface

C++ and Objective-C were both born in the 1980s, so there are a lot of similarities in syntax that make it easy to learn. As for the IDE I use, I use Xcode directly, which is very convenient.

International convention, print Hello world

cout << "hello world" << endl;
Copy the code

Here are a few key words:

Cout output CIN input endL newline print

Cout syntax forms:

cout << x << endl;
cout << "x + y =" << x+y << endl;
Copy the code

Cin syntax:

cin >> x;
cin >> x >> y;
Copy the code

What are the conveniences of such input and output modes? 1. Don’t worry about placeholders 2. Don’t worry about data types 3

####C++ namespace in C++, names can be symbolic constants, variables, macros, functions, structures, enumerations, classes, objects, and so on. To avoid naming conflicts for these identifiers in the design of large-scale programs and when programmers use a variety of C++ libraries, standard C++ introduced the namespace keyword (namespace/namespace/namespace/name domain) to give greater control over the scope of identifiers.

For example: students in the same grade with the same name, so to avoid mistaken identity, we can divide them into different classes, so that they can be separated. This class is called a namespace.

There are a few things about namespace description

1. Use a namespace to define a namespace. 2. Namespaces make it possible to distinguish variables with the same name. Namespaces make it possible to distinguish functions with the same name.

Code examples:

#include <iostream>
#include <stdlib.h>using namespace std; Namespace myNum // Enter the keyword of the namespace {int x = 105; } intmain() {// Define isOdd as the status bit bool isFlag =false;       
	
	if(myNum::x % 2 == 0) {// Change the status bit to befalse
        isFlag = false;
	}
	else{// Change the value of the status bit to betrue
        isFlag = true; } // Determine the status bit valueif(isFlag) {// If the status bit value istrue, then the printed variable x is odd; cout << x is odd; }else{// If the status bit value isfalse, the print variable x is even; cout << x is even; }return 0;
}
Copy the code

####C++ examples of reference code:

#include <iostream>using namespace std; int main(void) { int x = 3; Int &y = x; // Print x and y values cout << x << endl; cout << y <<endl; // change the value of y = 100; // Print the values of x and y again cout << x << endl; cout << y <<endl;return 0;
}
Copy the code

####C++ const

//const
#include <iostream>using namespace std; Int main(void) {// define constant count const int count = 3; const int *p = &count; // prints the string count times Hello C++for(int i = 0; i < count; i++)
	{
		cout << "Hello C++" << endl;
	} 
	return 0;
}
Copy the code

####C++ function new highlights ###### function parameters default value

void fun( int i,int j=5,int k=10 ); // Parameters with default parameter values must be at the far right end of the parameter listCopy the code

###### function overloading Multiple function parameters defined with the same function name in the same scope have different numbers and types of parameters

###### Inline functions are compiled to replace the function body code and arguments with function call statements inline function keyword: inline

C++ function parameter default values

1. When a function has more than one parameter, it can all have default values. 2. When a function has more than one parameter, it can have no default value. 3. When a function has multiple parameters with default values, the number of arguments can be less than the number of parameters.

Code examples:

#include <iostream>using namespace std; Int getMax(int a, int b) {int getMax(int a, int b) {returna > b ? a : b; } /** * return the maximum value in the array * arr: integer array * count: Int getMax(int *arr,int count) {// Define a variable and get the first element of the array int maxNum = arr[0];for(int i = 1; i < count; I++) {// compare the size of the variable to the next elementif(maxNum < arr[I]) {maxNum = arr[I]; }}returnmaxNum; } int main(void) {// Define an int array and initialize int numArr[3] = {3, 8, 6}; Int getMax(int a, int b) cout << getMax(numArr, 3) << endl; Cout << getMax(numArr[0], numArr[2]) << endl;return 0;
}
Copy the code

####C++ memory management what is memory management?

Memory is essentially a resource and the operating system is in charge of memory resources and for us developers, we can manually apply and return memory resources

As shown in figure:

To apply for memory using new and release memory using DELETE, check whether memory application is successful. To release memory, set the null pointer new to work with DELETE

Code examples:

#include <string.h>
#include <iostream>using namespace std; Char * STR = new char[100]; int main(void) {// Apply 100 char * STR = new char[100]; // copy the Hello C++ string into the allocated heap."Hello imooc"); // Prints the string cout << STR << endl; Delete [] STR; str = NULL;return 0;
}
Copy the code

The resources

http://www.imooc.com/learn/342 http://www.imooc.com/learn/381