概述
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<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成员函数
#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++之类的const成员变量和const成员函数const成员变量const成员函数所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复