-
聲明和定義
1.變量的定義
變量的定義用於為變量分配存儲空間,還可以為變量指定初始值。在一個程序中,變量有且僅有一個定義。
2.變量的聲明
用於向程序表明變量的類型和名字。程序中變量可以聲明多次,但只能定義一次。個人理解聲明就是只能看不能用。
3.兩者聯系與區別:
(1)定義也是聲明,因為當定義變量時我們也向程序表明了它的類型和名字;
(2)但聲明不是定義,可以通過使用extern關鍵字聲明變量而不定義它。不定義變量的聲明包括對象名、對象類型和對象類型前的關鍵字extern;
例:
extern int i;//聲明但不定義
int i;//聲明也定義
extern聲明不是定義,也不分配存儲空間。事實上,它只是說明變量定義在程序的其他地方。
注意:如果聲明有初始化式,那么它可被當作是定義,此時聲明也是定義了,即使聲明標記為extern,
例如:extern double pi = 3.1416;//聲明也定義,此句話雖然使用了extern,但是這條語句還是定義了pi,分配並初始化了存儲空間。
注意:只有當extern聲明位於函數外部時,才可以含有初始化式。
注意:因為初始化的extern聲明已經是定義了,而變量在程序中只能定義一次,所以對該變量隨后的任何定義都是錯誤的:
extern double pi = 3.1416;//定義了
double pi;//重定義,不合法
注意:在C++語言中,變量必須僅能定義一次,而且在使用變量之前必須定義或聲明變量。
4.為什么需要區分聲明和定義:
C++程序通常由許多文件組成。為了讓多個文件訪問相同的變量,C++區分了聲明和定義。任何在多個文件中使用的變量都需要既有定義又有聲明。在這種情況下,在一個文件中定義了變量,在其他使用改變了的文件中則只能包含變量的聲明(不能再包含定義,因為變量只能定義一次)。
-
實例
通過類的繼承實現不同角色的權限控制。權限:博士生>研究生>本科生,本科生只能訪問關於本科的課程信息,研究生可以訪問研究生和本科的課程信息,博士生可以訪問博士、研究生和本科的課程信息。
#include<iostream> #include<string> #include"student.h" #include"teacher.h" using namespace std; class System{ protected:
//這里只是聲明,沒有定義 static Student students1[100]; static Teacher teachers1[100]; static int s1; static int t1; static Student students2[100]; static Teacher teachers2[100]; static int s2; static int t2; static Student students3[100]; static Teacher teachers3[100]; static int s3; static int t3; }; class StudentSystem1 :virtual public System{ public: virtual void registe(string, string, string, string); void display1(); virtual void menu(); virtual bool search(string, string); }; class StudentSystem2 :virtual public StudentSystem1{ public: virtual void registe(string, string, string, string); void display2(); virtual void menu(); virtual bool search(string, string); }; class StudentSystem3 :virtual public StudentSystem2{ public: virtual void registe(string, string, string, string); void display3(); void menu(); bool search(string, string); };
#include"studentsystem.h" Student System::students1[100]; Teacher System::teachers1[100]; int System::s1; int System::t1; Student System::students2[100]; Teacher System::teachers2[100]; int System::s2; int System::t2; Student System::students3[100]; Teacher System::teachers3[100]; int System::s3; int System::t3; bool StudentSystem1::search(string nu,string p){ for (int i = 0; i < s1; i++){ if (students1[i].getNumber()==nu&&students1[i].getPassword() == p) return true; } return false; } bool StudentSystem2::search(string nu, string p){ for (int i = 0; i < s2; i++){ if (students2[i].getNumber() == nu&&students2[i].getPassword() == p) return true; } return false; } bool StudentSystem3::search(string nu, string p){ for (int i = 0; i < s3; i++){ if (students3[i].getNumber() == nu&&students3[i].getPassword() == p) return true; } return false; } void StudentSystem1::registe(string number, string password, string name, string room){ Student stu(number, password, name, room); students1[s1] = stu; s1++; cout << "注冊成功" << endl; } void StudentSystem2::registe(string number, string password, string name, string room){ Student stu(number, password, name, room); students2[s2] = stu; s2++; cout << "注冊成功" << endl; } void StudentSystem3::registe(string number, string password, string name, string room){ Student stu(number, password, name, room); students3[s3] = stu; s3++; cout << "注冊成功" << endl; } void StudentSystem1::display1(){ cout << "本科:"<<endl; for (int i = 0; i < t1; i++){ teachers1[i].display(); } system("pause"); system("cls"); } void StudentSystem2::display2(){ cout << "研究生:" << endl; for (int i = 0; i < t2; i++){ teachers2[i].display(); } system("pause"); system("cls"); } void StudentSystem3::display3(){ cout << "博士生:" << endl; for (int i = 0; i < t3; i++){ teachers3[i].display(); } system("pause"); system("cls"); } void StudentSystem1::menu(){ char opt = ' '; while (opt != '2'){ cout << "--------------------" << endl; cout << "1 查看課程信息" << endl; cout << "2 返回" << endl; cout << "--------------------" << endl; cin >> opt; switch (opt) { case '1': display1(); break; case '2': break; default: cout << "輸入錯誤" << endl; system("pause"); system("cls"); break; } } } void StudentSystem2::menu(){ char opt = ' '; while (opt != '3'){ cout << "--------------------" << endl; cout << "1 查看本科課程信息" << endl; cout << "2 查看研究生課程信息" << endl; cout << "3 注銷" << endl; cout << "--------------------" << endl; cin >> opt; switch (opt) { case '1': display1(); break; case '2': display2(); break; case '3': break; default: cout << "輸入錯誤" << endl; system("pause"); system("cls"); break; } } } void StudentSystem3::menu(){ char opt = ' '; while (opt != '4'){ cout << "--------------------" << endl; cout << "1 查看本科課程信息" << endl; cout << "2 查看研究生課程信息" << endl; cout << "3 查看博士生課程信息" << endl; cout << "4 注銷" << endl; cout << "--------------------" << endl; cin >> opt; switch (opt) { case '1': display1(); break; case '2': display2(); break; case '3': display3(); break; case '4': break; default: cout << "輸入錯誤" << endl; system("pause"); system("cls"); break; } } }