metalni

OOP Labs 6 NBA

May 30th, 2020
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. class NBAPlayer{
  7.     protected:
  8.         char * name;
  9.         char team[40];
  10.         double avgPoints;
  11.         double avgAssists;
  12.         double avgJumps;
  13.         void copy(const NBAPlayer &orig){
  14.             this->name = new char[strlen(orig.name)+1];
  15.             strcpy(this->name, orig.name);
  16.             strcpy(this->team, orig.team);
  17.             this->avgPoints = orig.avgPoints;
  18.             this->avgAssists = orig.avgAssists;
  19.             this->avgJumps = orig.avgJumps;
  20.         }
  21.     public:
  22.         NBAPlayer();
  23.         NBAPlayer(const char * name, const char * team, const double avgPoints, const double avgAssists, const double avgJumps);
  24.         NBAPlayer(const NBAPlayer &orig);
  25.         NBAPlayer &operator=(const NBAPlayer &orig);
  26.         ~NBAPlayer();
  27.         const double rating();
  28.         void print();
  29. };
  30.  
  31. NBAPlayer::NBAPlayer(){
  32.     this->name = new char[0];
  33.     strcpy(this->name, "");
  34.     strcpy(this->team, "");
  35.     this->avgPoints = 0;
  36.     this->avgAssists = 0;
  37.     this->avgAssists = 0;
  38. }
  39. NBAPlayer::NBAPlayer(const char * name, const char * team, const double avgPoints, const double avgAssists, const double avgJumps){
  40.     this->name = new char[strlen(name)+1];
  41.     strcpy(this->name, name);
  42.     strcpy(this->team, team);
  43.     this->avgPoints = avgPoints;
  44.     this->avgAssists = avgAssists;
  45.     this->avgJumps = avgJumps;
  46. }
  47. NBAPlayer::NBAPlayer(const NBAPlayer &orig){
  48.     this->copy(orig);
  49. }
  50. NBAPlayer &NBAPlayer::operator=(const NBAPlayer &orig){
  51.     if(this != &orig){
  52.         delete [] this->name;
  53.         this->copy(orig);
  54.     }
  55.     return *this;
  56. }
  57. NBAPlayer::~NBAPlayer(){
  58.     delete [] this->name;
  59. }
  60. const double NBAPlayer::rating(){
  61.     return ((this->avgPoints * 0.45) + (this->avgAssists * 0.3) + (this->avgJumps * 0.25));
  62. }
  63. void NBAPlayer::print(){
  64.     cout << this->name << " - " << this->team << endl;
  65.     cout << "Points: " << this->avgPoints << endl;
  66.     cout << "Assists: " << this->avgAssists << endl;
  67.     cout << "Rebounds: " << this->avgJumps << endl;
  68.     cout << "Rating: " << this->rating() << endl;
  69. }
  70.  
  71. //class AllStarPlayer
  72. class AllStarPlayer : public NBAPlayer{
  73.     private:
  74.         double avgAllStarPoints;
  75.         double avgAllStarAssists;
  76.         double avgAllStarJumps;
  77.         void copy(const AllStarPlayer &orig){
  78.             this->name = new char[strlen(orig.name)+1];
  79.             strcpy(this->name, orig.name);
  80.             strcpy(this->team, orig.team);
  81.             this->avgPoints = orig.avgPoints;
  82.             this->avgAssists = orig.avgAssists;
  83.             this->avgJumps = orig.avgJumps;
  84.             this->avgAllStarPoints = orig.avgAllStarPoints;
  85.             this->avgAllStarAssists = orig.avgAllStarAssists;
  86.             this->avgAllStarJumps = orig.avgAllStarJumps;
  87.         }
  88.     public:
  89.         AllStarPlayer();
  90.         AllStarPlayer(const NBAPlayer &orig, const double avgAllStarPoints, const double avgAllStarAssists, const double avgAllStarJumps) : NBAPlayer(orig){
  91.             this->avgAllStarPoints = avgAllStarPoints;
  92.             this->avgAllStarAssists = avgAllStarAssists;
  93.             this->avgAllStarJumps = avgAllStarJumps;
  94.         }
  95.         AllStarPlayer(const char * name, const char * team, const double avgPoints, const double avgAssists, const double avgJumps, const double avgAllStarPoints, const double avgAllStarAssists, const double avgAllStarJumps) : NBAPlayer(name, team, avgPoints, avgAssists, avgJumps){
  96.             this->avgAllStarPoints = avgAllStarPoints;
  97.             this->avgAllStarAssists = avgAllStarAssists;
  98.             this->avgAllStarJumps = avgAllStarJumps;
  99.         }
  100.         AllStarPlayer(const AllStarPlayer &orig);
  101.         AllStarPlayer &operator=(const AllStarPlayer &orig);
  102.         ~AllStarPlayer();
  103.         const double allStarRating();
  104.         const double rating();
  105.         void print();
  106. };
  107.  
  108. AllStarPlayer::AllStarPlayer(){
  109.     this->avgAllStarPoints = 0;
  110.     this->avgAllStarAssists = 0;
  111.     this->avgAllStarJumps = 0;
  112. }
  113. AllStarPlayer::AllStarPlayer(const AllStarPlayer &orig){
  114.     this->copy(orig);
  115. }
  116. AllStarPlayer &AllStarPlayer::operator=(const AllStarPlayer &orig){
  117.     if(this != &orig){
  118.         delete [] this->name;
  119.         this->copy(orig);
  120.     }
  121.     return *this;
  122. }
  123. AllStarPlayer::~AllStarPlayer(){}
  124. const double AllStarPlayer::allStarRating(){
  125.     return ((this->avgAllStarPoints * 0.3) + (this->avgAllStarAssists * 0.4) + (this->avgAllStarJumps * 0.3));
  126. }
  127. const double AllStarPlayer::rating(){
  128.     return ((NBAPlayer::rating() + this->allStarRating())/2.0);
  129. }
  130. void AllStarPlayer::print(){
  131.     NBAPlayer::print();
  132.     cout << "All Star Rating: " << this->allStarRating() << endl;
  133.     cout << "New Rating: " << this->rating() << endl;
  134. }
  135.  
  136. //main
  137. int main() {
  138.  
  139.   char name[50];
  140.   char team[40];
  141.   double points;
  142.   double assists;
  143.   double rebounds;
  144.   double allStarPoints;
  145.   double allStarAssists;
  146.   double allStarRebounds;
  147.  
  148.   NBAPlayer * players = new NBAPlayer[5];
  149.   AllStarPlayer * asPlayers = new AllStarPlayer[5];
  150.   int n;
  151.   cin >> n;
  152.  
  153.   if (n == 1) {
  154.  
  155.     cout << "NBA PLAYERS:" << endl;
  156.     cout << "=====================================" << endl;
  157.     for (int i = 0; i < 5; ++i) {
  158.       cin >> name >> team >> points >> assists >> rebounds;
  159.       players[i] = NBAPlayer(name,team,points,assists,rebounds);
  160.       players[i].print();
  161.     }
  162.   }
  163.   else if (n == 2) {
  164.  
  165.     for (int i=0; i < 5; ++i){
  166.       cin >> name >> team >> points >> assists >> rebounds;
  167.       cin >> allStarPoints >> allStarAssists >> allStarRebounds;
  168.       players[i] = NBAPlayer(name,team,points,assists,rebounds);
  169.       asPlayers[i] = AllStarPlayer(players[i],allStarPoints,allStarAssists,allStarRebounds);
  170.     }
  171.  
  172.     cout << "NBA PLAYERS:" << endl;
  173.     cout << "=====================================" << endl;
  174.     for (int i=0; i < 5; ++i)
  175.       players[i].print();
  176.  
  177.     cout << "ALL STAR PLAYERS:" << endl;
  178.     cout << "=====================================" << endl;
  179.     for (int i=0; i < 5; ++i)
  180.       asPlayers[i].print();
  181.  
  182.     }
  183.     else if (n == 3) {
  184.  
  185.       for (int i=0; i < 5; ++i){
  186.         cin >> name >> team >> points >> assists >> rebounds;
  187.         cin >> allStarPoints >> allStarAssists >> allStarRebounds;
  188.         asPlayers[i] = AllStarPlayer(name, team, points, assists, rebounds,
  189.                                      allStarPoints,allStarAssists,allStarRebounds);
  190.       }
  191.       cout << "ALL STAR PLAYERS:" << endl;
  192.       cout << "=====================================" << endl;
  193.       for (int i=0; i < 5; ++i)
  194.         asPlayers[i].print();
  195.  
  196.     }
  197.    
  198.     delete [] players;
  199.     delete [] asPlayers;
  200. }
Add Comment
Please, Sign In to add comment