Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<exception>
- using namespace std;
- /** ==================== S T A C K ================== **/
- template<class T>
- class Stack
- {
- int _size;
- int _top;
- T* _elements;
- public:
- Stack(int vel);
- ~Stack();
- void Push(const T& element);
- int GetSize()const;
- void Info();
- friend ostream& operator<< <>(ostream& COUT, Stack<T>& obj){
- Odjeca* pok = NULL;
- for (int i = 0; i < obj.GetSize(); i++){
- // this is always NULL. Why?
- // obj._elements[i] should be of type Odjeca, and I am putting "&" before it
- pok = dynamic_cast<Odjeca*>(&(obj._elements[i]));
- if (pok != NULL){
- cout << "Odjeca called" << endl;
- pok->Info();
- }
- else{
- COUT << obj._elements[i] << endl;
- }
- }
- return COUT;
- }
- };
- template<class T>
- Stack<T>::Stack(int vel = 20)
- {
- _size = vel;
- _top = 0;
- _elements = new T[_size];
- }
- template<class T>
- Stack<T>::~Stack(){ delete _elements; _elements = nullptr; }
- /** ==================================================== **/
- template<class T>
- void Stack<T>::Push(const T& e)
- {
- //if (IsFull())
- // throw NedozvoljenaOperacija("Stack je pun, nije moguce dodati novi element", __LINE__);
- _elements[_top++] = e;
- }
- /** ==================================================== **/
- template<class T>
- int Stack<T>::GetSize()const { return _top; }
- template<class T>
- void Stack<T>::Info(){
- for (int i = 0; i < _top; i++)
- cout << _elements[i] << endl;
- }
- /** ==================== S T A C K ================== **/
- /** ==================== P R O D U C T ================== **/
- class Proizvod
- {
- char _sifra[10];
- char* _naziv;
- double _cijena;
- double _popust;
- public:
- Proizvod(char* sifra = "<sif>", char* naziv = "<naz>", double cijena = -1, double popust = -1)
- {
- strncpy_s(_sifra, sifra, _TRUNCATE);
- int vel = strlen(naziv) + 1;
- _naziv = new char[vel];
- strcpy_s(_naziv, vel, naziv);
- _cijena = cijena;
- _popust = popust;
- }
- virtual ~Proizvod(){ delete[] _naziv; _naziv = NULL; }
- Proizvod(const Proizvod& orig)
- {
- strncpy_s(_sifra, orig._sifra, _TRUNCATE);
- int vel = strlen(orig._naziv) + 1;
- _naziv = new char[vel];
- strcpy_s(_naziv, vel, orig._naziv);
- _cijena = orig._cijena;
- _popust = orig._popust;
- }
- Proizvod& operator=(const Proizvod& orig){
- if (this == &orig)
- return *this;
- strncpy_s(_sifra, orig._sifra, _TRUNCATE);
- delete[] _naziv;
- int vel = strlen(orig._naziv) + 1;
- _naziv = new char[vel];
- strcpy_s(_naziv, vel, orig._naziv);
- _cijena = orig._cijena;
- _popust = orig._popust;
- return *this;
- }
- virtual void Info(){
- cout << "Sifra: " << _sifra << endl;
- cout << "Naziv: \t" << _naziv << endl;
- cout << "Cijena: " << _cijena << endl;
- cout << "Popust: " << _popust << endl;
- }
- char* GetSifra()const
- {
- int size = strlen(_sifra) + 1;
- char* sifra = new char[size];
- strcpy_s(sifra, size, _sifra);
- return sifra;
- }
- double GetCijena()const { return _cijena; }
- double GetPopust()const { return _popust; }
- friend ostream& operator<<(ostream& COUT, const Proizvod& obj){
- cout << "Sifraa: " << obj._sifra << endl;
- cout << "Nazivv: \t" << obj._naziv << endl;
- cout << "Cijenaa: " << obj._cijena << endl;
- cout << "Popustt: " << obj._popust << endl;
- return COUT;
- }
- };
- /** ==================== P R O D U C T ================== **/
- /** ==================== C L O T H E S (PRODUCT) ================== **/
- class Odjeca :public Proizvod
- {
- char* _velicine; // sizes: 34, 36, 38 ili XS, S, M...
- char* _boje; // colors: crna, bijela, siva
- public:
- Odjeca(char* sifra = "<sif>", char* naziv = "<naz>", double cijena = -1,
- double popust = -1, char* velicine = "<vel>", char* boje = "<boj>") :Proizvod(sifra, naziv, cijena, popust)
- {
- int vel = strlen(velicine) + 1;
- _velicine = new char[vel];
- strcpy_s(_velicine, vel, velicine);
- vel = strlen(boje) + 1;
- _boje = new char[vel];
- strcpy_s(_boje, vel, boje);
- }
- ~Odjeca() { delete[] _velicine, _boje; _velicine = _boje = NULL; }
- Odjeca(const Odjeca& orig) :Proizvod(orig){
- int vel = strlen(orig._velicine) + 1;
- _velicine = new char[vel];
- strcpy_s(_velicine, vel, orig._velicine);
- vel = strlen(orig._boje) + 1;
- _boje = new char[vel];
- strcpy_s(_boje, vel, orig._boje);
- }
- void Info(){
- Proizvod::Info();
- cout << "Velicine: " << _velicine << endl;
- cout << "Boje: " << _boje << endl;
- }
- friend ostream& operator<<(ostream& COUT, Odjeca& obj){
- obj.Info();
- cout << "Velicine: " << obj._velicine << endl;
- cout << "Boje: " << obj._boje << endl;
- return COUT;
- }
- };
- /** ==================== C L O T H E S (PRODUCT) ================== **/
- /** ==================== O R D E R ================== **/
- class Narudzba
- {
- int _brojNarudzbe;
- Stack<Proizvod>* _proizvodi;
- int _brojProizvoda;
- public:
- Narudzba(int brNarudz = -1){
- _brojNarudzbe = brNarudz;
- _proizvodi = new Stack<Proizvod>(100);
- _brojProizvoda = 0;
- }
- void NaruciProizvod(const Proizvod& obj){
- _proizvodi->Push(obj);
- _brojProizvoda++;
- }
- friend ostream& operator<<(ostream& COUT, Narudzba& obj)
- {
- COUT << "Br. narudzbe: " << obj._brojNarudzbe << endl;
- COUT << "Ukupno proizvoda: " << obj._brojProizvoda << endl << endl;
- COUT << "===== PROIZVODI U NARUDZBI =====" << endl;
- COUT << *obj._proizvodi;
- return COUT;
- }
- };
- /** ==================== O R D E R ================== **/
- void main()
- {
- Proizvod p1("gz214", "Stolna lampa", 49, 0.03);
- Proizvod p2("rd874", "Stolica kuhinjska", 74, 0.04);
- Odjeca o1("od765", "Dukserica", 45, 0.15, "XS, S, M, XL", "crna, crvena, bijela");
- Proizvod p3("iu852", "Vjesalica zidna", 8, 0.12);
- Odjeca o2("od112", "Majica", 35, 0.05, "M, S, X, XL, XXL", "crna, plava, zelena, bijela");
- // Order
- Narudzba narudzba(1);
- // adding products to the order
- narudzba.NaruciProizvod(p1);
- narudzba.NaruciProizvod(p3);
- // this item is of type Odjeca /Clothes/ so it should have
- // the two additional attributes displayed (_boje, _velicine => _colors, _sizes)
- narudzba.NaruciProizvod(o2);
- cout << narudzba << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement