C++之类的const成员变量和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<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;
}


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值