Advertisement
metalni

OOP Zadaci za vezbanje 2 Vozac

Jun 2nd, 2020
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. /*I was trying out ternary operator in this exercise, this program is 100% coded by me, just in case you wonder*/
  2.  
  3. #include <iostream>
  4. #include <cstring>
  5.  
  6. using namespace std;
  7.  
  8. class Vozac{
  9.     protected:
  10.         char name[100];
  11.         int age;
  12.         int noRaces;
  13.         bool isVeteran;
  14.     public:
  15.         Vozac(){
  16.             strcpy(this->name, "None");
  17.             this->age = 0;
  18.             this->noRaces = 0;
  19.             this->isVeteran = false;
  20.         }
  21.         Vozac(const char * name, const int age, const int noRaces, const bool isVeteran){
  22.             strcpy(this->name, name);
  23.             this->age = age;
  24.             this->noRaces = noRaces;
  25.             this->isVeteran = isVeteran;
  26.         }
  27.         virtual ~Vozac(){}    
  28.         virtual const double getSalaryPerRace() = 0;
  29.         virtual const double getTax() = 0;
  30.         friend ostream &operator << (ostream &os, Vozac &orig){
  31.             os << orig.name << "\n" << orig.age << "\n" << orig.noRaces << "\n";
  32.             if(orig.isVeteran)
  33.                 os << "VETERAN\n";
  34.             return os;
  35.         }
  36.         bool operator==(Vozac &orig){
  37.             return(this->getSalaryPerRace() == orig.getSalaryPerRace());
  38.         }
  39. };
  40.  
  41. class Avtomobilist : public Vozac{
  42.     private:
  43.         double price;
  44.     public:
  45.         Avtomobilist(){
  46.             this->price = 0.0;
  47.         }
  48.         Avtomobilist(const char * name, const int age, const int noRaces, const bool isVeteran, const double price) : Vozac(name, age, noRaces, isVeteran){
  49.             this->price = price;
  50.         }
  51.         ~Avtomobilist(){}
  52.         const double getSalaryPerRace(){
  53.             return this->price / 5;
  54.         }
  55.         const double getTax(){
  56.             return this->noRaces > 10 ? this->getSalaryPerRace() * 0.15 : this->getSalaryPerRace() * 0.1;
  57.         }
  58. };
  59.  
  60. class Motociklist : public Vozac{
  61.     private:
  62.         int power;
  63.     public:
  64.         Motociklist(){
  65.             this->power = 0;
  66.         }
  67.         Motociklist(const char * name, const int age, const int noRaces, const bool isVeteran, const int power) : Vozac(name, age, noRaces, isVeteran){
  68.             this->power = power;
  69.         }
  70.         ~Motociklist(){}
  71.         const double getSalaryPerRace(){
  72.             return this->power * 20;
  73.         }
  74.         const double getTax(){
  75.            return this->isVeteran ? this->getSalaryPerRace() * 0.25 : this->getSalaryPerRace() * 0.2;
  76.         }
  77. };
  78.  
  79. int soIstaZarabotuvachka(Vozac **v, int n, Vozac *vv){
  80.     int count = 0;
  81.     for(int i=0; i<n; i++)
  82.         if(*v[i] == *vv)
  83.             count++;
  84.     return count;
  85. }
  86.  
  87.  
  88.  
  89. int main() {
  90.     int n, x;
  91.     cin >> n >> x;
  92.     Vozac **v = new Vozac*[n];
  93.     char ime[100];
  94.     int vozrast;
  95.     int trki;
  96.     bool vet;
  97.     for(int i = 0; i < n; ++i) {
  98.         cin >> ime >> vozrast >> trki >> vet;
  99.         if(i < x) {
  100.             float cena_avto;
  101.             cin >> cena_avto;
  102.             v[i] = new Avtomobilist(ime, vozrast, trki, vet, cena_avto);
  103.         } else {
  104.             int mokjnost;
  105.             cin >> mokjnost;
  106.             v[i] = new Motociklist(ime, vozrast, trki, vet, mokjnost);
  107.         }
  108.     }
  109.     cout << "=== DANOK ===" << endl;
  110.     for(int i = 0; i < n; ++i) {
  111.         cout << *v[i];
  112.         cout << v[i]->getTax() << endl;
  113.     }
  114.     cin >> ime >> vozrast >> trki >> vet;
  115.     int mokjnost;
  116.     cin >> mokjnost;
  117.     Vozac *vx = new Motociklist(ime, vozrast, trki, vet, mokjnost);
  118.     cout << "=== VOZAC X ===" << endl;
  119.     cout << *vx;
  120.     cout << "=== SO ISTA ZARABOTUVACKA KAKO VOZAC X ===" << endl;
  121.     cout << soIstaZarabotuvachka(v, n, vx);
  122.     for(int i = 0; i < n; ++i) {
  123.         delete v[i];
  124.     }
  125.     delete [] v;
  126.     delete vx;
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement