定义学生类包含姓名和成绩两个数据成员,重载比较运算符(按成绩和按姓名两个模式比较)和输出运算符

C++模板【定义学生类包含姓名和成绩两个数据成员,重载比较运算符(按成绩和按姓名两个模式比较)和输出运算符】

实现下列功能。

1)定义学生类包含姓名和成绩两个数据成员,重载比较运算符(按成绩和按姓名两个模式比较)和输出运算符。
2)实现greater_than函数模板,用于比较两个对象的大小,如果a>b,返回true,否则返回false。
3)实现less_than函数模板,用于比较两个对象的大小,如果a<b,返回true,否则返回false。
4)实现Print函数模板,用于打印一维数组。
5)实现冒泡排序的函数模板,输入参数是数组、数组大小和用于比较的数组元素的函数指针。
void bubble_sort(T arr[], int len, bool(*compare)(T&, T&));

//学生类部分
enum SortType{
   
	BY_SCORE,BY_NAME
};
static SortType sort_type ;

class Student{
   
	public:
		 		
		Student(string name, int score):name_(name),score_(score){
   
		}
		
		bool operator > (const Student& rhs){
   
			if(sort_type == BY_SCORE){
   
				if(score_>rhs.score_){
   
					return true;
				}
				return false;
			}
			else if(sort_type == BY_NAME){
   
				if(name_>rhs.name_){
   
					return true;
				}
				return false;
			}

		}
		bool operator < (const Student& rhs){
   
			if(sort_type == BY_SCORE){
   
				if(score_<rhs.score_){
   
					return true;
				}
				return false;
			}
			else if(sort_type == BY_NAME){
   
				if(name_<rhs.name_){
   
					return true;
				}
				return false;
			}
		}
		
		//重载输出运算符
		friend ostream& operator<<(ostream& os,Student& student){
   
			os<<"["<<student.name_<<","<<student.score_<<"]";
		}
		
	private:
		string name_;
		int score_;
	
};
// 模板函数部分

template<class T>
bool greater_than(T& a,T& b){
   
	if(a>b){
   
		return true;
	}
	return false;
}

template<class T>
bool less_than(T& a,T& b){
   
	if(a<b){
   
		return true;
	}
	return false;
}

template<class T>
void Print(T t[],int len){
   
	for(int i = 0;i<len;i++){
   
		cout<<t[i]<<" ";
	}
	cout<<endl;
}

template<class T>
void bubble_sort(T arr[],
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值