生活随笔
收集整理的這篇文章主要介紹了
C++类的静态成员
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
靜態(tài)成員的提出是為了解決數(shù)據(jù)共享的問題,實現(xiàn)共享有多種方法,全局變量或?qū)ο缶哂芯窒扌院筒话踩?/strong>
靜態(tài)數(shù)據(jù)成員
可以實現(xiàn)多個對象之間的數(shù)據(jù)共享,并且使用靜態(tài)數(shù)據(jù)成員還不會破壞隱藏的原則,保證了安全性 靜態(tài)成員函數(shù)
和靜態(tài)數(shù)據(jù)成員一樣,都屬于類,而不屬于某個對象,對靜態(tài)成員的引用可以用類名限定的方法和靜態(tài)數(shù)據(jù)成員一樣,都屬于類,而不屬于某個對象,對靜態(tài)成員的引用可以用類名限定的方法
#include <iostream>
#include <string.h>
using namespace std
;
class Student
{
public:Student(int Nu
,char *Na
,double Sc
):Num(Nu
){int L
=strlen(Na
);Name
=new char[L
+1];strcpy(Name
,Na
);Score
=Sc
;Total_Score
=Total_Score
+Score
;}~Student(){delete[] Name
;}void Print(){cout
<<"Num\t"<<Num
<<endl
;cout
<<"Name\t"<<Name
<<endl
;cout
<<"Score\t"<<Score
<<endl
;}static void Print_Total_Score(){cout
<<"Total_Score\t"<<Total_Score
<<endl
;}private:const int Num
;char *Name
;double Score
;static double Total_Score
;
};double Student
::Total_Score
=0;int main()
{Student
s1(10001,"Zhang San",90),s2(10002,"Zhang San",80),s3(10003,"Zhang San",70);s1
.Print();cout
<<"----------------------------\n";s2
.Print();cout
<<"----------------------------\n";s2
.Print();cout
<<"----------------------------\n";Student
::Print_Total_Score();
}
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖
總結(jié)
以上是生活随笔為你收集整理的C++类的静态成员的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。