Advertisement
developer10

SO :: Issue with dynamic_cast

Jan 3rd, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include<exception>
  3.  
  4. using namespace std;
  5.  
  6. /** ==================== S T A C K ================== **/
  7. template<class T>
  8. class Stack
  9. {
  10.     int _size;
  11.     int _top;
  12.     T* _elements;
  13. public:
  14.     Stack(int vel);
  15.     ~Stack();
  16.  
  17.     void Push(const T& element);
  18.     int GetSize()const;
  19.  
  20.     void Info();
  21.  
  22.     friend ostream& operator<< <>(ostream& COUT, Stack<T>& obj){
  23.         Odjeca* pok = NULL;
  24.         for (int i = 0; i < obj.GetSize(); i++){
  25.             // this is always NULL. Why?
  26.             // obj._elements[i] should be of type Odjeca, and I am putting "&" before it
  27.             pok = dynamic_cast<Odjeca*>(&(obj._elements[i]));
  28.  
  29.             if (pok != NULL){
  30.                 cout << "Odjeca called" << endl;
  31.                 pok->Info();
  32.             }
  33.             else{
  34.  
  35.                 COUT << obj._elements[i] << endl;
  36.             }
  37.         }
  38.         return COUT;
  39.     }
  40. };
  41.  
  42. template<class T>
  43. Stack<T>::Stack(int vel = 20)
  44. {
  45.     _size = vel;
  46.     _top = 0;
  47.     _elements = new T[_size];
  48. }
  49.  
  50. template<class T>
  51. Stack<T>::~Stack(){ delete _elements; _elements = nullptr; }
  52.  
  53. /** ==================================================== **/
  54.  
  55. template<class T>
  56. void Stack<T>::Push(const T& e)
  57. {
  58.     //if (IsFull())
  59.     //  throw NedozvoljenaOperacija("Stack je pun, nije moguce dodati novi element", __LINE__);
  60.  
  61.     _elements[_top++] = e;
  62. }
  63.  
  64. /** ==================================================== **/
  65.  
  66. template<class T>
  67. int Stack<T>::GetSize()const { return _top; }
  68.  
  69. template<class T>
  70. void Stack<T>::Info(){
  71.     for (int i = 0; i < _top; i++)
  72.         cout << _elements[i] << endl;
  73. }
  74.  
  75. /** ==================== S T A C K ================== **/
  76.  
  77.  
  78. /** ==================== P R O D U C T ================== **/
  79. class Proizvod
  80. {
  81.     char _sifra[10];
  82.     char* _naziv;
  83.     double _cijena;
  84.     double _popust;
  85. public:
  86.     Proizvod(char* sifra = "<sif>", char* naziv = "<naz>", double cijena = -1, double popust = -1)
  87.     {
  88.         strncpy_s(_sifra, sifra, _TRUNCATE);
  89.  
  90.         int vel = strlen(naziv) + 1;
  91.         _naziv = new char[vel];
  92.         strcpy_s(_naziv, vel, naziv);
  93.  
  94.         _cijena = cijena;
  95.         _popust = popust;
  96.     }
  97.  
  98.     virtual ~Proizvod(){ delete[] _naziv; _naziv = NULL; }
  99.  
  100.     Proizvod(const Proizvod& orig)
  101.     {
  102.         strncpy_s(_sifra, orig._sifra, _TRUNCATE);
  103.  
  104.         int vel = strlen(orig._naziv) + 1;
  105.         _naziv = new char[vel];
  106.         strcpy_s(_naziv, vel, orig._naziv);
  107.  
  108.         _cijena = orig._cijena;
  109.         _popust = orig._popust;
  110.     }
  111.  
  112.     Proizvod& operator=(const Proizvod& orig){
  113.         if (this == &orig)
  114.             return *this;
  115.  
  116.         strncpy_s(_sifra, orig._sifra, _TRUNCATE);
  117.  
  118.         delete[] _naziv;
  119.  
  120.         int vel = strlen(orig._naziv) + 1;
  121.         _naziv = new char[vel];
  122.         strcpy_s(_naziv, vel, orig._naziv);
  123.  
  124.         _cijena = orig._cijena;
  125.         _popust = orig._popust;
  126.  
  127.         return *this;
  128.     }
  129.  
  130.     virtual void Info(){
  131.         cout << "Sifra: " << _sifra << endl;
  132.         cout << "Naziv: \t" << _naziv << endl;
  133.         cout << "Cijena: " << _cijena << endl;
  134.         cout << "Popust: " << _popust << endl;
  135.     }
  136.  
  137.     char* GetSifra()const
  138.     {
  139.         int size = strlen(_sifra) + 1;
  140.         char* sifra = new char[size];
  141.         strcpy_s(sifra, size, _sifra);
  142.  
  143.         return sifra;
  144.     }
  145.  
  146.     double GetCijena()const { return _cijena; }
  147.     double GetPopust()const { return _popust; }
  148.  
  149.     friend ostream& operator<<(ostream& COUT, const Proizvod& obj){
  150.        
  151.         cout << "Sifraa: " << obj._sifra << endl;
  152.         cout << "Nazivv: \t" << obj._naziv << endl;
  153.         cout << "Cijenaa: " << obj._cijena << endl;
  154.         cout << "Popustt: " << obj._popust << endl;
  155.  
  156.         return COUT;
  157.     }
  158. };
  159.  
  160. /** ==================== P R O D U C T ================== **/
  161.  
  162.  
  163. /** ==================== C L O T H E S (PRODUCT) ================== **/
  164. class Odjeca :public Proizvod
  165. {
  166.     char* _velicine;        // sizes: 34, 36, 38 ili XS, S, M...
  167.     char* _boje;            // colors: crna, bijela, siva
  168. public:
  169.     Odjeca(char* sifra = "<sif>", char* naziv = "<naz>", double cijena = -1,
  170.         double popust = -1, char* velicine = "<vel>", char* boje = "<boj>") :Proizvod(sifra, naziv, cijena, popust)
  171.     {
  172.         int vel = strlen(velicine) + 1;
  173.         _velicine = new char[vel];
  174.         strcpy_s(_velicine, vel, velicine);
  175.  
  176.         vel = strlen(boje) + 1;
  177.         _boje = new char[vel];
  178.         strcpy_s(_boje, vel, boje);
  179.     }
  180.  
  181.     ~Odjeca() { delete[] _velicine, _boje; _velicine = _boje = NULL; }
  182.  
  183.     Odjeca(const Odjeca& orig) :Proizvod(orig){
  184.         int vel = strlen(orig._velicine) + 1;
  185.         _velicine = new char[vel];
  186.         strcpy_s(_velicine, vel, orig._velicine);
  187.  
  188.         vel = strlen(orig._boje) + 1;
  189.         _boje = new char[vel];
  190.         strcpy_s(_boje, vel, orig._boje);
  191.     }
  192.  
  193.     void Info(){
  194.         Proizvod::Info();
  195.         cout << "Velicine: " << _velicine << endl;
  196.         cout << "Boje: " << _boje << endl;
  197.     }
  198.  
  199.     friend ostream& operator<<(ostream& COUT, Odjeca& obj){
  200.         obj.Info();
  201.         cout << "Velicine: " << obj._velicine << endl;
  202.         cout << "Boje: " << obj._boje << endl;
  203.         return COUT;
  204.     }
  205. };
  206.  
  207. /** ==================== C L O T H E S (PRODUCT) ================== **/
  208.  
  209.  
  210.  
  211. /** ==================== O R D E R ================== **/
  212. class Narudzba
  213. {
  214.     int _brojNarudzbe;
  215.     Stack<Proizvod>* _proizvodi;
  216.     int _brojProizvoda;
  217. public:
  218.     Narudzba(int brNarudz = -1){
  219.         _brojNarudzbe = brNarudz;
  220.         _proizvodi = new Stack<Proizvod>(100);
  221.         _brojProizvoda = 0;
  222.     }
  223.  
  224.     void NaruciProizvod(const Proizvod& obj){
  225.         _proizvodi->Push(obj);
  226.         _brojProizvoda++;
  227.     }
  228.  
  229.     friend ostream& operator<<(ostream& COUT, Narudzba& obj)
  230.     {
  231.         COUT << "Br. narudzbe: " << obj._brojNarudzbe << endl;
  232.         COUT << "Ukupno proizvoda: " << obj._brojProizvoda << endl << endl;
  233.         COUT << "===== PROIZVODI U NARUDZBI =====" << endl;
  234.  
  235.         COUT << *obj._proizvodi;
  236.  
  237.         return COUT;
  238.     }
  239. };
  240.  
  241. /** ==================== O R D E R ================== **/
  242.  
  243.  
  244. void main()
  245. {
  246.     Proizvod p1("gz214", "Stolna lampa", 49, 0.03);
  247.     Proizvod p2("rd874", "Stolica kuhinjska", 74, 0.04);
  248.     Odjeca o1("od765", "Dukserica", 45, 0.15, "XS, S, M, XL", "crna, crvena, bijela");
  249.     Proizvod p3("iu852", "Vjesalica zidna", 8, 0.12);
  250.     Odjeca o2("od112", "Majica", 35, 0.05, "M, S, X, XL, XXL", "crna, plava, zelena, bijela");
  251.  
  252.     // Order
  253.     Narudzba narudzba(1);
  254.  
  255.     // adding products to the order
  256.     narudzba.NaruciProizvod(p1);
  257.     narudzba.NaruciProizvod(p3);
  258.  
  259.     // this item is of type Odjeca /Clothes/ so it should have
  260.     // the two additional attributes displayed (_boje, _velicine => _colors, _sizes)
  261.     narudzba.NaruciProizvod(o2);
  262.  
  263.     cout << narudzba << endl;
  264.  
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement