叁-拾玖|c++入门笔记
c++初識
cout在std命名空間下
換行endl;
system("pause");//阻塞 return EXIT_SUCCESS;返回正常退出 #define _CRT_SECURE_NO_WARNINGS //當使用老的不安全函數仍然能編譯通過面向對象三大特性:封裝、繼承、多態
雙冒號作用域運算符
::代表作用域 如果前面什么都不添加 代表全局作用域
cout<<"atk="<<atk<<std::endl; //atk為局部變量,endl為std作用域下的變量 cout<<"全局atk="<<::atk<<std::endl; //atk為全局變量。namespace命名空間
為了避免二義性,解決名稱沖突,會在頭文件里將同名函數定義到命名空間下即可。
//game1.h內部分代碼 ··· namespace KingGlory {void goAtk(); } ··· //game2.h內部分代碼 ··· namespace LOL {void goAtk(); } ··· //game1.cpp內部分代碼 #include"game1.h" #include"game2.h" ··· void KingGlory::goAtk() {cout << "王者榮耀攻擊實現" << endl; } void LOL::goAtk() {cout << "LOL攻擊實現" << endl;}命名空間下可以存放 : 變量、函數、結構體、類…
命名空間必須要聲明在全局作用域
不可以在函數體內使用命名空間
命名空間可以嵌套命名空間
··· namespace B {int m_A = 10;namespace C{int m_A = 20;} } void test03() {cout << "B空間下的m_A = " << B::m_A << endl;cout << "C空間下的m_A = " << B::C::m_A << endl; } ···命名空間是開放的,可以隨時將新成員添加到命名空間下
再聲明命名空間,作用是合并而不是覆蓋。
命名空間可以匿名的
匿名命名空間是全局的,相當于是聲明了靜態變量。
命名空間可以起別名
namespace veryLongName {int m_E = 10000; } void test06() {namespace veryShortName = veryLongName;cout << veryShortName::m_E << endl;cout << veryLongName::m_E << endl;//兩個名都可以找到命名空間的變量 }using聲明
void test01() {int sunwukongId = 2;//1、using聲明//using KingGlory::sunwukongId ; //當using聲明與 就近原則同時出現,出錯,盡量避免//就近原則:先選擇調用函數體內的,再選擇調用函數體外的cout << sunwukongId << endl;}using編譯指令
void test02() {//int sunwukongId = 2;//2、using編譯指令using namespace KingGlory;using namespace LOL;//當using編譯指令 與 就近原則同時出現,優先使用就近//當using編譯指令有多個,需要加作用域 區分cout << KingGlory::sunwukongId << endl;cout << LOL::sunwukongId << endl; }C++對C語言的增強和拓展
1、全局變量監測增強
int a; int a = 10;//c++中會報錯重復定義C++檢測出重定義,而C監測不出來。
2、函數監測增強
getRectS( w , h)//c中不會報錯 {return w *h; } void test01() {printf("%d\n", getRectS(10, 10, 10)); } int getRectS(int w,int h)//c++中必須加上形參類型。 {return w *h; } void test01() {printf("%d\n", getRectS(10, 10)); }c中返回值不能檢測,形參類型不能檢測,函數調用個數不能檢測,而c++中這些都可以實現。
3、類型轉換監測增強
void test02() {char * p = malloc(64);//c中不會報錯 } void test02() {char * p = (char *)malloc(64);//cpp中需要強制類型轉換 }4、struct增強
c++可以放函數,創建結構體變量,可以簡化關鍵字 struct,而c不可以
struct Person {int age;void func()//C中會報錯{age++;} };5、bool類型拓展
C語言下,沒有bool類型,而c++下有。
bool flag = true; //bool類型 代表 真和假 true ----真(1) false ----假(0)void test04() {cout << sizeof(bool) << endl; //結果是1個字節//flag = false;//flag = 100; //將非0的數都為1,false為0;cout << flag << endl; }6、三目運算符增強
void test05() {//?:int a = 10;int b = 20;printf("ret = %d\n", a > b ? a : b);(a < b ? a : b )= 100; // C++下返回的是變量 b = 100printf("a = %d\n", a);printf("b = %d\n", b); }7、const增強
全局const
c++與c結論一致
const int m_A = 100;局部const
void test06() {//m_A = 200;//int * p = (int *)&m_A;//*p = 200;//局部constconst int m_B = 100;//m_B = 200;int * p = (int *)&m_B;//修改失敗*p = 200;cout << "m_B = " << m_B << endl;int arr[m_B]; //C++下const修飾的變量稱為常量}外部鏈接屬性
C語言下的const修飾全局變量默認是外部鏈接屬性
//test.c const int g_a = 1000; //main.c #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<stdlib.h> //外部鏈接屬性: int main(){extern const int g_a;//告訴編譯器在外部有一個g_a //報錯:一個無法解析的外部命令:鏈接出的錯printf("g_a = %d\n", g_a);system("pause");return EXIT_SUCCESS; }而C++下的const修飾的全局變量默認是內部鏈接屬性
//test.cpp extern const int g_b = 1000;//可以加關鍵字extern提高作用域 //const int g_b=1000;//默認是內部鏈接屬性 //main.cpp #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std;int main(){extern const int g_b;cout << "g_b = " << g_b << endl;;system("pause");return EXIT_SUCCESS; }const分配內存情況
1、 對const變量取地址,會分配臨時內存
void test01() {const int a = 10;//改不掉,因為沒有分配內存int * p = (int *)&a; }2、使用普通變量初始化const變量的時候
void test02() {int a = 10;const int b = a;//b分配到棧上//如果把a換成10就修改不成功,因為這時候把b放在符號表中,沒有分配內存,沒有地址//然后分配了一個臨時變量temp給b,讓指針p指向bint *p = (int *)&b;//定義一個一級指針儲存b的地址*p = 1000;//指針p指向的變量修改為1000//分配內存了,所以修改成功 cout << "b = " << b << endl;}3、對于自定義的數據類型
struct Person {string m_Name;//需要包含頭文件int m_Age; }; void test03() {const Person p={"xiaoming",20};//const變量必須初始化//p.m_Age = 10;//直接修改是失敗的Person * pp = (Person *)&p;(*pp).m_Name = "Tom";//pp是一個指針pp->m_Age = 10;cout << "姓名: " << p.m_Name << " 年齡: " << p.m_Age << endl; }盡量用const替換define
//define MAX 1024 //undifine 卸載宏 //const int max =1024;//const是有類型的而define沒有類型,const有作用域,define沒有作用域引用的基本語法
引用的目的:起別名
引用的基本語法:
類型 &別名 = 原名;
引用必須初始化,并且引用一旦初始化后就不可以引向其他向量。
對數組建立引用
1、直接建立引用
int arr[10];int(&pArr)[10] = arr;for (int i = 0; i < 10; i++){arr[i] = 100 + i;}for (int i = 0; i < 10; i++){cout << pArr[i] << endl;}//用原名方式賦值,別名方式打印2、先定義數組類型,再通過類型定義引用
typedef int(ARRAY_TYPE)[10];//創建一個10個元素數組類型//類型 &別名 = 原名ARRAY_TYPE & pArr2 = arr;//類型 ARRAY_TYPE 別名 pArr2 //別名可以起多個for (int i = 0; i < 10; i++){cout << pArr2[i] << endl;}參數的傳遞方式
1、值傳遞
void mySwap01(int a , int b) {int temp = a;a = b;b = temp;/*cout << ":::a = " << a << endl;cout << ":::b = " << b << endl;*/ } //只能修改函數里的ab,不能修改函數外的ab2、地址傳遞
void mySwap02(int *a, int *b) {int temp = *a;*a = *b;*b = temp; }3、引用傳遞
void mySwap03(int &a , int &b) // int &a = a; int &b = b; {int temp = a;a = b;b = temp; }引用注意事項
1、引用必須引一塊合法內存空間
void test02() {//int &a = 10;2、不要返回局部變量的引用
int &ref = func();cout << "ref = " << ref << endl;cout << "ref = " << ref << endl;//指向了一個局部變量//第一次可以保留,第二次就釋放掉了 }3、函數返回值是引用的時候,可以當做左值去計算
int& func2() {static int a = 10;//沒有被釋放掉return a; }void test03() {int &ref = func2();cout << "ref = " << ref << endl;cout << "ref = " << ref << endl;cout << "ref = " << ref << endl;cout << "ref = " << ref << endl;//當函數的返回值是引用,可以作為左值去計算func2() = 1000;cout << "ref = " << ref << endl;}指針引用
p指向指針的指針 *p 指向的是person本體 **p person本體
struct Person {int age; };void allocateSpace(Person ** p) { *p = (Person *)malloc(sizeof(Person));//malloc返回的是void *,然后強制轉換為一個Person*(*p)->age = 10;}void test01() {Person * p = NULL;//定義一個空指針allocateSpace(&p);//通過一段代碼給它分配內存cout << "p.age = " << p->age << endl; } //利用引用可以簡化指針 //可以直接用同級指針的引用給同級指針分配空間malloc動態內存空間分配函數,如果分配成功:則返回指向被分配內存空間的指針。不然,返回空指針NULL。malloc只管分配內存,并不能對其進行初始化。所以得到的一片新內存中,其值將是隨機的。一般意義上:我們習慣性的將其初始化為NULL。當然,也可以用memset函數的。void *類型可以強轉為任何其他類型的的指針。malloc則必須由我們計算字節數,并且在返回的時候強轉成實際指定類型的指針。
引用的自動轉換
#include<iostream> using namespace std; //發現是引用,轉換為 int*const ref = &a void testFunc(int& ref){ref=100; }int main() {int a = 10;int& aRef = a;//自動轉換為int* const aRef = &a;//這也能說明引用為什么必須初始化aRef = 20;//內部發現aRed是引用,自動幫我們轉換為*aRef = 20;cout<<"a:"<<endl;cout<<"aRef:"<<aRef<<endl;testFunc(a);return EXIT_SUCCESS; } void test01() {//int &ref = 10;//引用必須要有一個合法空間const int &ref = 10; // 加了const之后,相當于寫成//int temp = 10;//const int &ref = temp;int *p = (int *)&ref;// int* = const int**p = 10000;cout << ref << endl;//指針修改臨時內存 }void showValue(const int &a)//防止修改掉a {//a = 100000;//把外面的a修改掉了,因為引用指向的是外面的acout << "a = " << a << endl;}常量引用的使用場景 修飾函數中的形參,防止誤操作
void test02() {int a = 100;showValue(a);}總結
以上是生活随笔為你收集整理的叁-拾玖|c++入门笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 北方工商管理研修学院计算机,北方工商管理
- 下一篇: C++语法(二十)常函数、常对象