Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <algorithm>
- using namespace std;
- class Games {
- public:
- Games(); // Default constructor
- Games(string, string, string, int, double);
- void SetName(string);
- void SetScore(double);
- void Print();
- void SetType(string);
- void SetYear(int);
- void SetGenre(string);
- int GetScore() { //Gets the score form the private: section
- return score;
- }
- int GetYear() { //Gets the year form the private: section
- return year;
- }
- string GetName() { //Gets the name form the private: section
- return name;
- }
- string GetGenre() { //Gets the genre form the private: section
- return genre;
- }
- string GetType() { //Gets the type form the private: section
- return type;
- }
- private:
- string name, type, genre;
- double score;
- int year;
- };
- Games::Games() { // Default constructor definition
- name = "UnNamed";
- type = "Undefined";
- genre = "NoGenre";
- score = -1.0;
- year = -1;
- }
- Games::Games(string n, string t, string g, int y, double s) {
- name = n;
- type = t;
- genre = g;
- year = y;
- score = s;
- }
- void Games::SetName(string gameName) {
- name = gameName;
- }
- void Games::SetScore(double gameRank) {
- score = gameRank;
- }
- void Games::SetType(string gameType) {
- type = gameType;
- }
- void Games::SetYear(int gameYear) {
- year = gameYear;
- }
- void Games::SetGenre(string gameGenre) {
- genre = gameGenre;
- }
- void Games::Print() {
- cout << setw(20) << name << " -- ";
- cout << setw(5) << score << " -- ";
- cout << setw(5) << year << " -- ";
- cout << setw(5) << genre << " -- ";
- cout << setw(5) << type << endl;
- }
- void SortByScore(Games* target) //function to sort the array by score
- {
- sort(target, target + 5, [](Games& a, Games& b) ->
- bool {
- return a.GetScore() < b.GetScore();
- });}
- void SortByYear(Games* target) //function to sort the array by year
- {
- sort(target, target + 5, [](Games& a, Games& b) ->
- bool {
- return a.GetYear() < b.GetYear();
- });}
- void SortByName(Games* target) //function to sort the array by name
- {
- sort(target, target + 5, [](Games& a, Games& b) ->
- bool {
- return a.GetName() < b.GetName();
- });}
- void SortByGenre(Games* target) //function to sort the array by genre
- {
- sort(target, target + 5, [](Games& a, Games& b) ->
- bool {
- return a.GetGenre() < b.GetGenre();
- });}
- void SortByType(Games* target) //function to sort the array by type
- {
- sort(target, target + 5, [](Games& a, Games& b) ->
- bool {
- return a.GetType() < b.GetType();
- });}
- int main()
- {
- int userInput;
- Games myGames[5];
- myGames[0].SetName("Gran Turismo");
- myGames[0].SetYear(1997);
- myGames[0].SetGenre("Racing");
- myGames[0].SetType("PlayStation");
- myGames[0].SetScore(8.6);
- myGames[1] = Games("Super Mario Galaxy", "Wii Console", "Platform", 2007, 9.0);
- myGames[0].Print();
- myGames[1].Print();
- myGames[2].Print();
- cout << "Sorted by score they are: " << endl;
- SortByScore(&myGames[0]); //call to function to print the sorted version
- for(auto game : myGames)
- {
- if(game.GetName() != "UnNamed"){
- game.Print();
- }
- }
- cout << "Sorted by year they are: " << endl;
- SortByYear(&myGames[0]); //call to function to print the sorted version
- for(auto game : myGames)
- {
- if(game.GetName() != "UnNamed"){
- game.Print();
- }
- }
- cout << "Sorted by name they are: " << endl;
- SortByName(&myGames[0]); //call to function to print the sorted version
- for(auto game : myGames)
- {
- if(game.GetName() != "UnNamed"){
- game.Print();
- }
- }
- cout << "Sorted by genre they are: " << endl;
- SortByGenre(&myGames[0]); //call to function to print the sorted version
- for(auto game : myGames)
- {
- if(game.GetName() != "UnNamed"){
- game.Print();
- }
- }
- cout << "Sorted by type they are: " << endl;
- SortByType(&myGames[0]); //call to function to print the sorted version
- for(auto game : myGames)
- {
- if(game.GetName() != "UnNamed"){
- game.Print();
- }
- }
- cout << "input 1 to run again with the UnNamed titles, 0 to quit: ";
- cin >> userInput;
- if (userInput = 1){
- cout << "Sorted by score they are: " << endl;
- SortByScore(&myGames[0]); //call to function to print the sorted version
- for(auto game : myGames)
- {
- game.Print();
- }
- cout << "Sorted by year they are: " << endl;
- SortByYear(&myGames[0]); //call to function to print the sorted version
- for(auto game : myGames)
- {
- game.Print();
- }
- cout << "Sorted by name they are: " << endl;
- SortByName(&myGames[0]); //call to function to print the sorted version
- for(auto game : myGames)
- {
- game.Print();
- }
- cout << "Sorted by genre they are: " << endl;
- SortByGenre(&myGames[0]); //call to function to print the sorted version
- for(auto game : myGames)
- {
- game.Print();
- }
- cout << "Sorted by type they are: " << endl;
- SortByType(&myGames[0]); //call to function to print the sorted version
- for(auto game : myGames)
- {
- game.Print();
- }
- } else if(userInput = 0){
- return 0;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement