日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++之类的静态成员变量和静态成员函数

發(fā)布時(shí)間:2023/12/10 c/c++ 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++之类的静态成员变量和静态成员函数 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

static靜態(tài)成員函數(shù)

在類中。static 除了聲明靜態(tài)成員變量,還能夠聲明靜態(tài)成員函數(shù)。

普通成員函數(shù)能夠訪問全部成員變量。而靜態(tài)成員函數(shù)僅僅能訪問靜態(tài)成員變量。



我們知道。當(dāng)調(diào)用一個(gè)對(duì)象的成員函數(shù)(非靜態(tài)成員函數(shù))時(shí),系統(tǒng)會(huì)把當(dāng)前對(duì)象的起始地址賦給 this 指針。而靜態(tài)成員函數(shù)并不屬于某一對(duì)象。它與不論什么對(duì)象都無關(guān),因此靜態(tài)成員函數(shù)沒有 this 指針。既然它沒有指向某一對(duì)象,就無法對(duì)該對(duì)象中的非靜態(tài)成員進(jìn)行訪問。

能夠說。靜態(tài)成員函數(shù)與非靜態(tài)成員函數(shù)的根本差別是:非靜態(tài)成員函數(shù)有 this 指針。而靜態(tài)成員函數(shù)沒有 this 指針。由此決定了靜態(tài)成員函數(shù)不能訪問本類中的非靜態(tài)成員。

靜態(tài)成員函數(shù)能夠直接引用本類中的靜態(tài)數(shù)據(jù)成員,由于靜態(tài)成員相同是屬于類的,能夠直接引用。在C++程序中,靜態(tài)成員函數(shù)主要用來訪問靜態(tài)數(shù)據(jù)成員。而不訪問非靜態(tài)成員。

假設(shè)要在類外調(diào)用 public 屬性的靜態(tài)成員函數(shù)。要用類名和域解析符“::”。

如:



下面是一個(gè)完整演示樣例。 <pre name="code" class="cpp"> #include<iostream> #include<string> using namespace std;class Student{ private:string name;int age;float score;static int number; //定義靜態(tài)成員變量static float total; public:Student(string name,int age,float score);Student(const Student & s);~Student();void setName(string n);string getName();void setAge(int a);int getAge();void setScore(float s);float getScore();void say();static float getAverage(); }; /*注意。假設(shè)構(gòu)造函數(shù)的形參和 類的成員變量名字一樣。必須採用 this -> name = name ,而不能夠 寫成 name = name*/ Student::Student(string name,int age,float score){this->name = name;this ->age = age;this ->score = score;number++;total += score; }Student::Student(const Student & s){this ->name = s.name;this ->age = s.age;this ->score = s.score; }Student::~Student(){} string Student::getName(){return this->name; } int Student::getAge(){return this->age; } float Student::getScore(){return this ->score; }void Student::setName(string n){this ->name = n; }void Student::setAge(int a){this ->age =a ; }void Student::setScore(float s){this->score =s; }void Student::say(){cout << this->name <<" : " << this->age <<" : " << this ->score << " : " << Student::number <<endl; }float Student::getAverage(){if(number == 0){return 0;}elsereturn total/number; } //靜態(tài)變量必須初始化。才干夠使用 int Student::number = 0; float Student::total = 0;int main(int argc,char*argv[]) {//即使沒有創(chuàng)建對(duì)象也能夠訪問靜態(tài)成員方法cout << "沒有學(xué)生的時(shí)候的平均成績"<< Student::getAverage() <<endl;Student s1("lixiaolong",32,100.0);Student s2("chenglong",32,95.0);Student s3("shixiaolong",32,87.0);s1.say();s2.say();s3.say();cout << "平均成績?yōu)?#34; << Student::getAverage() <<endl;system("pause");return 0; }


轉(zhuǎn)載于:https://www.cnblogs.com/liguangsunls/p/7281411.html

總結(jié)

以上是生活随笔為你收集整理的C++之类的静态成员变量和静态成员函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。