Introduction to C++ (Part 1)

Table of contents

1.C++

1.1 What is C++

1.2 Development History of C++

2. Introduction to C++

2.1 C++ keywords

2.2 Namespace

2.3 C++ input and output

2.4 Default parameters

2.4.1 Concept of default parameters

2.4.2 Default parameter classification

2.5 Function overloading

2.5.1 Concept of function overloading

2.5.2 Classification of function overloading

 3. Summary


1.C++

1.1 What is C++

C++ (C plus plus) is a high-level computer programming language, which is an extension and upgrade of the C language. It was first developed in 1979 byBjarne Stroustrup(Bjarne Stroustrup--Father of C++ language a> was developed at AT&T Bell Studios. )

C language is a structured and modular language, suitable for processing smaller-scale programs. For complex problems and large-scale programs that require a high degree of abstraction and modeling, C language is not suitable. In order to solve the software crisis, in the 1980s, the computer industry proposed the idea of ​​OOP(object oriented programming: object-oriented), supporting object-oriented programming languages ​​came into being.

In 1982, Dr. Bjarne Stroustrup introduced and expanded the object-oriented concept based on the C language and invented a new programming language. In order to express the origin relationship between the language and the C language, it was named C++. Therefore: C++ is based on C language. It can not only perform procedural programming in C language (compatible with C language), but also perform and Object-based programming characterized by abstract data types can also be carried outObject-oriented programming.

1.2 Development History of C++

In 1979, when Benjani and others at Bell Labs tried to analyze the Unix kernel, they tried to modularize the kernel, so expanded it on the basis of the C language and added The mechanism of classes was completed, and a preprocessing program that could be run was completed, which was called C with classesC with classes a>.

The development of the language is just like the development in "Minecraft", from making a workbench, making tools, and later building your own home, from shallow to deep. Let's take a look at the development history of C++.

2. Introduction to C++

2.1 C++ keywords

C++ (C++98) has a total of 63 keywords, while the C language has 32 keywords. Let’s take a look at what keywords there are.

2.2 Namespace

In C/C++, there are a large number of variables, functions and classes, and the names of these variables, functions and classes will exist in the global scope, which may cause many conflicts. The purpose of using a namespace is to localize the name of the identifier to avoid naming conflicts or name pollution. The namespace keyword appears for this purpose. questionable.

#include <iostream>

//这一行是什么意思呢?
using namespace std;

 I believe that many beginners, like me, never cared about what this line of work means at first, so let’s learn together!

//这一行是什么意思呢?
//using namespace std;
//解读时刻:
			//其中,namespace--命名空间
			//		using namespace--全部展开(授权)命名空间
			//		std--C++标准库的命名空间
			//即:全部展开(授权)命名空间中的std

What is the use of namespace? It is obvious that the problem of naming conflicts in C language can be solved. There is only one solution in C language - change the name! ! ! The namespace in C++ effectively solves the problem of naming conflicts, for example:

#include <iostream>
using namespace std;

//命名冲突
namespace fyd
{
	//命名空间中可以定义变量/函数/类型
	int pow = 1;
}

int main()
{
	// ::--域作用限定符
	cout << fyd::pow << endl;
	int a = 1, b = 2;
	cout << pow(b, a) << endl;

	return 0;
}

Output result:

Obviously, namespaces solve the naming conflict problem. At the same time, there is not just one way to use namespaces, but three.

·Add namespace name and domain qualifier

int main()
{
	// ::--域作用限定符
	cout << fyd::pow << endl;
	return 0;
}

·Use using to introduce a member of the namespace

using fyd::a;
int main()
{
	cout << a << endl;
	return 0;
}

·Use using namespace namespace name introduction

using namespace fyd;
int main()
{
	cout << fyd::a << endl;
	cout << a << endl;
	return 0;
}

2.3 C++ input and output

What are the new faces such as cin, cout, endl, etc. that I use in the namespace? Obviously, C++ uses this to communicate with the world. Let’s learn together!

// std是C++标准库的命名空间名,C++将标准库的定义实现都放到这个命名空间
#include <iostream>
using namespace std;

int main()
{
	int a = 1;
	int b = 0;
	cin >> b;
	cout << a << endl;
	//cin -- Console in
	//cout -- Console out
	// >> -- 流提取运算符
	// << -- 流插入运算符
	// endl -- 换行也可用‘\n’效果一致
	return 0;
}

illustrate:

·When usingcout standard output object (console) and cin standard input object (keyboard), Must include <iostream>header file and use std according to namespace usage.

·cout and cin are global stream objects, and endl is a special C++ symbol that represents newline output. They are both included in the <iostream> header file.

·<< is the stream insertion operator, >> is the stream extraction operator.

·It is more convenient to use C++ for input and output. There is no need to manually control the format like printf/scanf input and output. C++ input and output can automatically identify variable types.

#include <iostream>
using namespace std;

int main()
{
	double x = 1.111;
	double y = 0;
	int a = 1;
	int b = 0;

	//可自动识别变量类型
	cin >> b >> y;

	cout << a << x << endl;
	
	return 0;
}

2.4 Default parameters

2.4.1 Concept of default parameters

The default parameter isSpecify a default value for the function's parameter when declaring or defining a function . When calling this function, if no actual parameter is specified, the default value of the formal parameter is used, otherwise the specified actual parameter is used.

#include <iostream>
using namespace std;

void fyd(double a = 3.1415926)
{
	cout << a << endl;
}

int main()
{
	//没有传参时,使用参数默认值
	fyd();
	//传参时,使用指定实参
	fyd(3.14);
	
	return 0;
}

operation result:

2.4.2 Default parameter classification

·All default parameters

#include <iostream>
using namespace std;

void fyd(double a = 3.1415926, int b = 9, int c = 1)
{
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;
}

int main()
{
	fyd();

	return 0;
}

operation result:

 

·Semi-default parameters

#include <iostream>
using namespace std;

void fyd(double a, int b = 9, int c = 1)
{
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;
}

int main()
{
	fyd(1.11);

	return 0;
}

operation result:

Notice:

·Semi-default parameters must be given in order from right to left and cannot be given at intervals.

·Default parameters cannot appear in both function declaration and definition.

·Default value must be a constant or global variable

·C language is not supported (the compiler does not support it)

2.5 Function overloading

2.5.1 Concept of function overloading

Function overloading is a special case of functions that C++ allows in Declare several functions with similar functions in the same scope functions with the same name , the functions of these functions with the same name The formal parameter list (parameter number or type or type order) is different, and is often used to deal with the problem of different data types when implementing similar functions.

2.5.2 Classification of function overloading

1. Different parameter types

#include <iostream>
using namespace std;

int fyd(int a, int b)
{
	return a + b;
}

double fyd(double a, double b)
{
	return a + b;
}

int main()
{
	int i = 2;
	int j = 3;

	double k = 2.22;
	double l = 3.33;

	cout << fyd(i, j) << endl;
	cout << fyd(k, l) << endl;

	return 0;
}
operation result:

2. The number of parameters is different

#include <iostream>
using namespace std;

int fyd(int a, int b)
{
	return a + b;
}

int fyd(int a = 999)
{
	return a;
}

int main()
{
	int i = 2;
	int j = 3;

	cout << fyd(i, j) << endl;
	cout << fyd() << endl;

	return 0;
}

operation result:

 3. The order of parameter types is different

#include <iostream>
using namespace std;

void fyd(int a, double b)
{
	cout << a + b << endl;
}

void fyd(double a, int b)
{
	cout << a + b << endl;
}

int main()
{
	fyd(2, 1.11);
	fyd(1.11, 2);

	return 0;
}

operation result:

 3. Summary

In the past, for the convenience of playing competitions, I only knew how to use C++, but I didn’t know the relevant concepts and core. From now on, I decided to learn C++ again in a down-to-earth manner. I hope everyone will learn with me!

Guess you like

Origin blog.csdn.net/weixin_74809706/article/details/131759192