C++是一種高級編程語言,被廣泛應用於開發操作系統、瀏覽器、遊戲等軟件,是計算機專業學生必修的一門課程。本文將介紹從零開始學習C++編程的基礎語法和實現程序的一些知識和技巧。
一、數據類型
C++中的基本數據類型包括整型、浮點型、字符型和布爾型,其中整型包括short、int和long三個類型,浮點型包括float和double兩個類型,字符型是用來表示單個字符,布爾型只有兩個值:true和false。
#include <iostream>
using namespace std;
int main()
{
int num = 10;
float f = 3.14;
char ch = 'a';
bool flag = true;
cout << "num = " << num << endl;
cout << "f = " << f << endl;
cout << "ch = " << ch << endl;
cout << "flag = " << flag << endl;
return 0;
}
輸出結果:
num = 10
f = 3.14
ch = a
flag = 1
可以看到,布爾型的true被輸出為1,false被輸出為0。
二、運算符
C++中的運算符包括算術運算符、關係運算符、邏輯運算符、位運算符和賦值運算符。
算術運算符包括加、減、乘、除和取模運算,關係運算符包括等於、不等於、大於、小於、大於等於和小於等於運算,邏輯運算符包括與、或和非運算,位運算符包括按位與、按位或、按位取反和按位異或運算,賦值運算符包括等於、加等於、減等於、乘等於、除等於和取模等於運算。
#include <iostream>
using namespace std;
int main()
{
int a = 10, b = 20;
cout << "a + b = " << a + b << endl;
cout << "a - b = " << a - b << endl;
cout << "a * b = " << a * b << endl;
cout << "a / b = " << a / b << endl;
cout << "a % b = " << a % b << endl;
int c = 30, d = 30;
cout << "c == d: " << (c == d) << endl;
cout << "c != d: " << (c != d) << endl;
cout < d: " < d) << endl;
cout << "c < d: " << (c < d) << endl;
cout <= d: " <= d) << endl;
cout << "c <= d: " << (c <= d) << endl;
bool e = true, f = false;
cout << "e && f: " << (e && f) << endl;
cout << "e || f: " << (e || f) << endl;
cout << "!e: " << !e << endl;
int g = 0x5, h = 0x3;
cout << "g & h: " << (g & h) << endl;
cout << "g | h: " << (g | h) << endl;
cout << "~g: " << ~g << endl;
cout << "g ^ h: " << (g ^ h) << endl;
int i = 5, j = 10;
i += j;
cout << "i += j: " << i << endl;
i -= j;
cout << "i -= j: " << i << endl;
i *= j;
cout << "i *= j: " << i << endl;
i /= j;
cout << "i /= j: " << i << endl;
i %= j;
cout << "i %= j: " << i << endl;
return 0;
}
輸出結果:
a + b = 30
a - b = -10
a * b = 200
a / b = 0
a % b = 10
c == d: 1
c != d: 0
c > d: 0
c = d: 1
c <= d: 1
e && f: 0
e || f: 1
!e: 0
g & h: 1
g | h: 7
~g: -6
g ^ h: 6
i += j: 15
i -= j: 5
i *= j: 50
i /= j: 5
i %= j: 5
三、控制語句
C++中的控制語句包括順序結構、選擇結構和循環結構。
順序結構是指按照代碼的先後順序依次執行每一條語句,例如:
#include <iostream>
using namespace std;
int main()
{
int x = 10, y = 20, z = 30;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
cout << "z = " << z << endl;
return 0;
}
輸出結果:
x = 10
y = 20
z = 30
選擇結構是指根據條件的真假來判斷執行哪一條語句,包括if語句和switch語句。
#include <iostream>
using namespace std;
int main()
{
int x = 10;
if (x < 0) {
cout << "x is negative" << endl;
} else if (x == 0) {
cout << "x is zero" << endl;
} else {
cout << "x is positive" << endl;
}
int y = 2;
switch (y) {
case 1:
cout << "y is one" << endl;
break;
case 2:
cout << "y is two" << endl;
break;
default:
cout << "y is not one or two" << endl;
break;
}
return 0;
}
輸出結果:
x is positive
y is two
循環結構是指重複執行一段代碼,包括while循環、do-while循環和for循環。
#include <iostream>
using namespace std;
int main()
{
int x = 0;
while (x < 5) {
cout << "x = " << x << endl;
x++;
}
int y = 0;
do {
cout << "y = " << y << endl;
y++;
} while (y < 5);
for (int i = 0; i < 5; i++) {
cout << "i = " << i << endl;
}
return 0;
}
輸出結果:
x = 0
x = 1
x = 2
x = 3
x = 4
y = 0
y = 1
y = 2
y = 3
y = 4
i = 0
i = 1
i = 2
i = 3
i = 4
四、函數
函數是一段可以重複使用的代碼,可以實現特定的功能。在C++中定義一個函數需要指定函數的返回類型、函數名稱和參數列表,例如:
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
int main()
{
int x = 2, y = 3;
cout << "x + y = " << add(x, y) << endl;
return 0;
}
輸出結果:
x + y = 5
五、數組
數組是一種存儲一組數據的結構,C++支持靜態數組和動態數組。靜態數組在定義時需要指定數組的長度,動態數組可以在運行時進行分配和釋放。
#include <iostream>
using namespace std;
int main()
{
int arr1[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
cout << "arr1[" << i << "] = " << arr1[i] << endl;
}
int arr2[] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
cout << "arr2[" << i << "] = " << arr2[i] << endl;
}
int* arr3 = new int[5];
for (int i = 0; i < 5; i++) {
arr3[i] = i + 1;
cout << "arr3[" << i << "] = " << arr3[i] << endl;
}
delete[] arr3;
return 0;
}
輸出結果:
arr1[0] = 1
arr1[1] = 2
arr1[2] = 3
arr1[3] = 4
arr1[4] = 5
arr2[0] = 1
arr2[1] = 2
arr2[2] = 3
arr2[3] = 4
arr2[4] = 5
arr3[0] = 1
arr3[1] = 2
arr3[2] = 3
arr3[3] = 4
arr3[4] = 5
六、指針
指針是C++中的重要概念,可以通過指針來訪問內存中的數據。指針變量存儲的是內存地址。
#include <iostream>
using namespace std;
int main()
{
int num = 10;
int* p = #
cout << "num = " << num << endl;
cout << "p = " << p << endl; // 輸出的是指針變量p所指向的內存地址
cout << "*p = " << *p << endl; // 輸出的是p所指向的內存地址中的值
int arr[5] = {1, 2, 3, 4, 5};
int* q = arr; // 數組名本身就是一個指針
for (int i = 0; i < 5; i++) {
cout << "arr[" << i << "] = " << *(q + i) << endl;
}
return 0;
}
輸出結果:
num = 10
p = 0x7ffee7b46bdc
*p = 10
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
七、文件操作
C++中的文件操作包括文件的讀寫和文件指針的操作。文件指針指向文件中的某個位置,可以通過文件指針進行讀寫。
#include <iostream>
#include <fstream>
using namespace std;int main()
{
ofstream fout("data.txt"); // 打開文件用於寫入
for (int i = 1; i <= 5; i++) {
fout << i <> num) { // 從文件中讀取數據
cout << num << endl;
}
fin
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/187221.html