metalni

OOP Labs 7 Zicani instrumenti

Jun 2nd, 2020
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. class ZicanInstrument {
  7.     private:
  8.         char name[20];
  9.         int noStrings;
  10.         float price;
  11.     public:
  12.         ZicanInstrument();
  13.         ZicanInstrument(const char * ime, const int brojZici, const float cena);
  14.         ~ZicanInstrument();
  15.         bool operator==(const ZicanInstrument *orig);
  16.         virtual float cena();
  17. };
  18.  
  19. ZicanInstrument::ZicanInstrument(){
  20.     strcpy(this->name, "None");
  21.     this->noStrings = 0;
  22.     this->price = 0.0;
  23. }
  24. ZicanInstrument::ZicanInstrument(const char * ime, const int brojZici, const float cena){
  25.     strcpy(this->name, ime);
  26.     this->noStrings = brojZici;
  27.     this->price = cena;
  28. }
  29. ZicanInstrument::~ZicanInstrument(){}
  30. bool ZicanInstrument::operator==(const ZicanInstrument *orig){
  31.     if(this->noStrings == orig->noStrings)
  32.         return true;
  33.     else
  34.         return false;
  35. }
  36. float ZicanInstrument::cena(){
  37.     return this->price;
  38. }
  39.  
  40. //Mandolina
  41. class Mandolina : public ZicanInstrument {
  42.     private:
  43.         char shape[20];
  44.     public:
  45.         Mandolina();
  46.         Mandolina(const char * ime, const int brojZici, const float cena, const char * forma) : ZicanInstrument(ime, brojZici, cena){
  47.             strcpy(this->shape, forma);        
  48.         }
  49.         ~Mandolina();
  50.         float cena();
  51. };
  52.  
  53. Mandolina::Mandolina(){
  54.     strcpy(this->shape, "N/A");
  55. }
  56. Mandolina::~Mandolina(){}
  57. float Mandolina::cena(){
  58.     if(!strcmp(this->shape, "Neapolitan"))
  59.         return ZicanInstrument::cena() + ZicanInstrument::cena() * 0.15;
  60.     else return ZicanInstrument::cena();
  61. }
  62.  
  63. //Violina
  64. class Violina : public ZicanInstrument{
  65.     private:
  66.         double size;
  67.     public:
  68.         Violina();
  69.         Violina(const char * ime, const int brojZici, const float cena, const float golemina) : ZicanInstrument(ime, brojZici, cena){
  70.             this->size = golemina;
  71.         }
  72.         ~Violina();
  73.         float cena();
  74. };
  75.  
  76. Violina::Violina(){
  77.     this->size = 0.0;
  78. }
  79. Violina::~Violina(){}
  80. float Violina::cena(){
  81.     if(this->size == 0.25)
  82.         return ZicanInstrument::cena() + ZicanInstrument::cena() * 0.1;
  83.     else if(this->size == 1.00)
  84.         return ZicanInstrument::cena() + ZicanInstrument::cena() * 0.2;
  85.     else
  86.         return ZicanInstrument::cena();
  87. }
  88.  
  89. //global function
  90. void pecatiInstrumenti(ZicanInstrument &zi, ZicanInstrument **x, int n){
  91.     for(int i=0; i<n; i++){
  92.         if(zi == x[i])
  93.             cout << x[i]->cena() << endl;
  94.     }
  95. }
  96.  
  97. //main
  98. int main() {
  99.     char ime[20];
  100.     int brojZici;
  101.     float cena;
  102.     char forma[20];
  103.     cin >> ime >> brojZici >> cena >> forma;
  104.     Mandolina m(ime, brojZici, cena, forma);
  105.     int n;
  106.     cin >> n;
  107.     ZicanInstrument **zi = new ZicanInstrument*[2 * n];
  108.     for(int i = 0; i < n; ++i) {
  109.         cin >> ime >> brojZici >> cena >> forma;
  110.         zi[i] = new Mandolina(ime, brojZici, cena, forma);
  111.     }
  112.     for(int i = 0; i < n; ++i) {
  113.         float golemina;
  114.         cin >> ime >> brojZici >> cena >> golemina;
  115.         zi[n + i] = new Violina(ime, brojZici, cena, golemina);
  116.     }
  117.     pecatiInstrumenti(m, zi, 2 * n);
  118.     for(int i = 0; i < 2 * n; ++i) {
  119.         delete zi[i];
  120.     }
  121.     delete [] zi;
  122.     return 0;
  123. }
Add Comment
Please, Sign In to add comment