1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 | #include <iostream> #include <string> using namespace std; class Produkt { private: string name; double cena; int rabat; bool czy_jest_promocja() { return (rabat > 0 && rabat < 100); } public: Produkt() {} Produkt(string name, double cena, int rabat) { this->name = name; this->cena = cena; this->rabat = rabat; cout << to_string(); } void ustaw_promocje(int rabat) { this->rabat = rabat; } string to_string() { double cena1 = 1; if(czy_jest_promocja()) { cena1 = this->cena * (1.0 - this->rabat / 100.0); } else { cena1 = this->cena; } string c = name + " " + std::to_string(cena1) + "\n"; return c; } static double get_real_price(Produkt pro) { double cena1 = 1; if(pro.czy_jest_promocja()) { cena1 = pro.cena * (1.0 - pro.rabat / 100.0); } else { cena1 = pro.cena; } return cena1; } static Produkt najdrozszy_produkt(Produkt* tab, int size) { int maxindex = 0; double maxprice = get_real_price(tab[0]); for (int i = 1; i < size; i++) { if(get_real_price(tab[i]) > maxprice) { maxindex = i; } } return tab[maxindex]; } }; int main() { Produkt* produkty = new Produkt[4]; produkty[0] = Produkt("cos", 80, 50); produkty[1] = Produkt("produkt2", 1, 80); produkty[2] = Produkt("costam", 11.9, 30); produkty[3] = Produkt("produkt2", 42.0, 69); produkty[2].ustaw_promocje(60); Produkt najdrozszy = Produkt::najdrozszy_produkt(produkty, 4); cout << "najdrozszy przedmiot to " << najdrozszy.to_string(); delete produkty; } |