C++之类的const成员变量和const成员函数

const成员变量 【C++之类的const成员变量和const成员函数】如果类中有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; };

以下是一个完整的例子

#include #include 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 <


const成员函数
#include #include 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 <




    推荐阅读