Introduction to C++ (a brief discussion of classes and objects)

1 namespace

1-1 Definition of namespace

The purpose of defining a namespace is not to conflict with the name of the identifier. Functions, variables, and types can be defined in the namespace.

For example: rand and strlens here are actually functions. In the namespace, they can avoid conflicts with the rand function and strlens function in the global scope. Secondly, the namespace can also be nested in definitions.

At the same time, a namespace defines a new scope. Everything in the namespace is limited to that namespace

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
namespace bit
{
	int rand = 10;
	int strlens = 20;
	int Add(int a, int b)
	{
		return a + b;
	}
	class bit
	{
		int Sub(int a = 4, int b = 5)
		{
			return b - a;
		};
		double _money;
		float  _lucky;
		int _life;
	namespace character
	{
		char* next;
		int people;
	}
}

1-2 Use of namespace

Method 1: Add namespace name + scope qualifier

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
namespace bit
{
	int rand = 10;
	int strlens = 20;
	int Add(int a, int b)
	{
		return a + b;
	}
	class bit
	{
		int Sub(int a = 4, int b = 5)
		{
			return b - a;
		};
		double _money;
		float  _lucky;
		int _life;
	}*p;
	namespace character
	{
		char* next;
		int people=10;
	}
}
int main()
{
	cout << bit::character::people<< endl;
	return 0;
}

Method 2: Use using to introduce a member of the namespace

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
namespace bit
{
	int rand = 10;
	int strlens = 20;
	int Add(int a, int b)
	{
		return a + b;
	}
	class bit
	{
		int Sub(int a = 4, int b = 5)
		{
			return b - a;
		};
		double _money;
		float  _lucky;
		int _life;
	}*p;
	namespace character
	{
		char* next;
		int people=10;
	}
}
using bit::Add;
int main()
{
	cout << Add(4,5)<< endl;
	return 0;
}

Method 3: Use using namespace to introduce a namespace

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
namespace bit
{
	int rand = 10;
	int strlens = 20;
	int Add(int a, int b)
	{
		return a + b;
	}
	class bit
	{
		int Sub(int a = 4, int b = 5)
		{
			return b - a;
		};
		double _money;
		float  _lucky;
		int _life;
	}*p;
	namespace character
	{
		char* next;
		int people=10;
	}
}
using namespace bit;
using namespace character;
int main()
{
	cout <<people << endl;
	return 0;
}

2 C++ input and output

The input and output of C++ will automatically identify the type, which is very convenient. It does not require printf to specify the type of output like C. Secondly, C++ also needs to include header files. The header files required for the input stream cout and the output stream cin are #include<iostream>,< ;< is the stream insertion operator, >> is the stream extraction operator, std is the namespace name of the C++ standard library, and C++ places the definition and implementation of the standard library in this namespace.

3 Default parameters

The called function can have no parameters, or the parameters can be default. Here are some examples:

void fun(int a = 10)
{
	cout << a << endl;
}
int main()
{
	fun(2);
	fun();
	return 0;
}

When there are multiple parameters, the defaults cannot be separated, and the defaults can only be consecutive from right to left.

int fun(int a = 10,int b=6,int c = 8)
{
	return a + b + c;
}
int main()
{
	cout << fun(2, 4) << endl;
	cout << fun(4, 4, 4) << endl;
	cout << fun(5) << endl;
	fun(5);
	return 0;
}

4 Function overloading

In other words, I can use the same function name but different parameter types. The compiler will automatically identify the function parameter types and match them accordingly.

int fun(int a = 10,int b=6,int c = 8)
{
	return a + b + c;
}
void fun(char* p, char a, int b)
{
	cout << p << endl;
	cout << a << endl;
	cout << b << endl;
}
int main()
{
	char arr[15] = "I love China!";
	cout << fun(2, 4) << endl;
	fun(arr, 65, 4);
	return 0;
}

5 quotes

Quoting is also equivalent to taking an alias. For example, your name also has a nickname and a big name.

The referenced variable is actually the variable itself. In syntax, the reference is that the alias and the original name share the same space.

But the underlying logic is different. The underlying logic is pointers.

int main()
{
	int a = 10;
	int& b = a;
	b = 100;
	cout << a << endl;
	
	return 0;
}

6 inner function number

Inline functions are actually a way of exchanging space for time. The inline function will be expanded at the place where it is called without opening up a function stack frame, which improves the running efficiency of the program. However, if the space of your inline function is large, it will be very difficult. If it is large, the compiler will automatically undefine its inline.

Keyword modification: inline

You can see that the following assembly code does not open a stack frame for the fun function at all, it is just called.

Guess you like

Origin blog.csdn.net/2301_79811170/article/details/135001954