const成员变量
如果类中有const成员变量,那么初始化的时候,只能用参数初始化列表构造函数
复制代码
1
2
3
4Student(string name,int age,float score):name(name),age(age),score(score),max_length(3){ number++; total += score; }
复制代码
1
2
3
4
5
6
7
8//拷贝构造函数中,const成员变量的初始化,用初始化列表 Student(const Student & s):max_length(3){ this ->name = s.name; this ->age = s.age; this ->score = s.score; number++; total += score; };
以下是一个完整的例子
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119#include<iostream> #include<string> using namespace std; class Student{ private: string name; int age; float score; //const成员变量 const int max_length; //定义静态成员变量 static int number; static float total; public: //Student(string name,int age,float score); //有const成员变量,必须用参数初始化列表, Student(string name,int age,float score):name(name),age(age),score(score),max_length(3){ number++; total += score; } //拷贝构造函数中,const成员变量的初始化,用初始化列表 Student(const Student & s):max_length(3){ this ->name = s.name; this ->age = s.age; this ->score = s.score; number++; total += score; }; ~Student(); void setName(string n); string getName(); void setAge(int a); int getAge(); void setScore(float s); float getScore(); void say(); static float getAverage(); }; /* 注意,如果构造函数的形参和 类的成员变量名字一样,必须采用 this -> name = name ,而不可以 写成 name = name, 但是如果用参数初始化列表写构造函数,就可以避免这个问题 */ /* Student::Student(string name,int age,float score){ this->name = name; this ->age = age; this ->score = score; number++; total += score; } */ /* Student::Student(const Student & s){ this ->name = s.name; this ->age = s.age; this ->score = s.score; } */ Student::~Student(){} string Student::getName(){ return this->name; } int Student::getAge(){ return this->age; } float Student::getScore(){ return this ->score; } void Student::setName(string n){ this ->name = n; } void Student::setAge(int a){ this ->age =a ; } void Student::setScore(float s){ this->score =s; } void Student::say(){ cout << this->name <<" : " << this->age <<" : " << this ->score << " : " << Student::number <<endl; } float Student::getAverage(){ if(number == 0) { return 0; } else return total/number; } //静态变量必须初始化,才可以使用 int Student::number = 0; float Student::total = 0; int main(int argc,char*argv[]) { //即使没有创建对象也可以访问静态成员方法 cout << "没有学生的时候的平均成绩"<< Student::getAverage() <<endl; Student s1("lixiaolong",32,100.0); s1.say(); Student s2("chenglong",32,95.0); s2.say(); Student s3("shixiaolong",32,87.0); s3.say(); Student s4(s1); s4.say(); cout << "平均成绩为" << Student::getAverage() <<endl; system("pause"); return 0; }
const成员函数
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119#include<iostream> #include<string> using namespace std; class Student{ private: string name; int age; float score; //const成员变量 const int max_length; //定义静态成员变量 static int number; static float total; public: //Student(string name,int age,float score); //有const成员变量,必须有参数初始化列表, Student(string name,int age,float score):name(name),age(age),score(score),max_length(3){ number++; total += score; } //拷贝构造函数中,const成员变量的初始化,用初始化列表 Student(const Student & s):max_length(3){ this ->name = s.name; this ->age = s.age; this ->score = s.score; number++; total += score; }; ~Student(); void setName(string n); string getName()const; void setAge(int a); int getAge() const; void setScore(float s); float getScore() const; void say() const; static float getAverage(); }; /* 注意,如果构造函数的形参和 类的成员变量名字一样,必须采用 this -> name = name ,而不可以 写成 name = name, 但是如果用参数初始化列表写构造函数,就可以避免这个问题 */ /* Student::Student(string name,int age,float score){ this->name = name; this ->age = age; this ->score = score; number++; total += score; } */ /* Student::Student(const Student & s){ this ->name = s.name; this ->age = s.age; this ->score = s.score; } */ Student::~Student(){} string Student::getName()const{ return this->name; } int Student::getAge() const{ return this->age; } float Student::getScore()const{ return this ->score; } void Student::setName(string n){ this ->name = n; } void Student::setAge(int a){ this ->age =a ; } void Student::setScore(float s){ this->score =s; } void Student::say()const{ cout << this->name <<" : " << this->age <<" : " << this ->score << " : " << Student::number <<endl; } float Student::getAverage(){ if(number == 0) { return 0; } else return total/number; } //静态变量必须初始化,才可以使用 int Student::number = 0; float Student::total = 0; int main(int argc,char*argv[]) { //即使没有创建对象也可以访问静态成员方法 cout << "没有学生的时候的平均成绩"<< Student::getAverage() <<endl; Student s1("lixiaolong",32,100.0); s1.say(); Student s2("chenglong",32,95.0); s2.say(); Student s3("shixiaolong",32,87.0); s3.say(); Student s4(s1); s4.say(); cout << "平均成绩为" << Student::getAverage() <<endl; system("pause"); return 0; }
最后
以上就是害羞金鱼最近收集整理的关于C++之类的const成员变量和const成员函数const成员变量const成员函数的全部内容,更多相关C++之类内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复