1. 程式人生 > >C++七日成蝶筆記(一)

C++七日成蝶筆記(一)

目錄

1. C++ I/O方式

2. 名字空間的定義

3. 資料型別新成員

(1)C++ 新型別 —— 布林型別 bool

(2)隨用隨定義

4. 引用

(1)普通型別引用的定義與使用

(2)指標型別引用的定義與使用

(3)引用作為函式引數

5. 函式的預設值以及過載

(1)預設值術語

 (2)函式預設值

(3)作用域術語

(4)函式過載

6. 記憶體管理

(1)記憶體管理

(2)檔案的讀寫

(3)檔案開啟方式

7.關鍵字 const

(1)const 修飾普通資料型別

(2)const 修飾指標型別

(3)const 修飾函式引數


 


1. C++ I/O方式

輸入過程:輸入裝置 → 輸入流 → scanf/cin → 變數

輸出過程:變數 → printf/cout → 輸入流 → 輸出裝置

輸入/輸出流的語法:

1. cout 語法的形式:

cout << a << endl;

2. cin 語法的形式:

cin >> a;
cin >> a >> b;

【注】與C語言相比的便利之處:

1) 不用關注佔位符

2) 不用關注資料型別

【注】使用 cin、cout 必須增加標頭檔案:

#include <iostream>

2. 名字空間的定義

關鍵詞:namespace

不同的名字空間(A,B)內允許有相同名稱的變數(x,y)、函式( fun() )等。

namespace A
{
    int x;
    int y;
    void fun();
    void fun1();
}

namespace B
{
    int x;
    int y;
    void fun();
    void fun2();
}

【注】呼叫名字空間的語句:

using namespace A;

【注】呼叫名字空間內變數以及函式的方法:

A :: x;
A :: fun();

3. 資料型別新成員

資料型別:

1. 基本型別:

整型 int;字元型:char;實型:單精度 float、雙精度 double、常雙精度 long double;

2. 構造型別:

陣列型別;結構型別 struct;聯合型別 union;列舉型別 enum

3. 指標型別

4. 空型別(無值型別)void

(1)C++ 新型別 —— 布林型別 bool

真:true   1;假:false   0;

bool flag = false;
if (flag)
{
    //to do
}
else
{
    //to do
}

布林(bool)型別的輸出方式:

int main (void)
{
    bool flag = false;
    cout << flag <<endl; //輸出 0
    cout << boolalpha << flag << endl; //輸出 false
    return 0;
}

(2)隨用隨定義

int main (void)
{
    cout << " x = ";
    int x = 0;
    cin >> x;
    cout << " y = ";
    int y = 0;
    cin >> y;
    cout << " x+y = " << x+y ;
    return 0;
}

4. 引用

C++ 中的引用就是變數的別名。

(1)普通型別引用的定義與使用

int maozi = 3;
int &mz = maozi;
mz = 4;
cout << maozi; //輸出 4
maozi = 6;
cout << mz; //輸出 6

【注】無論是操作別名還是變數,只要其中一個發生變化另一個也隨之發生變化。

(2)指標型別引用的定義與使用

bool *p = NULL;   //定義布林型別指標變數 p
bool *&q = p;     //為布林型別指標 p 取別名 q
bool x = false;
bool *p = &x;
bool *&q = p;
x = true;
cout << boolalpha << *p;    //輸出 true
cout << boolalpha <<*q;     //輸出 true
*q = false;
cout << boolalpha << x;     //輸出 false
*p = true;
cout << boolalpha <<*q;     //輸出 true

(3)引用作為函式引數

void fun(int &a, int &b)
{
    int c = 0;
    c = a;
    a = b;
    b = c;
}
int x = 10, y = 20;
fun(x, y);

5. 函式的預設值以及過載

(1)預設值術語

預設值 default / value

 (2)函式預設值

void fun (int i, int j = 5, int k = 10)
{
    cout << " i = " << i << " j = " << j << " k = " << k << endl;
}
int main ()
{
    fun (20);            //輸出:i = 20 j = 5 k = 10
    fun (20, 30);        //輸出:i = 20 j = 30 k = 10
    fun (20, 30, 40);    //輸出:i = 20 j = 30 k = 40
    return 0;
}

【注】

1. 有預設引數值的引數必須在引數表的最右端。

int f(int a, int b = 0, int c); // 錯誤
int f(int a, int b = 0, int c = 0); // 正確

2. 宣告和定義同時給出預設值,有些編譯器會報錯,有些不會。所以最好只在函式宣告時給出預設值。

程式碼演示:

#include <iostream>
#include <stdlib.h>
using namespace std;
void fun(int x, int y = 4, int z = 1);

int main(void)
{
	int a = 10;
	int b = 15;
	fun(a, b);
	system("pause");
	return 0;
}

void fun(int x, int y, int z)
{
	cout << "x = 0" << x << endl;
	cout << "y = 0" << y << endl;
	cout << "z = 0" << z << endl;
}

(3)作用域術語

void fun1 () //作用域為 fun1
{
    int x;
}
void fun2 () //作用域為 fun2
{
    char x;
}

(4)函式過載

同一作用域下名字相同,引數不同的多個函式。

1)名字相同,引數的個數不同:

void fun ()
void fun (int x)
void fun (int x, int y)

2)名字相同,引數型別不同:

void fun (int *p)
int fun (int &p)
int fun (int x = 0)

過載總結:

1)過載函式的引數格式,引數型別,引數順序3者中必須至少有一種不同,返回值不同不作為過載依據。

2)過載函式的功能應該相近,不為過載而過載。

3)main() 函式不能過載。

程式碼演示:

#include <iostream>
#include <stdlib.h>
using namespace std;

int max1(int x, int y);
int max1(int x, int y, int z);
int max1(int *p, int *q);

int main(void)
{
	int a = 10;
	int b = 5;
	int c = 7;
	cout << max1(b, c) << endl;    //輸出:7
	cout << max1(a, b, c) << endl; //輸出:10
	cout << max1(&a, &c) << endl;  //輸出:10

	system("pause");
	return 0;
}

int max1(int x, int y)
{
	return x > y ? x : y;
}
int max1(int x, int y, int z)
{
	if (x > y)
	{
		if (x > z)
			return x;
		else
			return z;	
	}
	else
	{
		if (y > z)
			return y;
		else
			return z;
	}
}

int max1(int *p, int *q)
{
	if (*p > *q)
		return *p;
	else
		return *q;
	//return *p > *q ? *p : *q;
}

6. 記憶體管理

記憶體的分類:

(1)棧區(stack):有編譯器自動分配釋放;

(2)全域性區/靜態區(static):存放全域性變數、靜態資料、常量;

(3)文字常量區:常量字串存放在這裡,程式結束後由系統釋放;

(4)長鬚程式碼區:存放函式體的二進位制程式碼;

(5)堆區(heap):程式設計師申請/釋放。

(1)記憶體管理

1)申請記憶體:new

int *p = new int;
int *p = new int(10);  //*p = 10
int *p = new int[5];  //申請陣列,大小為 5

2)釋放記憶體: delete

int *p = new int;  //申請
delete p;          //釋放

int *p = new int(10);    //申請
delete p;                //釋放

int *p = new int[5];     //申請
delete []p;              //釋放

3)判斷申請記憶體的失敗

int *p = new int[200];
if(NULL == p)
{
    //記憶體申請失敗
}

4)釋放記憶體完畢的後續工作

int *p = new int[200];
delete []p;
p = NULL;

【注】記憶體管理罪與罰:當申請的記憶體忘記釋放,就會產生記憶體洩漏。

程式碼演示:

#include <iostream>
#include <stdlib.h>
using namespace std;

int main(void)
{
	/*int *p = new int;
	if (NULL == p)
	{
		cout << "failed" << endl;
	}
	cout << "ok" << endl;
	*p = 30;
	delete p;
	p = NULL;*/

	int *p = new int[100];
	if (NULL == p)
	{
		cout << "failed" << endl;
	}
	cout << "ok" << endl;
	for (int i = 0; i < 100; i++)
	{
		p[i] = i;
	}
	delete []p;
	p = NULL;
	system("pause");
	return 0;
}

(2)檔案的讀寫

1)檔案輸出

​
#include <fstream>
using namespace std;
int main (void)
{
    ofstream myFile("D:\\TEST\\1.txt",ios::out);    //開啟檔案 1.txt
    myFile << "hello C++" << endl;                   //寫入 hello C++
    myFile.close();                                  //關閉檔案
    return 0;
}

2)檔案輸入

​
#include <fstream>
using namespace std;
int main (void)
{
    char ch;
    ifstream myFile("D:\\TEST\\1.txt",ios::in);     //開啟檔案 1.txt
    myFile >> ch;                                    //讀出檔案內容至 ch
    myFile.close();                                  //關閉檔案
    return 0;
}

(3)檔案開啟方式

ios::app 將輸出資料新增到檔案的結尾
ios::ate 將一個檔案開啟作為輸出檔案,並轉移到檔案尾。可以在檔案的任何位置寫資料
ios::in 開啟一個檔案作為輸入檔案
ios::out 開啟一個檔案作為輸出檔案
ios::trunc 如果檔案有內容則將檔案內容丟棄(預設)
ios::binary 開啟一個檔案進行二進位制輸入或輸出

程式碼演示:

#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;

int main(void)
{
	/*ofstream myFile("D:\\TEST\\1.txt", ios::out); 
	myFile << "hello C++" << endl;
	myFile.close();*/

	char ch[9];
	ifstream myFile("D:\\TEST\\1.txt", ios::in);
	for (int i = 0; i < 9; i++)
	{
		myFile >> ch[i];
		cout << ch[i];    //輸出:helloC++
	}
	myFile.close();
	system("pause");
	return 0;
}

7.關鍵字 const

關鍵字:const

constant 單詞的簡寫,是關鍵字的含義。可以通過修飾一個變數使其成為常量。

const int x = 0;
int const x = 0;

const int *p = 0;
int const *p = 0;

int *const p = 0;  //修飾指標型別變數

const int *const p = 0;
int const *const p = 0;

(1)const 修飾普通資料型別

int x = 0;  x = 10;  //正確
const int x = 0;  x = 10;  //錯誤

double y = 0.5; y = 1.8;  //正確
double const y = 0.5; y = 1.8;  //錯誤

(2)const 修飾指標型別

int x = 0;
int y = 0;
int *p = &x;  //定義指標變數 p,p 指向 x
*p = 3;       //重新賦值
p = &y;       //p 指向 y

int x = 0;
int y = 0;
int const*p = &x;  //定義指標變數 p,p 指向 x 且不能重新賦值
*p = 3;            //錯誤
p = &y;            //p 指向 y

int x = 0;
int y = 0;
int *const p = &x;  //定義指標變數 p,p 指向 x 且不能指向其他變數
*p = 3;             //重新賦值
p = &y;             //錯誤

int x = 0;
int y = 0;
int *const*p = &x;  //定義指標變數 p,p 指向 x
*p = 3;             //錯誤
p = &y;             //錯誤

(3)const 修飾函式引數

int max (int x, int y)
int max (const int x, int const y)   //在max函式內不會出現x, y賦值的情況
int max (int const*p, int *const q)  //不改變 p 的值,且不指向其他地址