第七章 类

7.1

 1 #include <iostream>
 2 #include <vector>
 3 #include <cctype>
 4 #include <string>
 5 #include <iterator>
 6 #include <initializer_list>
 7 #include <cassert>
 8 
 9 using namespace std; 
10 
11 struct Sales_data{
12     string bookNo;
13     unsigned units_sold = 0;
14     double revenue = 0.0;         //单价 
15 }; 
16 
17 int main()  
18 {     
19     Sales_data total;
20     if (cin >> total.bookNo >> total.units_sold >> total.revenue) {
21         Sales_data trans;
22         while (cin >> trans.bookNo >> trans.units_sold >> trans.revenue) {
23             if (trans.bookNo == total.bookNo) {
24                 total.units_sold += trans.units_sold;
25             }
26             else {
27                 cout << total.bookNo << "的销售额为:" << total.units_sold * total.revenue << endl;
28                 total.bookNo = trans.bookNo;
29                 total.units_sold = trans.units_sold;
30                 total.revenue = trans.revenue;
31             }
32         }
33         cout << total.bookNo << "的销售额为:" << total.units_sold * total.revenue << endl;
34     }
35     else {
36         cerr << "No data?!" << endl; 
37     }
38     return 0; 
39 }
View Code

7.2

 1 struct Sales_data {
 2     string bookNo;                //书的ISBN 
 3     unsigned units_sold = 0;    //售出的本数 
 4     double revenue = 0.0;        //销售额 
 5     Sales_data& combine(const Sales_data &);
 6     string isbn()    {    return this -> bookNo;    }
 7 };
 8 
 9 Sales_data& Sales_data::combine(const Sales_data &rhs)
10 {
11     units_sold += rhs.units_sold;
12     revenue += rhs.revenue;
13     return *this;
14 }
View Code

7.3

 1 #include <iostream>
 2 #include <vector> 
 3 #include <string>
 4  
 5 using namespace std;
 6 
 7 struct Sales_data {
 8     string bookNo;                //书的ISBN 
 9     unsigned units_sold = 0;    //售出的本数 
10     double revenue = 0.0;        //销售额 
11     Sales_data& combine(const Sales_data &);
12     string isbn()    {    return this -> bookNo;    }
13 };
14 
15 Sales_data& Sales_data::combine(const Sales_data &rhs)
16 {
17     units_sold += rhs.units_sold;
18     revenue += rhs.revenue;
19     return *this;
20 }
21 
22 int main()
23 {
24     Sales_data total;
25     if (cin >> total.bookNo >> total.units_sold >> total.revenue) {
26         Sales_data trans;
27         while (cin >> trans.bookNo >> trans.units_sold >> trans.revenue) {
28             if (total.isbn() == trans.isbn())    total.combine(trans);
29             else {
30                 cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
31                 total.bookNo = trans.bookNo;
32                 total.units_sold = trans.units_sold;
33                 total.revenue = trans.revenue;
34             }
35         }
36         cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
37     }
38     else {
39         cerr << "No data?!" << endl;
40         return -1;
41     }
42     return 0;
43 }
View Code

 

7.4&7.5

1 struct Person{
2     string name;
3     string address;
4     string get_name() const    {    return this->name;    }
5     string get_address() const    {    return this->address;    }
6 };
View Code

应该是cosnt成员函数,在这两个函数体内不应该修改成员数据的值。

7.6&7.7

 1 #include <iostream>
 2 #include <vector> 
 3 #include <string>
 4  
 5 using namespace std;
 6 
 7 struct Sales_data {
 8     string bookNo;                //书的ISBN 
 9     unsigned units_sold = 0;    //售出的本数 
10     double revenue = 0.0;        //销售额 
11     Sales_data& combine(const Sales_data &);
12     string isbn()    {    return this -> bookNo;    }
13 };
14 
15 Sales_data& Sales_data::combine(const Sales_data &rhs)
16 {
17     units_sold += rhs.units_sold;
18     revenue += rhs.revenue;
19     return *this;
20 }
21 
22 istream &read(istream &is, Sales_data &item)
23 {
24     is >> item.bookNo >> item.units_sold >> item.revenue;
25     return is;
26 }
27 
28 ostream &print(ostream &os, const Sales_data &item)
29 {
30     os << item.bookNo << " " << item.units_sold << " " << item.revenue;
31     return os;
32 }
33 
34 Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
35 {
36     Sales_data sum = lhs;
37     sum.combine(rhs);
38     return sum;
39 }
40 
41 int main()
42 {
43     Sales_data total;
44     if (read(cin, total)) {
45         Sales_data trans;
46         while (read(cin, trans)) {
47             if (total.isbn() == trans.isbn())    total = add(total, trans);
48             else {
49                 print(cout, total);
50                 cout << endl;
51                 total = trans;        //结构体赋值很方便 
52 //                total.bookNo = trans.bookNo;
53 //                total.units_sold = trans.units_sold;
54 //                total.revenue = trans.revenue;
55             }
56         }
57         cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
58     }
59     else {
60         cerr << "No data?!" << endl;
61         return -1;
62     }
63     return 0;
64 }
View Code

7.8

read函数:从给定的流中将数据读到给定的对象里(可能函数里有要改变对象的值的操作)

print函数:将给定对象的内容打印到给定的流中(直接打印对象的内容,无需改变对象的值)

7.9

 1 #include <iostream>
 2 #include <vector> 
 3 #include <string>
 4  
 5 using namespace std;
 6 
 7 struct Person{
 8     string name;
 9     string address;
10     string get_name()    {    return this->name;    }
11     string get_address()    {    return this->address;    }
12 };
13 
14 istream &read(istream &is, Person &item)
15 {
16     is >> item.name >> item.address;
17     return is;
18 }
19 
20 ostream &print(ostream &os, const Person &item)
21 {
22     os << item.name << " " << item.address;
23     return os;
24 }
25 
26 int main()
27 {
28     Person per;
29 //    cin >> per.name >> per.address;
30     read(cin, per);
31     print(cout, per);
32 //    string ss = per.get_name();
33 //    cout << ss << " " << per.get_address() << endl;
34     return 0;
35 }
View Code

7.10

条件部分的作用:连续输入两次数据

7.11

 1 #include <iostream>
 2 #include <vector> 
 3 #include <string>
 4  
 5 using namespace std;
 6 
 7 struct Sales_data {
 8     string bookNo;                //书的ISBN 
 9     unsigned units_sold = 0;    //售出的本数 
10     double revenue = 0.0;        //销售额 
11     Sales_data& combine(const Sales_data &);
12     string isbn()    {    return this -> bookNo;    }
13     //四种构造函数 
14     Sales_data() = default;
15     Sales_data(const string &bookName): bookNo(bookName) {}
16     Sales_data(const string &bookName, unsigned sellNum, double earn): bookNo(bookName), units_sold(sellNum), revenue(earn) {}
17     Sales_data(istream &is);
18 };
19 
20 istream &read(istream &is, Sales_data &item)
21 {
22     is >> item.bookNo >> item.units_sold >> item.revenue;
23     return is;
24 }
25 
26 Sales_data::Sales_data(istream &is)    
27 {    
28     read(is, *this);    
29 }
30 
31 ostream &print(ostream &os, const Sales_data &item)
32 {
33     os << item.bookNo << " " << item.units_sold << " " << item.revenue << endl;
34     return os;
35 }
36 
37 
38 int main()
39 {
40     Sales_data book1;
41     print(cout, book1);
42     Sales_data book2("ac");
43     print(cout, book2);
44     Sales_data book3("ad", 0, 0.0);
45     print(cout, book3);
46     Sales_data book4(cin);
47     print(cout, book4);
48     return 0;
49 }
View Code

7.12

    Sales_data(istream &is) { is >> bookNo >> units_sold >> revenue; }

7.13

 1 #include <iostream>
 2 #include <vector> 
 3 #include <string>
 4  
 5 using namespace std;
 6 
 7 struct Sales_data {
 8     string bookNo;                //书的ISBN 
 9     unsigned units_sold = 0;    //售出的本数 
10     double revenue = 0.0;        //销售额 
11     Sales_data& combine(const Sales_data &);
12     string isbn()    {    return this -> bookNo;    }
13     //四种构造函数 
14     Sales_data() = default;
15     Sales_data(const string &bookName): bookNo(bookName) {}
16     Sales_data(const string &bookName, unsigned sellNum, double earn): bookNo(bookName), units_sold(sellNum), revenue(earn) {}
17     Sales_data(istream &is) { is >> bookNo >> units_sold >> revenue; }
18 };
19 
20 ostream &print(ostream &os, const Sales_data &item)
21 {
22     os << item.bookNo << " " << item.units_sold << " " << item.revenue << endl;
23     return os;
24 }
25 Sales_data& Sales_data::combine(const Sales_data &rhs)
26 {
27     units_sold += rhs.units_sold;
28     revenue += rhs.revenue;
29     return *this;
30 }
31 
32 istream &read(istream &is, Sales_data &item)
33 {
34     is >> item.bookNo >> item.units_sold >> item.revenue;
35     return is;
36 }
37 
38 Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
39 {
40     Sales_data sum = lhs;
41     sum.combine(rhs);
42     return sum;
43 }
44 
45 
46 int main()
47 {
48     Sales_data total(cin);
49     if (cin) {
50         Sales_data trans(cin);
51         do {
52             if (total.isbn() == trans.isbn())    total = add(total, trans);
53             else {
54                 print(cout, total);
55                 cout << endl;
56                 total = trans;        //结构体赋值很方便 
57             }
58         } while (read(cin, trans));
59         cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
60     }
61     else {
62         cerr << "No data?!" << endl;
63         return -1;
64     }
65     return 0;
66 }
View Code

7.14

    Sales_data() = default;

7.15

1 struct Person{
2     string name;
3     string address;
4     string get_name() const    {    return this->name;    }
5     string get_address() const    {    return this->address;    }
6     //两种构造函数 
7     Person() = default;
8     Person(string iName, string iAddress): name(iName), address(iAddress) {}
9 };
View Code

 

7.16

没有特别的位置与次数限制

public成员定义类的接口

private成员封装(隐藏)了类的实现细节

7.17

有区别,class默认是private,struct默认是public

7.18

封装是把类的数据成员与接口分离,实现对数据的隐藏和保护。封装可以防止用户在不经意间对类数据的改变,封装好的类可以直接让别的用户直接使用,就像基本类型一样,而不需要用户对代码的修改。

7.20

友元的用处:允许其他类或函数访问本类的非公有成员

使用友元的好处:可以使一些非本类的成员函数能访问到类的非公有成员;

缺点是:破坏了类的封装、可维护性、可靠性。

7.21

 1 #include <iostream>
 2 #include <vector> 
 3 #include <string>
 4  
 5 using namespace std;
 6 
 7 class Sales_data {
 8     friend ostream &print(ostream &os, const Sales_data &item);
 9     friend Sales_data add(const Sales_data &lhs, const Sales_data &rhs);
10     friend istream &read(istream &is, Sales_data &item);
11 private:
12     string bookNo;                //书的ISBN 
13     unsigned units_sold = 0;    //售出的本数 
14     double revenue = 0.0;        //销售额 
15 public:
16     Sales_data& combine(const Sales_data &);
17     string isbn() const    {    return bookNo;    }
18     //四种构造函数 
19     Sales_data() = default;
20     Sales_data(const string &bookName): bookNo(bookName) {}
21     Sales_data(const string &bookName, unsigned sellNum, double earn): bookNo(bookName), units_sold(sellNum), revenue(earn) {}
22     Sales_data(istream &is) { is >> bookNo >> units_sold >> revenue; }
23 };
24 
25 ostream &print(ostream &os, const Sales_data &item)
26 {
27     os << item.bookNo << " " << item.units_sold << " " << item.revenue << endl;
28     return os;
29 }
30 
31 Sales_data& Sales_data::combine(const Sales_data &rhs)
32 {
33     units_sold += rhs.units_sold;
34     revenue += rhs.revenue;
35     return *this;
36 }
37 
38 istream &read(istream &is, Sales_data &item)
39 {
40     is >> item.bookNo >> item.units_sold >> item.revenue;
41     return is;
42 }
43 
44 Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
45 {
46     Sales_data sum = lhs;
47     sum.combine(rhs);
48     return sum;
49 }
50 
51 
52 int main()
53 {
54     Sales_data total(cin);
55     if (cin) {
56         Sales_data trans(cin);
57         do {
58             if (total.isbn() == trans.isbn())    total = add(total, trans);
59             else {
60                 print(cout, total);
61                 total = trans;        //结构体赋值很方便 
62             }
63         } while (read(cin, trans));
64         print(cout, total);
65     }
66     else {
67         cerr << "No data?!" << endl;
68         return -1;
69     }
70     return 0;
71 }
View Code

7.22

 1 #include <iostream>
 2 #include <vector> 
 3 #include <string>
 4  
 5 using namespace std;
 6 
 7 class Person{
 8     friend ostream &print(ostream &os, const Person &man);
 9     friend istream &read(istream &is, Person &man);
10 private:
11     string name;
12     string address;
13 public:
14     //两种构造函数 
15     Person() = default;
16     Person(string iName, string iAddress): name(iName), address(iAddress) {}
17     string get_name() const    {    return name;    }
18     string get_address() const    {    return address;    }
19 };
20 
21 ostream &print(ostream &os, const Person &item)
22 {
23     os << item.name << " " << item.address << endl;
24     return os;
25 }
26 
27 istream &read(istream &is, Person &item)
28 {
29     is >> item.name >> item.address;
30     return is;
31 }
32 
33 
34 int main()
35 {
36     Person per;
37     read(cin, per);
38     string ss = per.get_name();
39     print(cout, per);
40     return 0;
41 }
View Code

7.23

 1 class Screen{
 2 private:
 3     unsigned width = 480;
 4     unsigned height = 320;
 5     unsigned cursor = 0;
 6     string content;
 7 public:
 8     Screen() = default;
 9     Screen(unsigned w, unsigned h, string s): width(w), height(h), content(s) {}
10     inline char get() const { return content[cursor];    }
11     inline char get(unsigned r, unsigned c) const    {    unsigned row = r * width;    return content[row + c];    }
12     Screen &move(unsigned r, unsigned c)
13     {
14         unsigned row = r * width;
15         cursor = row + c;
16         return *this;
17     }
18 };
View Code

7.24

	Screen() = default;
	Screen(unsigned w, unsigned h, int num): width(w), height(h), content(num, ' ') {}
	Screen(unsigned w, unsigned h, char c): width(w), height(h), content(w*h, c) {}

7.25

能,因为Screen的数据成员都是自带拷贝或赋值相关的函数。

7.26

inline double avg_price() const
{
  return units_sold ? revenue/units_sold : 0;
}

7.27

 1 #include <iostream>
 2 #include <vector> 
 3 #include <string>
 4  
 5 using namespace std;
 6 
 7 class Screen{
 8 private:
 9     unsigned width = 480;
10     unsigned height = 320;
11     unsigned cursor = 0;
12     string content;
13     void do_display(ostream &os) const    {    os << content;    }
14 public:
15     using pos = unsigned;
16     //三种构造函数 
17     Screen() = default;
18     Screen(unsigned w, unsigned h, int num): width(w), height(h), content(num, ' ') {}
19     Screen(unsigned w, unsigned h, char c): width(w), height(h), content(w*h, c) {}
20     inline char get() const { return content[cursor];    }
21     inline char get(unsigned r, unsigned c) const    {    unsigned row = r * width;    return content[row + c];    }
22     Screen &move(unsigned r, unsigned c)
23     {
24         unsigned row = r * width;
25         cursor = row + c;
26         return *this;
27     }
28     Screen &set(char);
29     Screen &set(pos, pos, char);
30     Screen &display(ostream &os){
31         do_display(os);
32         return *this;
33     }
34     const Screen &display(ostream &os) const {    os << content;    }
35 };
36 
37 inline Screen &Screen::set(char c)
38 {
39     content[cursor] = c;
40     return *this;
41 } 
42 
43 inline Screen &Screen::set(pos r, pos col, char ch)
44 {
45     content[r * width + col] = ch;
46     return *this;
47 }
48 
49 int main()
50 {
51     Screen myScreen(5, 5, 'X');
52     myScreen.move(4, 0).set('#').display(cout);
53     cout << endl;
54     myScreen.display(cout);
55     cout << endl;
56     return 0;
57 }
View Code

7.28

第二个display将打印一片X,没有#,原因是返回的不是引用,这样在myScreen调用move返回的是一个对象,这个对象已经和myScreen没关系了。

7.30

优点:可以非常明确地指出访问的是调用该函数的对象的成员,且可以在成员函数中使用与数据成员同名的形参

缺点:太多余了

7.31

class Y;

class X{
	Y *p;
};

class Y{
	X a;
};

7.32

 1 class Screen{
 2     friend class Window_mgr;
 3 private:
 4     unsigned width = 480;
 5     unsigned height = 320;
 6     unsigned cursor = 0;
 7     string content;
 8 public:
 9     using pos = unsigned;
10     //三种构造函数 
11     Screen() = default;
12     Screen(unsigned w, unsigned h, int num): width(w), height(h), content(num, ' ') {}
13     Screen(unsigned w, unsigned h, char c): width(w), height(h), content(w*h, c) {}
14     const Screen &display(ostream &os) const {    os << content;    }
15 };
16 
17 class Window_mgr{
18 private:
19     vector<Screen> screens = {Screen(24, 80, ' ')};
20 public:
21     using ScreenIndex = vector<Screen>::size_type;
22     void clear(ScreenIndex);
23 };
24 
25 void Window_mgr::clear(ScreenIndex i)
26 {
27     Screen &s = screens[i];
28     s.content = string(s.height * s.width, ' ');
29 }
View Code

7.33

编译器报错:[Error] 'pos' does not name a type

修正指定size函数的返回类型是属于哪个类,即

Screen::pos Screen::size() const 
{	
	return height * width;	
}

7.34

会导致编译出错,因为对 pos 的使用出现在它的声明之前,此时编译器并不知道pos到底是什么含义。

7.35

Type使用的是类内的定义;类外的initVal的返回类型是string,类内的initVal的返回类型是double。

代码存在错误,修正后:

typedef string Type;  
Type initVal();  
class Exercise {  
public:  
    typedef double Type;  
    Type setVal(Type);  
    Type initVal();  
private:  
    int val;  
};  
  
Exercise::Type Exercise::setVal(Type parm) 
{
  val = parm + initVal();
  return val;
}  

 

7.36

rem应先被定义:

struct X{
	X(int i, int j): base(i), rem(base % j) {}
	int base, rem;
};

 

7.37

Sales_data first_item(cin);   //因为是istream类型,所以等待用户输入  
int main() {  
  Sales_data next;  //这个默认构造函数在初始化值Sales_data(std::string s = ""); 值为bookNo=""、cnt=0、revenue=0  
  Sales_data last("9-999-99999-9"); //使用了Sales_data(std::string s = ""); ,值为bookNo="9-999-99999-9"、cnt=0、revenu=0  
}

 

7.38

Sales_data(std::istream &is = std::cin)  
{  
    read(cin, *this);  
}

 

7.39

不合法,因为存在多个默认构造函数。

7.40

Book,构造函数如下:

class Book  
{  
    string title;  
    string author;  
    unsigned id;  
    unsigned num;  
    double price;  
public:  
    Book() = default;  
    Book(string t, string a = "", unsigned i = 0, unsigned n = 0, double p = 0.0)  
        :title(t), author(a), id(i), num(n), price(p) {}  
    Book(istream &is);  
}; 

7.41

 1 #include <iostream>
 2 #include <vector> 
 3 #include <string>
 4  
 5 using namespace std;
 6 
 7 class Sales_data{
 8 private:
 9     string bookNo;
10     unsigned units_sold;
11     double revenue;
12 public:
13     Sales_data(string name, unsigned n, double earn): bookNo(name), units_sold(n), revenue(earn)    { cout << "我是非委托
";}
14     //委托构造函数
15     Sales_data(): Sales_data("default", 0, 0.0)    { cout << "我是默认构造函数
";}
16     Sales_data(string ss): Sales_data("only string", 0, 0)    { cout << "我只有字符串形参
";}
17     Sales_data(istream &is): Sales_data()    { is >> bookNo >> units_sold >> revenue;} 
18 }; 
19 
20 
21 int main()
22 {
23     Sales_data book1;
24     cout << endl;
25     Sales_data book2("feiweituo", 2, 20.0);
26     cout << endl;
27     Sales_data book3("only string");
28     cout << endl;
29     Sales_data book4(cin);
30     cout << endl;
31     return 0;
32 }
View Code

7.42

 1 class Book  
 2 {  
 3     string title;  
 4     string author;  
 5     unsigned id;  
 6     unsigned num;  
 7     double price;  
 8 public:  
 9     Book(): Book("", 0, 0, 0.0) {}      //委托构造函数 
10     Book(string t, string a = "", unsigned i = 0, unsigned n = 0, double p = 0.0)  
11         :title(t), author(a), id(i), num(n), price(p) {}  
12     Book(istream &is);  
13 };
View Code

 

7.43

 1 class NoDefault{
 2 public:
 3     NoDefault(int a): fa(a) {}
 4 private:
 5     int fa;    
 6     string ss;
 7     //... 
 8 }; 
 9 
10 class C{
11 private:
12     NoDefault tt;
13 public:
14     C(int t): tt(t) {}
15 };
View Code

7.44

非法,因为NoDefault没有默认构造函数

7.45

合法,C有默认构造函数

7.46

a 错误:可以不提供构造函数,此时会自动生成一个默认构造函数

b 错误:带默认参数构造函数也是默认构造函数

c 错误:就算怎么无意义,也最好提供一个默认值

d 错误:在不定义任何构造函数的情况下才会自动提供一个默认的构造函数

7.47

这个随便,因为这是一把双刃剑

不带explicit的优点:可以从构造函数的参数类型向类类型隐式转换

带explicit的优点:任何构造函数(尤其是带一个参数的)都不能隐式地创建类对象

带explicit的缺点:该构造函数只能以直接初始化的形式使用

7.48

第一行创建了一个string对象,第二行和第三行都是调用Sales_data的构造函数

不是explicit时:

item1对象会被成功创建

item2对象也会被成功创建

是explicit时:

item1对象会被成功创建(因为直接初始化)

item2对象也会被成功创建(同上)

7.49

(a)合法 
(b)不合法,Salesdata &类型与Salesdata类型之间不可转换 
(c)不合法,把combine声明成了常量成员函数,所以该函数无法修改数据成员的值

7.51

string接受的单参数是const char*类型,如果我们得到了一个常量指针,则把它看做string对象是自然而然的过程,编译器自动把参数类型转换成类类型也非常符合逻辑,因此我们无须指定为explicit。

与string相反,vector接受的单参数是int类型,这个参数的原意是指定vector的容量。如果我们在本来需要vector的地方提供一个int值并且希望这个int值自动转换成vector,则这个过程显得比较牵强,因此把vector的单参数构造函数定义成explicit的更加合理。

例如:定义为explicit是为了防止隐式的类型转换

        void fun(vecor<int> vi); 
        fun(10);        //如果允许这样,那就意味不明了

7.52

"978-0590353403"初始化bookNo成员;

25初始化units_sold成员;

15.99初始化revenue成员。

7.53

class Debug{
private:
	bool hardware = true;
	bool io = true;
	bool other = true;
public:
	constexpr Debug() = default;
	constexpr Debug(bool h, bool i, bool o): hardware(h), io(i), other(o)	{} 
};

7.54

不应该,因为constexpr函数必须返回值

7.55

是的,首先它是一个聚合类,其次它的数据成员都是字面值类型

7.56

类的静态成员:与类关联在一起的成员

优点:存在于任何对象之外,让所有对象共享(不需要在每个对象中都设置)

区别:静态成员的类型可以是它所属的类类型;可以使用静态成员作为默认实参

7.57

 1 class Account{
 2 private:
 3     string owner;
 4     double amount;
 5     static double interestRate;    //利息率
 6     static double initRate();
 7     static constexpr int period = 30;
 8     static Account mem1; 
 9 public:
10     Account() = default;
11     Account(string own, double money): owner(own), amount(money) {}
12     void calculator()    { amount += amount * interestRate; }
13     static double rate()    { return interestRate; }
14     static void rate(double); 
15 };
16 double Account::interestRate = initRate();
17 constexpr int Account::period;        //初始值在类的定义内提供 
18 Account Account::mem1("liu", 20);
19 void Account::rate(double newRate)
20 {
21     interestRate = newRate;
22 }
23 double Account::initRate()
24 {
25     return 2.35;
26 }
View Code

7.58

类内进行了两条静态数据成员的初始化,而他们又并非常量类型,修改后:

 1 class Exampl  
 2 {  
 3 public:  
 4     static double rate;  
 5     static const int vecSize = 20;  
 6     static vector<double> vec;  
 7 };  
 8 //example.C  
 9 #include "example.h"  
10 double Exampl::rate = 6.5;  
11 vector<double> Exampl::vec(Exampl::vecSize);
View Code

 

 

原文地址:https://www.cnblogs.com/xzxl/p/7666378.html