1. 程式人生 > 其它 >c++周小結(第一週)

c++周小結(第一週)

技術標籤:c++

文章目錄


一.C++與C的區別

1. 標頭檔案

C以.h結尾
C++無拓展名

2.字串

2.1 標頭檔案

C++中使用字串要加標頭檔案string
C中不用新增

2.2 風格

C風格字串: char 變數名[] = “字串值”
C++風格字串:string 變數名 = “字串值”

3.C++中新增變數型別bool

bool型別有兩種內建常量true(轉化為整數1)和false(轉換為整數0)表示狀態。//bool、true、false都為關鍵字

4.賦值

C中的賦值只有一種:‘=’
C++中除了使用‘=’外,還可以使用‘()’

5. 結構體

C中struct關鍵字不能省略
C++中struct關鍵字可以省略

二.記憶不深的知識點

1.邏輯運算子

在這裡插入圖片描述

2.結構體

2.1陣列

語法: struct 結構體名 陣列名{元素個數}={ {},{},…{} }

示例:

//結構體定義
struct student
{
    //成員列表
    string name;//名字
    int sex;	//性別
    int age;    //年齡
};
int main(){
    //結構體陣列
    struct student a[3]=
    {
{"張三","男",18}, {"李四","男",20}, {"王五","男",22} } return 0; }

2.2指標

利用操作符 ->可以通過結構體指標訪問結構體屬性

示例:

//結構體定義
struct student
{
    //成員列表
    string name;//名字
    int sex;	//性別
    int age;    //年齡
};
int main(){
    //建立學生變數
struct student stu = {"張三","男",18}; //通過指標指向結構體變數 struct student * p = &stu; //通過指標訪問結構體變數中的資料 cout<<"姓名:"<< p->name<<"年齡:"<< p->age<<"分數:"<< p->score<<endl; return 0; }

2.3結構體巢狀

結構體中成員可以是另一個結構體

示例:

struct person
{
	string stuID;//學號
	string name;//名字
	int sex;//性別
	int age;//年齡
	string allscore;//總成績
};
struct eas
{
	struct person personarray[1000];
	int size;//學生人數
};