1-->static与类
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class AA { public: AA(int a, int b) { m_a = a; m_b = b; } int getC() { m_c++; return m_c; } //静态的成员方法 static int& getCC() { return m_c; } private: //static修饰的静态成员变量 static int m_c; int m_a; int m_b; }; //静态成员变量的初始化,一定类的外边。 int AA::m_c = 100; int main(void) { AA a1(10, 20); AA a2(100, 200); cout << a1.getC() << endl;//101 cout << a2.getC() << endl;//102 //a1.getCC() = 200; AA::getCC() = 200; cout << a1.getC() << endl;//201 cout << a2.getC() << endl;//202 return 0; }
示例2
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class Box { public: Box(int l, int w) { len = l; width = w; } int volume() { int v = len *width*hight; cout << "高度是" << hight << endl; cout << "体积是" << v << endl; return v; } static void changeHight(int h) { hight = h; } private: int len; int width; static int hight; }; int Box::hight = 100; int main(void) { Box b1(10, 20); Box b2(100, 200); b1.volume(); b2.volume(); Box::changeHight(300); b1.volume(); b2.volume(); return 0; }
示例 学生求平均分数
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class Student { public: Student(int id, double score) { //创建一个学生 m_id = id; m_score = score; m_count++;//累加一个个数 sum_score += score; } static int getCount() { return m_count; } static double getAvg() { return sum_score / m_count; } ~Student() { m_count--; sum_score -= m_score; } private: int m_id; double m_score;//得分 //统计学生个数的静态成员变量 static int m_count; //统计学生总分数的静态成员变量 static double sum_score; }; int Student::m_count = 0; double Student::sum_score = 0.0; int main(void) { Student s1(1, 80); Student s2(2, 90); Student s3(3, 100); cout << "学生总人数: " << Student::getCount() << endl; cout << "学生的平均分: " << Student::getAvg() << endl; return 0; }
static不占用内存空间
#include <iostream> using namespace std; class C1 { public: int i; //4 int j; //4 int k; //4 }; //12 class C2 { public: int i; int j; int k; static int m; //4 public: int getK() const { return k; } //4 void setK(int val) { k = val; } //4 }; struct S1 { int i; int j; int k; }; //12 struct S2 { int i; int j; int k; static int m; }; //12? int main() { cout << "c1 : " << sizeof(C1) << endl; cout << "c1 : " << sizeof(C2) << endl; C2 c1, c2; //c1.getK(); //为什么会return c1.k //c2.getK(); // 为什么会return c2.k cout << " ----- " << endl; cout << "c1 : " << sizeof(S1) << endl; cout << "c1 : " << sizeof(S2) << endl; return 0; }
#include <stdio.h> class Test { private: int mCount; public: Test() : mCount(0) { mCount++; } ~Test() { --mCount; } int getCount() { return mCount; } }; Test gTest; int main() { Test t1; Test t2; printf("count = %d\n", gTest.getCount()); printf("count = %d\n", t1.getCount()); printf("count = %d\n", t2.getCount()); return 0; }
再看示例:
#include <stdio.h> class Test { private: static int cCount; public: Test() { cCount++; } ~Test() { --cCount; } int getCount() { return cCount; } }; int Test::cCount = 0; Test gTest; int main() { Test t1; Test t2; printf("count = %d\n", gTest.getCount()); printf("count = %d\n", t1.getCount()); printf("count = %d\n", t2.getCount()); Test* pt = new Test(); printf("count = %d\n", pt->getCount()); delete pt; printf("count = %d\n", gTest.getCount()); return 0; }
示例
#include <stdio.h> class Test { public: static int cCount; public: Test() { cCount++; } ~Test() { --cCount; } int getCount() { return cCount; } }; int Test::cCount = 0; int main() { printf("count = %d\n", Test::cCount); Test::cCount = 1000; printf("count = %d\n", Test::cCount); return 0; }
再看 示例:
#include <stdio.h> class Demo { private: int i; public: int getI(); static void StaticFunc(const char* s); static void StaticSetI(Demo& d, int v); }; int Demo::getI() { return i; } void Demo::StaticFunc(const char* s) { printf("StaticFunc: %s\n", s); } void Demo::StaticSetI(Demo& d, int v) { d.i = v; } int main() { Demo::StaticFunc("main Begin..."); Demo d; Demo::StaticSetI(d, 10); printf("d.i = %d\n", d.getI()); Demo::StaticFunc("main End..."); return 0; }
示例3
#include <stdio.h> class Test { private: static int cCount; public: Test() { cCount++; } ~Test() { --cCount; } static int GetCount() { return cCount; } }; int Test::cCount = 0; int main() { printf("count = %d\n", Test::GetCount()); Test t1; Test t2; printf("count = %d\n", t1.GetCount()); printf("count = %d\n", t2.GetCount()); Test* pt = new Test(); printf("count = %d\n", pt->GetCount()); delete pt; printf("count = %d\n", Test::GetCount()); return 0; }
时间: 2024-10-14 02:29:58