Rebirth: I want to learn C++ first day

I was born again, and today I started to re-learn C++ with the memories of the last century

Table of contents

namespace

I/O stream

default parameter (default parameter)

function overloading


namespace

The newly defined namespace is a new solution proposed by C++ to prevent naming conflicts for variables, functions, and classes. It is a specific domain scope defined by itself outside the global scope and belongs to the namespace itself . Each domain does not interfere with each other and has clear boundaries. Use to avoid naming conflicts .

#include<iostream>
using namespace std;
namespace myspace
{
    int a=20;//这个a在命名空间中
    void func()//这个函数在命名空间中
    {
        cout<<"is my func"<<endl;
    }
}
int a=10;//这个a在全局作用域中
void func()//此函数在全局作用域中
{
    cout<<"is your func"<<endl;
}
int main()
{
    cout<<a<<endl;
    func();
    return 0;
}

The output of the above figure is

 From this we can see that the global scope and the namespace do not interfere with each other, and the boundaries are clear. By default, variables, functions, and classes in the global scope are used.

So how to use variables, functions, and classes in the namespace?

The use of namespaces is divided into three types: 1. Expand all namespaces 2. Partially expand namespaces 3. Specified use

The following are explained in turn:

Full expansion of the namespace: directly expand the variables, functions, and classes in the namespace to the scope of the global scope, so that the code in the global scope can directly use the content defined in the namespace. But this method is more dangerous, and it is easy to be the same as the variable, function, and class name in the original global scope, causing contradictions.

#include<iostream>
using namespace std;
namespace myspace
{
    int a = 20;//这个a在命名空间中
    void func()//这个函数在命名空间中
    {
        cout << "is my func" << endl;
    }
}
using namespace myspace;//全部展开方式:using+namespace+命名空间名
int main()
{
    cout << a << endl;//使用的是命名空间的变量和函数
    func();
    return 0;
}

Conflict : If there are the same variables, functions, and class names in the global scope and in the namespace, they cannot be directly expanded, otherwise there will be conflicts with the same name.

Partial expansion of namespace: Partial expansion of namespace is to expose a variable, function, or class in the namespace to the global scope. Thus using the namespace.

#include<iostream>
using namespace std;
namespace myspace
{
    int a = 20;//这个a在命名空间中
    void func()//这个函数在命名空间中
    {
        cout << "is my func" << endl;
    }
}
using myspace::a;//部分展开规则:using+命名空间名+::+要使用的变量或函数或类
int main()
{
    cout << a << endl;//这里只能访问到a,不能访问到func()
    return 0;
}

Specified use of the namespace: The specified use of the namespace does not expand any variables, functions, or classes in the namespace. Directly specify the use where it is to be used, and specify it in every place where it is used. This method is the most cumbersome, but it is the safest, and there is no hidden danger of duplicate names. 

#include<iostream>
using namespace std;
namespace myspace
{
    int a = 20;//这个a在命名空间中
    void func()//这个函数在命名空间中
    {
        cout << "is my func" << endl;
    }
}
int main()
{
    cout << myspace::a << endl;//指定使用规则:指定的命名空间名+::+变量或函数或类
    myspace::func();
    return 0;
}

Automatic merging of namespaces

In two namespaces of two different files, or two namespaces of the same file, if the names of the two namespaces are the same, the namespaces will be automatically merged.

For example, in a simplest C++ program,

#include<iostream>
#include<stdlib.h>
using namespace std;//合并后共有的std标准命名空间全部展开
int main()
{
    cout<<"hello world"<<endl;
    return 0;
}

Two header files are included here, but they all share the standard namespace of std. Their two respective std namespaces will be merged into a common namespace std.

How to apply std reasonably?

In daily practice, just use namespace std to expand all the standard namespaces. But in large-scale projects, it should be expanded carefully to prevent the names in std from being the same as other global variables, functions, and class names in the project.

I/O stream

Only the most basic C++ input and output are introduced here

#include<iostream>
using namespace std;
int main()
{
	int a = 0;
	char ch;
	cout << "请输入a" << endl;
	cin >> a;
	cout<< "请输入ch" << endl;
	cin >> ch;
	cout << "a=" <<a << endl;
	cout << "ch=" << ch << endl;
	
	return 0;
}
illustrate:
1. When using cout standard output object (console out console output ) and cin standard input object (console in console input ) , you must include the < iostream > header file, and use std (standard namespace) according to the namespace usage method.
2. cout and cin are global stream objects, and endl is a special C++ symbol that represents newline output. They are all included in the <
iostream > header file.
3. << is a stream insertion operator and >> is a stream extraction operator .
4. It is more convenient to use C++ for input and output, and does not need to manually control the format like printf/scanf input and output.
The input and output of C++ can automatically identify the variable type.
5. In fact, cout and cin are objects of type ostream and istream respectively , and >> and << also involve operator overloading and other knowledge

default parameter (default parameter)

The default parameter is to specify a default value for the parameter of the function when declaring or defining the function . When calling this function, if no real
parameter, the default value of the formal parameter is used, otherwise the specified actual parameter is used. as follows
#include<iostream>
using namespace std;
void func(int x=2)
{
    cout<<x<<endl;
}
int main()
{
    func();//没有指定实参,使用缺省值2
    func(1);//指定实参则使用指定实参
    return 0;
}

 

Full default and half default

All default: all function parameters are default parameters. Semi-default: Some parameters of the function are default parameters.

Notice: 

1. The semi-default parameters must be given sequentially from right to left , and cannot be given alternately.
void Func(int a, int b = 10, int c = 20)
 {
     cout<<"a = "<<a<<endl;
     cout<<"b = "<<b<<endl;
     cout<<"c = "<<c<<endl;
 }
2. Default parameters cannot appear in function declaration and definition at the same time. If the statement is given, the definition cannot be given.
#include<iostream>
using namespace std;
void func(int x = 2);//函数声明已给缺省参数
void func(int x)//函数定义,这里不可以是缺省参数(int x=2)
{
    cout << x << endl;
}
int main()
{
    func();
    func(1);
    return 0;
}

function overloading

Function overloading: It is a special case of functions. C++ allows several functions with the same name to declare similar functions in the same scope .
The formal parameter list ( parameter number or type or type order ) of some functions with the same name is different , which is often used to deal with data types that implement similar functions
different questions.
1. The number of parameters is different:
#include<iostream>
using namespace std;
void func()
{
    cout << "null" << endl;
}
void func(int x)
{
    cout << x << endl;
}
int main()
{
    func();//调用无参func
    func(1);//调用有参func
    return 0;
}

2. Different parameter types

#include<iostream>
using namespace std;
void swap(int* a,int* b)
{
    int tmp = *a;
    *a = *b;
    *b = tmp;
}
void swap(char* a, char* b)
{
    char tmp = *a;
    *a = *b;
    *b = tmp;
}
int main()
{
    int a = 2, b = 3;
    char c = 'j', d = 'k';
    swap(a, b);//调用int,int的swap
    swap(c, d);//调用char,char的swap
    cout << "a=" << a << " "<< "b=" << b << endl;
    cout << "c=" << c <<" " << "d=" << d << endl;
    return 0;
}

 3. The order of types is different

#include<iostream>
using namespace std;
void f(int a, char b)
{
	cout << "f(int a,char b)" << endl;
}
void f(char b, int a)
{
	cout << "f(char b, int a)" << endl;
}
int main()
{
	f(2, 'a');//调用int char的f函数
	f('a', 2);//调用char int的f函数
	return 0;
}

Guess you like

Origin blog.csdn.net/2301_76144863/article/details/131783983