Static member variables and static member functions such as C + +

Source: Internet
Author: User

Static Statics member functionIn the class. Static in addition to declaring static member variables, it is also possible to declare a statically member function.

The normal member function is able to access all member variables. Static member functions can only access static member variables.



we know. When a member function (non-static member function) of an object is called, the system assigns the starting address of the current object to the this pointer. A static member function does not belong to an object. It has nothing to do with any object, so the static member function does not have the this pointer. Since it does not point to an object, you cannot access non-static members of the object.

able to say. The fundamental difference between a static member function and a non-static member function is that the non-static member function has the this pointer. The static member function does not have this pointer. This determines that a static member function cannot access non-static members in this class.

static member functions can refer directly to static data members in this class, because static members are the same class and can be referenced directly. In C + + programs, static member functions are primarily used to access static data members. Without visiting non-static members.

assume that you want to out-of-class with the public property of a static member function. To use the class name and the domain resolver "::".

Such as:

The following is a full demo sample.

<pre name= "code" class= "CPP" > #include <iostream> #include <string>using namespace Std;class student{ private:string name;int age;float score;static int number; Define static member variables 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 ();};/ Note Assume that the constructor's shape participates in the same name as the member variable of the class. You must use this, name = name, and not be able to write name = Name*/student::student (string name,int age,float score) {this->name = Name;t His->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;} Static variables must be initialized. Ability to use int student::number = 0;float Student::total = 0;int main (int argc,char*argv[]) {//Even if no object is created, you can access static member methods cout << "Average score without students" << 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 << "Average score" << student::getaverage () <<endl;system ("pause"); return 0;}



Static member variables and static member functions such as C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.