Sort Class object
Code:
#include<iostream>
#include<algorithm>
using namespace std;
class Candidates{
string name;
int age;
double cgpa;
public:
Candidates(){
}
Candidates(string name,int age,double cgpa){
this->name = name;
this->age = age;
this->cgpa = cgpa;
}
void print(){
cout<<"Name : "<<name<<'\t';
cout<<"Age : "<<age<<'\t'<<"CGPA : "<<cgpa<<endl;
}
bool operator<(const Candidates ca)const{
if(cgpa>ca.cgpa){//increasing order CGPA
return true;
}else if(cgpa==ca.cgpa){
if(age<ca.age){//decreasing order age
return true;
}else if(age == ca.age){
if(name<ca.name){//decreasing order name
return true;
}
}
}return false;
}
};
int main(){
Candidates ca[5];
ca[0] = Candidates("Arefin",20,3.78);
ca[1] = Candidates("Baki",21,3.45);
ca[2] = Candidates("Saha",20,3.68);
ca[3] = Candidates("Mike",20,3.45);
ca[4] = Candidates("Clarck",21,3.45);
sort(ca,ca+5);
for(int i=0;i<5;i++){
ca[i].print();
}
return 0;
}
মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন