File tree Expand file tree Collapse file tree 3 files changed +53
-0
lines changed Expand file tree Collapse file tree 3 files changed +53
-0
lines changed Original file line number Diff line number Diff line change
1
+ // static member: member of a class, which is related to the class correctly, not the specific object.
2
+ // advantage: you can change the value of a static member once and change all the value in all objects.
3
+ // differences: firstly, we can use it as an incomplete type. Secondly, it can be used as a default argument.
Original file line number Diff line number Diff line change
1
+ #ifndef EX7_57_H
2
+ #define EX7_57_H
3
+
4
+ #include < iostream>
5
+ #include < string>
6
+
7
+ class Account
8
+ {
9
+ public:
10
+ Account () = default ;
11
+ Accoutn (std::istream &is){ is >> owner >> amount; }
12
+ Account (double a, std::string o) : amount(a), owner(o) { }
13
+
14
+ void calculate () { amount += amount * interestRate; }
15
+ static double rate () { return interestRate; }
16
+ static void rate (double );
17
+
18
+ private:
19
+ double amount;
20
+ std::string owner;
21
+ static double interestRate;
22
+ static double initRate ();
23
+ };
24
+
25
+ void Account::rate (double newRate)
26
+ {
27
+ interestRate = newRate;
28
+ }
29
+
30
+ double Account::initRate ()
31
+ {
32
+ return 0.0 ;
33
+ }
34
+
35
+ double Account::interestRate = initRate();
36
+
37
+ #endif
Original file line number Diff line number Diff line change
1
+ // example.h
2
+ class Example
3
+ {
4
+ public:
5
+ static double rate = 6.5 ;// must be constexpr
6
+ static const int vecSize = 20 ;
7
+ static vector<double > vec (vecSize);// error, in-class initializer should use = or {}
8
+ };
9
+
10
+ // example.C
11
+ #include " example.h"
12
+ double Example::rate;
13
+ vector<double > Example::vec;// vector<double> Example::vec(vecSize);
You can’t perform that action at this time.
0 commit comments