[C++]類和物件學習總結
阿新 • • 發佈:2019-01-27
本文章分為知識點、例子和心得
基礎知識:
- 必須在定義了類之後,才可以定義類的物件,物件是類的例項或實體。
- 物件成員的訪問:1、圓點訪問形式:物件名.公有成員(例:)
2、指標訪問形式:物件指標變數名->公有成員(例:)
- class與struct:1、class中,成員預設情況是private
- 利用建構函式建立物件:1、利用建構函式直接建立物件.
一般形式 : 類名 物件名[(實參表)];
2、利用建構函式建立物件時,通過指標和new來實現。
一般語法形式為 : 類名 *指標變數 = new 類名[(實參表)];
- 必須使用引數初始化列表對資料成員進行初始化的幾種情況:
1、資料成員為常量 2、資料成員為引用型別 3、資料成員為沒有無參建構函式的類的物件
注意:
- 在類的定義中不能對資料成員進行初始化。
- 類定義必須以分號“;”結束
- 類的任何成員都必須指定訪問屬性,一般將資料成員定義為私有成員或保護成員,將成員函式定義為公有成員。
例子:
例1:學生資訊管理系統
#include<iostream> #include<string> #include<algorithm> using namespace std; class student { string name; int no; int score[3]; float average; int order; public: student(int id,string na,int x,int y,int z):name(na),no(id) { score[0]=x,score[1]=y,score[2]=z; order=-1,average=(score[0]+score[1]+score[2])/3; } student() { score[0]=score[1]=score[2]=0; order=-1,average=0; } int getNo(){return no;} float getAverage(){return average;}; void setAverage(int avg){average=avg;} void setOrder(int x){order=x;} int getOrder(){return average;}; string getName(){return name;} void setName(string name){this->name=name;} void display(); }; void student::display() { cout<<name<<"\t"<<no<<"\t"<<score[0]<<"\t"<<score[1]<<"\t"<<score[2]<<"\t"<<average<<"\t\t"<<order<<endl; } bool cmp1(student stu1,student stu2) { if(stu1.getAverage()-stu2.getAverage()>=1e-9) return 1; else return 0; } bool cmp2(student stu1,student stu2) { return stu1.getNo()<stu2.getNo(); } class student_list { student list[60]; int n; public: student_list():n(0){}; void add(); void deleteStu(); void query(); void change(); void display(int flag);//flag=1,成績按學號排列。flag=0,成績按名次排列 void menu(); int search(int no);//按學號查詢。找到返回在成績表中的位置,否則返回-1 void sortList(); }; //該函式的功能是新增一個或者多個同學基本資訊。 void student_list::add() { int no,x,y,z; string name; system("cls"); cout<<"請輸入 學號、姓名、數學、英語、C++,輸入-1結束\n"; while((cin>>no)&&no!=-1) { cin>>name>>x>>y>>z; student s(no,name,x,y,z); list[n++]=s; sortList(); } } //該函式的功能是對改變了的成績表進行名次更新。結果可以按名次排列也可按學號排列 void student_list::sortList() { sort(list,list+n,cmp1); int i; for(i=0;i<n;i++) list[i].setOrder(i+1); } void student_list::display(int flag)//flag=1,按學號排列; flag=0,按名次排列 { if(flag) sort(list,list+n,cmp2); else sort(list,list+n,cmp1); cout<<"姓名"<<"\t"<<"學號"<<"\t"<<"數學"<<"\t"<<"英語"<<"\t"<<"c++"<<"\t"<<"平均成績"<<"\t"<<"名次"<<endl; for(int i=0;i<n;i++) list[i].display(); } int student_list::search(int no) { int i; for(int i=0;i<n;i++) if(list[i].getNo()==no) return i; return -1; } void student_list::query() { int no,i; system("cls"); cout<<"請輸入要查詢同學的學號,按-1結束查詢:"; while(cin>>no&&no!=-1) { i=search(no); if(i!=-1) { cout<<"姓名"<<"\t"<<"學號"<<"\t"<<"數學"<<"\t"<<"英語"<<"\t"<<"c++"<<"\t"<<"平均成績"<<"\t"<<"名次"<<endl; list[i].display(); cout<<"請輸入要查詢的同學的學號,按-1結束查詢:"; } else cout<<"學號輸入有誤,請重輸,輸入-1結束查詢"<<endl; } } //該函式的功能是實現按學號進行修改操作,並輸出查詢的結果 void student_list::change() { int no,i,x,y,z; string name; system("cls"); cout<<"請輸入要修改同學的學號,按-1結束脩改:"; while(cin>>no&&no!=-1) { i=search(no); if(i!=-1) { cout<<"請輸入修改後的姓名、數學、英語、C++\n"; cin>>name>>x>>y>>z; student s(no,name,x,y,z); list[i]=s; sortList(); cout<<"請輸入要修改同學的學號,按-1結束脩改:"; } else cout<<"學號輸入錯誤,請重輸,輸入-1結束"; } } void student_list::deleteStu() { int no,i; system("cls"); cout<<"請輸入要刪除同學的學號,按-1結束:"; while(cin>>no&&no!=-1) { i=search(no); if(i!=-1) { for(int a=i;a<=n;a++) { list[a]=list[a+1]; } n=n-1; sortList(); cout<<"請輸入要刪除同學的學號,按-1結束:"; } else cout<<"學號輸入錯誤,請重輸,輸入-1結束"; } }
心得:
本章為C++最重要的一節,也是C++和C的不同點,學好類基本C++就穩了,剩下知識需要鍛鍊來積累了。
調程式的時候注意老師說的,調通一個類再寫下一個類
學生管理系統還不會選擇功能,只能在main函式裡面選擇
學的不是很精,還需要繼續學習,五一再研究