注:若沒有特指是 靜態成員時,默認都是普通成員;ios
1 類中的普通成員安全
類中的成員變量 和 成員函數 是分開存儲的。其中,ide
1)每一個對象都有獨立的成員變量;成員變量能夠存儲在 棧空間、堆空間、全局數據區;函數
2)全部對象共享類的成員函數;成員函數 只能存儲在 代碼段;this
2 類中的靜態成員(static)spa
類中的靜態成員指針
一、用 static關鍵字 修飾;code
二、能夠用 類名::成員名 訪問 靜態成員;對象
三、靜態成員 屬於 整個類;blog
四、靜態成員 是所屬類的成員,其它類不能訪問;
五、靜態成員的內存分配 是 惟一的;
1) 靜態成員變量
特徵:一、靜態成員變量 屬於 整個類全部;
二、靜態成員變量的生命週期不依賴任何對象;(靜態成員變量的生命週期在程序的運行期)
三、全部對象共享類的靜態成員變量;
四、能夠經過 類名 直接訪問公有的靜態成員變量;
五、能夠經過 對象名 訪問公有的靜態成員變量;
六、靜態成員變量 須要在類外單獨分配空間;(類內聲明、類外定義並初始化)
七、靜態成員變量 在程序內部位於全局數據區,不計入類的內存計算。
緣由/好處:使用靜態成員變量實現多個對象之間的數據共享不會破壞隱藏的原則,保證了安全性還能夠節省內存。
使用方法:
一、在類的內部,使用 static 修飾普通成員變量;
二、在類的外部(全局做用域),使用 Type ClassName::VarName = value 初始化,並申請存儲空間;
注:靜態成員變量不屬於類的任何對象,因此並非對象創建時被定義的,因此它不能由類的構造函數初始化,通常也不能在類內初始化;
1 /* 2 靜態成員變量 只能在類的內部聲明,在類的外部(全局區)定義和初始化; 3 */ 4 5 #include <iostream> 6 7 using namespace std; 8 9 class Test{ 10 public: 11 int GetA() const{return a;} 12 private: 13 static int a; // 靜態成員變量 14 }; 15 //int Test::a;若是這樣定義不賦予初值,則初值爲零 16 int Test::a = 1; 17 18 int main(int argc, char *argv[]) 19 { 20 Test T; 21 22 cout << T.GetA() << endl; 23 24 return 0; 25 }
靜態成員變量 被類的全部對象共享,包括派生類對象;
1 #include <iostream> 2 3 using namespace std; 4 5 class Base{ 6 public: 7 static int a; // 靜態成員變量 8 }; 9 10 // int Test::a;若是這樣定義不賦予初值,則初值爲零 11 int Base::a; 12 13 class Derived : public Base{ 14 15 }; 16 17 18 int main(int argc, char *argv[]) 19 { 20 Base B; 21 Derived D; 22 23 B.a++; 24 cout << B.a << endl; // 1 25 D.a++; 26 cout << D.a << endl; // 2 27 28 return 0; 29 }
靜態成員變量能夠做爲普通成員函數的默認形參,而普通成員變量則不能夠;
1 class Test{ 2 public: 3 static int a; //靜態成員變量 4 int b; 5 void fun_1(int i = a); //正確 6 //void fun_2(int i = b); //報錯 7 };
靜態成員變量的類型 能夠是所屬類的類型,而普通成員變量則不能夠。普通成員變量只能聲明爲 所屬類類型的 指針或引用;
1 class Test{ 2 public: 3 static Test a; //正確 4 Test b; //報錯 5 Test *pTest; //正確 6 Test &m_Test; //正確 7 static Test *pStaticObject; //正確 8 };
靜態成員變量在const函數中能夠修改,而普通的成員變量是萬萬不能修改的;
1 /* 2 const修飾的是當前this指針所指向的對象是const,可是靜態成員變量不屬於任何類的對象,它被類的全部對象修改,因此this指針不修飾靜態的成員變量,因此能夠更改。 3 */ 4 class Test{ 5 public: 6 static int a; 7 int b; 8 public: 9 Test():b(0){} 10 void test() const 11 { 12 a++; 13 //b++; // err // const指的是不能修改當前調用該函數對象的成員變量 14 } 15 }; 16 17 int Test::a;
2)靜態成員函數
特徵:一、靜態成員函數 屬於 整個類全部;
二、全部對象共享類的靜態成員函數;
二、能夠經過 類名 直接訪問公有的靜態成員函數;
三、能夠經過 對象名 訪問公有的靜態成員函數;
四、靜態成員函數 只能 訪問靜態成員,不能訪問 非靜態成員;
五、靜態成員函數沒有this指針,也就是說靜態成員函數不能使用修飾符(也就是函數後面的const關鍵字);
緣由:處理靜態成員變量;
使用方法:直接用 static 修飾 普通成員函數(類內聲明時),不須要 static 修飾(類外定義時);
總結:
案例分析:
1 /** 2 * 統計某班選修課考試的平均成績 3 */ 4 5 #include <iostream> 6 #include <string> 7 8 using namespace std; 9 10 class Student 11 { 12 private: 13 string name; // 姓名 14 string gender; // 性別 15 float score; // 分數 16 string subject; // 課程 17 static int total_counts; // 總人數 18 static float chinese_scores; // 語文分數 19 static int chinese_counts; // 語文課人數 20 static float math_scores; // 數學分數 21 static int math_counts; // 數學課人數 22 public: 23 Student(string name, string gender, float score, string subject); 24 ~Student(); 25 static float aveScores(string subject); 26 void printStudentInfo(); 27 void printAveScores(); 28 }; 29 int Student::total_counts = 0; 30 float Student::chinese_scores = 0; 31 int Student::chinese_counts = 0; 32 float Student::math_scores = 0; 33 int Student::math_counts = 0; 34 35 Student::Student(string name, string gender, float score, string subject) 36 { 37 this->name = name; 38 this->gender = gender; 39 this->score = score; 40 this->subject = subject; 41 42 if(subject == "chinese" || subject == "語文") 43 { 44 chinese_scores += score; 45 chinese_counts++; 46 } 47 else if(subject == "math" || subject == "數學") 48 { 49 math_scores += score; 50 math_counts++; 51 } 52 else 53 { 54 cout << "this is no the subect:" << subject << endl; 55 } 56 total_counts += (chinese_counts + math_counts); 57 } 58 59 Student::~Student() 60 { 61 total_counts--; 62 chinese_counts--; 63 math_counts--; 64 } 65 66 float Student::aveScores(string subject) 67 { 68 float ave_score = 0; 69 70 if(chinese_counts > 0 && subject == "chinese" || subject == "語文") 71 { 72 ave_score = (chinese_scores / chinese_counts); 73 //cout << subject << "\t" << chinese_counts << "\t" << chinese_scores << endl; 74 } 75 else if(math_counts > 0 && subject == "math" || subject == "數學") 76 { 77 ave_score = (math_scores / math_counts); 78 //cout << subject << "\t" <<math_counts << "\t" << math_scores << endl; 79 } 80 81 return ave_score; 82 } 83 84 void Student::printStudentInfo() 85 { 86 cout << name << "\t" << gender << "\t" << score << "\t" << subject << endl; 87 } 88 89 void Student::printAveScores() 90 { 91 cout <<subject << " average score: " << aveScores(subject) << endl; 92 } 93 94 int main(int argc, char const *argv[]) 95 { 96 const int SIZE = 5; 97 Student stu[SIZE] = 98 { 99 Student("10001", "male", 92, "語文"), 100 Student("10002", "male", 91, "數學"), 101 Student("10003", "male", 91, "數學"), 102 Student("10004", "male", 93, "語文"), 103 Student("10005", "male", 92, "語文"), 104 }; 105 106 for(int i = 0; i < SIZE; i++) 107 { 108 stu[i].printStudentInfo(); 109 } 110 111 stu[0].printAveScores(); 112 stu[1].printAveScores(); 113 114 cout << "語文" << " average score: " << Student::aveScores("語文") << endl; 115 cout << "數學" << " average score: " << Student::aveScores("數學") << endl; 116 117 return 0; 118 }