类和对象Ⅹ----static

老师讲解

static

Q1:how 理解static?

A1 : 是类中的全局变量,被所有对象共享

1 通过对象访问得平均分

#include<iostream>

using namespace std;

class Student {
	
	public :
		
		//班级人数,静态数据成员
		static int count;
		//班级的总分数,静态数成员 
		static double total;
		//定义带参数的构造函数 
		Student(double score) {
			
			//this指针区分成员和非成员
			this->score = score;
			count++;
			total += score; 
		}
		 
		double getAverage() {
			
			return total / count;
		}
		
		double getTotal () {
			
			return total;
		}
	private:
		
		//个人分数 
		double score;
};

//static数据成员初始化 
int Student::count = 0;
double Student::total = 0.0;

int main () {
	
	/* 
	cout<<Student::count<<endl; 
	cout<<Student::total<<endl; 
	
	Student s1(70);
	
	cout<<Student::count<<endl; 
	cout<<Student::total<<endl;
	
	//另一种输出方法 
	cout<<s1.count<<endl;
	*/
	 
	Student s1(70), s2(80);
	//通过对象访问 
	cout<<s1.getAverage(); 
	//通过类名访问 
	//cout<<Student::getAverage();
	//cout<<Student::getTotal();
	
	return 0;
}

执行结果:

2 通过类名访问得均分和总分

#include<iostream>

using namespace std;

class Student {
	
	public :
		
		//班级人数,静态数据成员
		static int count;
		//班级的总分数,静态数成员 
		static double total;
		//定义带参数的构造函数 
		Student(double score) {
			
			//this指针区分成员和非成员
			this->score = score;
			count++;
			total += score; 
		}
		 
		static double getAverage() {
			
			return total / count;
		}
		
		static double getTotal () {
			
			return total;
		}
	private:
		
		//个人分数 
		double score;
};

//static数据成员初始化 
int Student::count = 0;
double Student::total = 0.0;

int main () {
	
	/* 
	cout<<Student::count<<endl; 
	cout<<Student::total<<endl; 
	
	Student s1(70);
	
	cout<<Student::count<<endl; 
	cout<<Student::total<<endl;
	
	//另一种输出方法 
	cout<<s1.count<<endl;
	*/
	 
	Student s1(70), s2(80);
	//通过对象访问 
	//cout<<s1.getAverage(); 
	//通过类名访问 
	cout<<Student::getAverage()<<endl;
	cout<<Student::getTotal()<<endl;
	
	return 0;
}

 执行结果:

3 定义一个对象数组,继续执行上述操作

#include<iostream>

using namespace std;

class Student {
	
	public :
		
		//班级人数,静态数据成员
		static int count;
		//班级的总分数,静态数成员 
		static double total;
		//定义带参数的构造函数 
		Student(double score) {
			
			//this指针区分成员和非成员
			this->score = score;
			count++;
			total += score; 
		}
		 
		static double getAverage() {
			
			return total / count;
		}
		
		static double getTotal () {
			
			return total;
		}
	private:
		
		//个人分数 
		double score;
};

//static数据成员初始化 
int Student::count = 0;
double Student::total = 0.0;

int main () {
	
	/* 
	cout<<Student::count<<endl; 
	cout<<Student::total<<endl; 
	
	Student s1(70);
	
	cout<<Student::count<<endl; 
	cout<<Student::total<<endl;
	
	//另一种输出方法 
	cout<<s1.count<<endl;
	*/
	 
	//定义对象数组 
	Student s[3] = {Student(70), Student(80), Student(90)};
	//通过对象访问 
	//cout<<s1.getAverage(); 
	//通过类名访问 
	cout<<Student::getAverage()<<endl;
	cout<<Student::getTotal()<<endl;
	
	//这两个语句等价 
	cout<<s[0].count<<endl;
	cout<<Student::count<<endl; 
	
	return 0;
}

执行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值