?? 第一個 c++ 代碼
?? 例1:
#include <iostream>
using namespace std;
int main() {
cout << "hello world" << endl;
return 0;
}
-
#include <iostream>
標準輸入輸出 -
std
是c++
標準庫的命名空間,將標準庫的定義實現(xiàn)都放到這個命名空間中 -
using namespace std
展開std
里的內(nèi)容 -
cout
?c
代表的是console
控制臺的意思,out
有輸出的意思 -
<<
流運算符 流插入 -
endl
等價于'\n'
?? 命名空間
在 c++
中,變量、函數(shù)和類的名稱存在于全局作用域中,可能會導(dǎo)致沖突。比如:在 #include <stdlib.h>
中存在一個 rand
函數(shù),當你再定義一個全局的 rand
變量這時編譯器就會報錯。而使用命名空間將變量、類型、函數(shù)放在一個域中以放命名沖突,使用 namespace
關(guān)鍵字來創(chuàng)建命名空間。
?? 命名空間的定義和訪問
語法: namespace name {}
?? 例2:
// 定義命名空間
namespace mySpace {
char name[10] = "sunwukong";
int age = 18;
char gender = 'male';
int add(int num1, int num2) {
return num1 + num2;
}
struct Node {
int val;
struct Node* next;
};
}
// 命名空間的訪問
int main() {
struct mySpace::Node node;
node.val = 10;
cout << mySpace::age << endl;
cout << mySpace::add(10, 20) << endl;
return 0;
}
ps:
若是變量或者函數(shù)直接使用命名空間的 name::
來訪問即可。但是如果是結(jié)構(gòu)類型 mySpace::struct Node
這樣的寫法是 error
的。要在 struct
后面寫命名空間 struct mySpace::Node
。
?? 命名空間可以嵌套
namespace N1 {
int a;
int b;
namespace N2 {
int c;
int d;
}
}
ps:
還有一種情況,若在許多文件中存在多個相同的命名空間,編譯器最后會合成為同一個命名空間。
?? 例3:
#include <iostream>
using namespace std;
int num = 10;
int main() {
int num = 1;
cout << num << endl;
return 0;
}
ps:
以上代碼會有局部優(yōu)先的問題,所以最終 num
訪問結(jié)果是 1
,但是也可以訪問到全局的 num
使用 ::
默認代表訪問全局域 ,例如:cout << ::num << endl;
。::
前面加上命名空間的名稱代表訪問命名空間的某個對象。
?? 命名空間的使用
-
name::a
加命名空間名稱及作用域限定符 -
using name::a
使用using
將命名空間中某個成員引入 -
using namespace name
使用using namespace
引入命名空間
?? c++ 輸入輸出
?? 例4:
#include <iostream>
using namespace std;
int main() {
int num = 0;
double score = 0.0;
cin >> num >> score;
cout << num <<" " << score << endl;
return 0;
}
-
cin、cout
?c
代表console
有控制臺的意思in
有內(nèi)的意思out
有外的意思,cin
標準輸入對象(鍵盤)、cout
標準輸出對象(控制臺),必須包含<iostream>
以及命名空間std
-
cout
和cin
是全局的的流對象,它們都包含在<iostream>
頭文件中 <<
是留插入運算符、>>
是流提取運算符-
c++
的輸入輸出可以自動識別類型
?? 函數(shù)的缺省參數(shù)
缺省參數(shù)是聲明或者定義函數(shù)時為函數(shù)的參數(shù)指定一個默認值。在調(diào)用函數(shù)時,如果沒有指定實參則采用默認值(缺省參數(shù)),否則使用指定實參。
?? 例5:
#include <iostream>
using namespace std;
void Func(int num = 10) {
cout << num << endl;
}
int main() {
Func(); // 10
Func(20); // 20
return 0;
}
- 全缺省參數(shù)
#include <iostream>
using namespace std;
void Func(int a = 10, int b = 20, int c = 30) {
cout << a << endl;
cout << b << endl;
cout << c << endl << endl;
}
int main() {
Func();
Func(100);
Func(100 , 200);
Func(100 , 200 , 300);
return 0;
}
- 半缺省參數(shù)
void Func(int a , int b = 20, int c = 30) {
cout << a << endl;
cout << b << endl;
cout << c << endl << endl;
}
ps:
半缺省參數(shù)必須從右向左依次來給出,不可以隔著給。缺省參數(shù)不能在函數(shù)聲明和定義中同時出現(xiàn)(一般在聲明中出現(xiàn))。文章來源:http://www.zghlxwxcb.cn/news/detail-615846.html
?? 函數(shù)重載
c++
允許在同一個作用域中聲明功能類似的同名函數(shù),這些同名函數(shù)的形參類型(參數(shù)個數(shù)、類型、順序)不同, 常用來處理功能類似但數(shù)據(jù)類型不同的問題。文章來源地址http://www.zghlxwxcb.cn/news/detail-615846.html
// 類型不同
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
// 順序不同
void f(int a, char b) {
cout << "void f(int a, char b)" << endl;
}
void f(char a , int b) {
cout << "void f(char b , int a)" << endl;
}
// 個數(shù)不同
void f() {
cout << "void f()" << endl;
}
void f(int a) {
cout << "void f(int a)" << endl;
}
到了這里,關(guān)于C++基礎(chǔ)知識 (命名空間、輸入輸出、函數(shù)的缺省參數(shù)、函數(shù)重載)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!