Define a Square class that has a double-precision data member named side_length. The detailed requirements are:
- Define a constructor function to initialize side_length with an input parameter. The default value of side_length is 4. In addition, calculate the perimeter and area of the constructed Square object and accumulate it to static data members total_perimeter and total_area, respectively.
- Define an accessor function to display a Square object’s side_length.
- Define a mutator function to set a Square object’s side_length.
- Define a static function totalPerimeter () to display the value of total_perimeter.
- Define a static function totalArea() to display the value of total_area.
- The main() function:
- Define a Square a with a side length of 5. Use accessor to output the side length of a, total area and total perimeter.
- Use mutator to change the side length of a to 3.Use accessor to output the side length of a, total area and total perimeter.
- Define a Square b with the default value.Use accessor to output the side length of b, total area and total perimeter.
#include <bits/stdc++.h>
using namespace std;
class Square {
private:
double side_length;
public:
static double total_perimeter;
static double total_area;
Square(double side_length = 4.0) : side_length(side_length) {
total_perimeter = side_length * 4;
total_area = side_length * side_length;
}
void accessor() {
printf("The side length is %.3f\n", side_length);
}
void mutator(double side_length) {
this->side_length = side_length;
total_perimeter = side_length * 4;
total_area = side_length * side_length;
}
static void totalPerimeter() {
cout << "The total area is " << total_perimeter << endl;
}
static void totalArea() {
cout << "The total area is " << total_area << endl;
}
};
double Square::total_perimeter = 0;
double Square::total_area = 0;
int main() {
Square* a = new Square(5.0);
a->accessor();
a->totalArea();
a->totalPerimeter();
a->mutator(3);
a->accessor();
a->totalArea();
a->totalPerimeter();
Square* b = new Square();
b->accessor();
b->totalArea();
b->totalPerimeter();
return 0;
}
筆記
靜態成員
在類定義中,它的成員(包括成員變量和成員函數),這些成員可以用關鍵字static聲明為靜態的,稱為靜態成員
不管這個類創建了多少個對象,靜態成員在內存中只有一份,被所有屬於這個類的對象共享
靜態成員變量
在一個類中,若將一個成員變量聲明為static,這種成員稱為靜態成員變量
靜態變量,是在編譯階段就分配了空間,對象還沒有創建時,就已經分配了空間
- 靜態成員變量必須在類中聲明,在類外初始化
- 如果沒有初始化就會報錯,以下是錯誤寫法
-
#include <bits/stdc++.h> using namespace std; class Person { public: static int a; }; int main() { cout << Person::a; return 0; }
- 在構造函數類初試化也會報錯,以下是錯誤寫法
-
#include <bits/stdc++.h> using namespace std; class Person { public: static int a; Person() { a = 1; } }; int main() { cout << Person::a; return 0; }
- 故慾正確運行,應該像這樣寫
-
#include <bits/stdc++.h> using namespace std; class Person { public: static int a; }; int Person::a = 1; int main() { cout << Person::a; return 0; }
- 靜態數據成員不屬於某個對象,在為對象分配空間中不包括靜態成員所佔空間
- 靜態數據成員可以通過類名或者對象名來引用
-
#include <bits/stdc++.h> using namespace std; class Person { public: static int a; }; int Person::a = 1; int main() { cout << Person::a << endl; Person x; x.a = 2; cout << Person::a << endl; Person y; y.a = 3; cout << y.a << endl; Person::a = 4; cout << y.a << endl; return 0; }
- 靜態成員變量也是有訪問的權限
- 即private的成員不能直接訪問,以下寫法會報錯
-
#include <bits/stdc++.h> using namespace std; class Person { private: static int a; }; int Person::a = 1; int main() { cout << Person::a; return 0; }
靜態成員函數
- 所有對象都共享同一份函數
- 靜態成員函數只可以訪問靜態成員變量,不可以訪問非靜態成員變量
- 因為在內存中只有一份靜態成員函數,故調用靜態成員函數訪問非靜態變量會造成系統不知道訪問的是哪個對象的成員變量
- 比如這樣寫就會報錯
-
#include <bits/stdc++.h> using namespace std; class Person { private: int a = 1; public: static void getA() { cout << a; } }; int main() { Person::getA(); return 0; }
- 靜態成員函數也是有訪問權限的
- 靜態成員函數有兩種訪問方式
- 通過對象
- 通過類名