StefiIOE

NBA lab 6 oop : nasleduvanje

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