I have a vector of different class objects. And I want to sort the vector using the the top level variable percentage
in the Grading class. I override the method for operator< for all the classes but for some reason it keeps on sorting using by the name
variable.
Here is the design of the classes.
Here is the code
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <iomanip>
using namespace std;
/* -------------------------------------------------------- */
/* ---------------------- Grading Class ------------------- */
/* -------------------------------------------------------- */
class Grading
{
public:
string name;
int percent;
void get_raw_score();
void get_adj_score();
virtual void print() { }
};
bool operator< (const Grading& a, const Grading& b) const {
return (a.percent < b.percent);
}
/* -------------------------------------------------------- */
/* ---------------------- Assignment Class ---------------- */
/* -------------------------------------------------------- */
class Assignment : public Grading
{
public:
int score;
void print() {
cout << name << std::setw(20) << percent << std::setw(20) << score << endl;
}
};
bool operator< (Assignment& a, Assignment& b) {
return (a.percent < b.percent);
}
/* -------------------------------------------------------- */
/* ---------------------- Exam Class ---------------------- */
/* -------------------------------------------------------- */
class Exam : public Grading
{
public:
int score;
Exam(string n, int g, int s) {
name = n;
percent = g;
score = s;
}
void print() {
cout << name << std::setw(20) << percent << std::setw(20) << score << endl;
}
};
bool operator< (Exam& a, Exam& b) {
return (a.percent < b.percent);
}
/* -------------------------------------------------------- */
/* ------------------- Project Class ---------------------- */
/* -------------------------------------------------------- */
class Project : public Assignment
{
public:
string letter_grade;
Project(string n, int g, string l_g) {
name = n;
percent = g;
letter_grade = l_g;
}
void print() {
cout << name << std::setw(20) << percent << std::setw(20) << letter_grade << endl;
}
};
bool operator< (Project& a, Project& b) {
return (a.percent < b.percent);
}
/* -------------------------------------------------------- */
/* ---------------------- Quiz Class ---------------------- */
/* -------------------------------------------------------- */
class Quiz : public Exam
{
public:
string letter_grade;
Quiz(string n, int g, string l_g) : Exam(n, g, score)
{
name = n;
percent = g;
letter_grade = l_g;
}
void print() {
cout << name << std::setw(20) << percent << std::setw(20) << letter_grade << endl;
}
};
bool operator< (Quiz& a, Quiz& b) {
return (a.percent < b.percent);
}
/* -------------------------------------------------------- */
/* ---------------------- CourseWork class ---------------- */
/* -------------------------------------------------------- */
class CourseWork {
public:
CourseWork() {}
void push_back(Grading* a) {
work.push_back(a);
}
// print the data and sort by name
void sort_name() {
std::sort(work.begin(), work.end(), greater<Grading *>());
}
void sort_score() {
std::sort(work.begin(), work.end(), less<Grading *>());
}
friend ostream& operator<<(ostream& os, const CourseWork& dt) {
cout << "Grading" << std::setw(20) << "Percentage" << std::setw(20) << "Raw-Score" << endl;
for (int i = 0; i < (int)dt.work.size(); i++) {
dt.work.at(i)->print();
}
return os;
}
private:
vector<Grading*> work;
};
/* -------------------------------------------------------- */
/* ---------------------- MAIN ---------------------------- */
/* -------------------------------------------------------- */
int main () {
CourseWork c;
c.push_back(new Quiz("Quiz", 5, "B-"));
c.push_back(new Quiz("Quiz", 5, "C+"));
c.push_back(new Quiz("Quiz", 5, "A"));
c.push_back(new Exam("Midterm", 10, 50));
c.push_back(new Exam("Final", 30, 85.5));
c.push_back(new Project("Project", 5, "A-"));
c.push_back(new Project("Project", 15, "B-"));
c.push_back(new Project("Project", 15, "B-"));
c.push_back(new Project("Demo", 10, "C"));
cout << "** Showing populated data..." << endl;
cout << c << endl << endl;;
c.sort_name();
cout << "** Showing sorted by name..." << endl;
cout << c << endl << endl;
c.sort_score();
cout << "** Showing sorted by score..." << endl;
cout << c << endl;
// c.sort_score();
return 0;
}
SOLVED
bool GradingCompare (Grading* l, Grading* r) {
return (l->percent < r->percent);
}
void sort_name() {
std::sort(work.begin(), work.end(), greater<Grading *>());
}