Skip to content

Commit 3a20334

Browse files
committed
ch7 completed
7-56 to 7-58
1 parent 9cb39b0 commit 3a20334

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

ch7/7_56.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
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.

ch7/7_57.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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

ch7/7_58.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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);

0 commit comments

Comments
 (0)