欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++入門基礎(chǔ)之命名空間、輸入輸出和缺省參數(shù)

 更新時(shí)間:2023年01月10日 11:22:39   作者:我的代碼愛(ài)吃辣  
C++入門基礎(chǔ)篇的內(nèi)容為C++的基本特性,只有在掌握C++的基本特性后,是進(jìn)入后面類和對(duì)象學(xué)習(xí)的基礎(chǔ),下面這篇文章主要給大家介紹了關(guān)于C++入門基礎(chǔ)之命名空間、輸入輸出和缺省參數(shù)的相關(guān)資料,需要的朋友可以參考下

一.命名空間

在C/C++中,變量、函數(shù)和后面要學(xué)到的類都是大量存在的,這些變量、函數(shù)和類的名稱將都存

在于全局作用域中,可能會(huì)導(dǎo)致很多沖突。使用命名空間的目的是對(duì)標(biāo)識(shí)符的名稱進(jìn)行本地化,以避免命名沖突或名字污染,namespace關(guān)鍵字的出現(xiàn)就是針對(duì)這種問(wèn)題的。

例如:

#include<stdio.h>
 
int rand = 10;
int main()
{
	int a = 10;
	return 0;
}

這時(shí)候代碼沒(méi)有任何問(wèn)題。

#include<stdio.h>
#include<stdlib.h>
 
int rand = 10;
int main()
{
	int a = 10;
	return 0;
}

出現(xiàn)這個(gè)問(wèn)題,我們知道,在頭文件<stdlib.h>里面有一個(gè)函數(shù)rand(),所以頭文件展開(kāi)后就會(huì)出現(xiàn)定義沖突的現(xiàn)象。在C++中為了避免這種,利用命名空間。

(1)命名空間的定義

定義命名空間,需要使用到namespace關(guān)鍵字,后面跟命名空間的名字,然后接一對(duì){}即可,{}
中即為命名空間的成員。

#include<stdio.h>
 
namespace ytt
{
	int a = 5;
}
 
int a = 10;
int main()
{
	printf("%d", a);
	return 0;
}

這樣就不會(huì)有定義沖突的問(wèn)題了,那如果我們想訪問(wèn),值為5的那個(gè)變量a,又要怎么辦呢?

(2)命名空間的使用

如果直接訪問(wèn)是訪問(wèn)不到的。

1.訪問(wèn)方法

命名空間的名稱 +  :: + 命名空間內(nèi)的變量或者函數(shù)。

namespace ytt
{
	int a = 5;
	int Add(int a, int b)
	{
		return a + b;
	}
}
int main()
{
	printf("a=%d\n", ytt::a);
	printf("4+5=%d\n", ytt::Add(4 , 5));
	return 0;
}

 2.命名空間的嵌套

namespace ytt
{
	int Add(int a, int b)
	{
		return a + b;
	}
	namespace wq
	{
		int Max(int a, int b)
		{
			return a > b ? a : b;
		}
	}
}
int main()
{
	printf("Max=%d\n", ytt::wq::Max(10, 15));
	return 0;
}

嵌套的情況下,就是一層一層訪問(wèn):

(3)全局域

訪問(wèn)全局域,只需要  :: +  全局變量

namespace ytt
{
	int a = 10;
}
int a = 5;
int main()
{
	int a = 1;
	//局部a
	printf("a=%d\n", a);
	//全局a
	printf("a=%d\n", ytt::a);
	//命名空間內(nèi)的a
	printf("a=%d\n", ::a);
	return 0;
}

 二.輸入&&輸出

C++的輸入輸出是函數(shù):cin,cout,被包含在頭文件 <iostream> 中。

(1) cout

#include<iostream>
int main()
{
	std::cout << "hello world" << std:: endl;
	std::cout << "hello world\n";
    return 0;
}

<<是流插入運(yùn)算符

有了前面,命名空間的學(xué)習(xí),我們也就能看出來(lái)了,cout 也是被封裝到命名空間 std里面的,endl 是封裝在 std 里面的換行符,和 ' \n '是一樣的。

(2)cin

#include<iostream>
int main()
{
	int a = 0;
	std::cin >> a;
	std::cout << "a = " << a;
    return 0;
}

>>是流提取運(yùn)算符,cin 也是被封裝到命名空間 std里面的。

實(shí)際上cout和cin分別是 ostream 和 istream 類型的對(duì)象,>>和<<也涉及運(yùn)算符重載等知識(shí),這些知識(shí)我們我們后續(xù)才會(huì)學(xué)習(xí),所以我們這里只是簡(jiǎn)單學(xué)習(xí)他們的使用。后面我們還有有一個(gè)章節(jié)更深入的學(xué)習(xí)IO流用法及原理。 

(3)cin cout自動(dòng)是識(shí)別類型

cin cout 相對(duì)于 C語(yǔ)言的scnaf printf,少了類型的修飾,所以在C++中,cin 和 cout是可以自動(dòng)識(shí)別類型的。

int main()
{
	int a = 0;
	double b = 0;
	char c = 0;
	std::cin >> a >> b >> c;
	std::cout << a <<std::endl;
	std::cout << b << std::endl; 
	std::cout << c << std::endl;
	return 0;
}

 三.命名空間的展開(kāi)

(1)使用using namespace 命名空間名稱引入

我們?cè)趯懗绦虻臅r(shí)候,有時(shí)候會(huì)發(fā)生,某個(gè)命名空間的變量名,函數(shù)名,經(jīng)常被使用,我們每一次使用都要加上命名空間,會(huì)非常麻煩。所以我們使用using namespace 命名空間名稱引入。

#include<iostream>
namespace ytt
{
	int a = 0;
	int b = 2;
}
using namespace ytt;
using namespace std;
int main()
{
	cout << a << endl;
	cout << b << endl;
    return 0;
}

這樣使用就是將命名空間的所有定義全部展開(kāi),這樣雖然使得我們不用每次都去包含命名空間,到那時(shí)也使得我們辛辛苦苦建立的命名空間也就沒(méi)有了意義。因?yàn)槎荚谶@里展開(kāi)了,就會(huì)發(fā)生定義相同的沖突。所以這種使用方法在企業(yè)開(kāi)發(fā)時(shí)禁止的,我們平時(shí)練習(xí)代碼時(shí),為了方便可以使用。

(2)使用using將命名空間中某個(gè)成員引入

上述使用使用using namespace 將整個(gè)命名空間展開(kāi),會(huì)有造成沖突的可能,我們還可以將命名空間的某一成員引入。

#include<iostream>
namespace ytt
{
	int a = 0;
	int b = 2;
}
using ytt::a;
using ytt::b;
using std::cout;
using std::endl;
int main()
{
	cout << a << endl;
	cout << b << endl;
    return 0;
}

四.缺省參數(shù)

(1)缺省參數(shù)概念

缺省參數(shù)是聲明或定義函數(shù)時(shí)為函數(shù)的參數(shù)指定一個(gè)缺省值。在調(diào)用該函數(shù)時(shí),如果沒(méi)有指定實(shí)參則采用該形參的缺省值,否則使用指定的實(shí)參。

#include<iostream>
using std::cout;
using std::endl;
void fun(int a = 10)
{
	cout << a << endl;
}
int main()
{
	fun();  //沒(méi)傳參數(shù),使用缺省參數(shù)
	fun(100);//傳了參數(shù),就使用傳的參數(shù)
	return 0;
}

(2)缺省參數(shù)分類

1.全缺省參數(shù)

void Func(int a = 10, int b = 20, int c = 30)
{
    cout<<"a = "<<a<<endl;
    cout<<"b = "<<b<<endl;
    cout<<"c = "<<c<<endl;
}

調(diào)用時(shí):

void Func(int a = 10, int b = 20, int c = 30)
{
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;
}
int main()
{
	Func();         //true
	Func(1);        //true
	Func(1, 2);     //true
	Func(1, 2, 3);  //true
	Func(, 1, 2);   //error
    Func(1, , 3);   //error
	return 0;
}

 帶有缺省參數(shù)的函數(shù),傳參數(shù)時(shí)必須從左往右連續(xù),不能跳著給參數(shù)。

2.半缺省參數(shù)

void Func(int a, int b = 10, int c = 20)
{
    cout<<"a = "<<a<<endl;
    cout<<"b = "<<b<<endl;
    cout<<"c = "<<c<<endl;
}

3.注意:

1. 半缺省參數(shù)必須從右往左依次來(lái)給出,不能間隔著給

2. 缺省參數(shù)不能在函數(shù)聲明和定義中同時(shí)出現(xiàn)

#include<iostream>
 
using std::cout;
using std::endl;
//函數(shù)聲明
void Func(int a = 10, int b = 20, int c = 30);
int main()
{
	Func();         //true
	return 0;
}
//函數(shù)定義
void Func(int a = 10, int b = 20, int c = 30)
{
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;
}

 3. 缺省值必須是常量或者全局變量

五.最后

到此這篇關(guān)于C++入門基礎(chǔ)之命名空間、輸入輸出和缺省參數(shù)的文章就介紹到這了,更多相關(guān)C++命名空間、輸入輸出和缺省參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論