Visa elektrotehnicka skola, Beograd
Ласло Краус
Објектно програмирање 1 - колоквијуми
Други колоквијум
15. 6. 2014.

Задатак | Напомене | Решење

Поставка задатка (решење | садржај)

Написати на језику C++ следеће класе (класе опремити оним конструкторима, деструктором и оператором за доделу вредности који су потребни за безбедно коришћење класа; грешке пријављивати изузецима типа једноставних класа које су опремљене исписивањем поруке):

Написати на језику C++ програм направи туристичку групу и испише је на главном излазу. У групи треба да се налазе један туриста и две групе по два туриста. Последње додавање у главну групу треба да не успе. Користити константне параметре (не треба ништа учитавати с главног улаза.


Напомене (садржај)


Решење задатка (поставка | садржај)

// zbirka.h

#ifndef _zbirka_h_
#define _zbirka_h_

#include <iostream&>
using namespace std;

class GPun {};
inline ostream& operator<<(ostream& it, const GPun&)
  { return it << "*** Zbirka je puna!"; }

template <typename T, int U&>
class Zbirka {
  T* niz; int kap, n;
  Zbirka(const Zbirka&) {}
  void operator=(const Zbirka&) {}
public:
  Zbirka(int k=10) { niz = new T[kap = k]; n = 0; }
  ~Zbirka();
  Zbirka& dodaj(T t) {
    if (n == kap) throw GPun();
    niz[n++] = t;
    return *this;
  }
  int duz() const { return n; }
  template <typename T, int U&>
  friend ostream& operator<<(ostream& it, const Zbirka<T,U&>& z);
};

template <typename T, int U&>
Zbirka<T,U&>::~Zbirka() {
  if (U == 2) for (int i=0; i<n; delete niz[i++]);
  delete [] niz;
}

template <typename T, int U&>
ostream& operator<<(ostream& it, const Zbirka<T,U&>& z) {
  for (int i=0; i<z.n; i++)
    if (U) it << *z.niz[i] << endl; else it <<  z.niz[i] << endl;
  return it;
}
#endif

// ucesnik.h #ifndef _ucesnik_h_ #define _ucesnik_h_ #include <iostream&> using namespace std; class Ucesnik { Ucesnik(const Ucesnik&) {} void operator=(const Ucesnik&) {} public: Ucesnik() {} virtual ~Ucesnik() {} virtual char vrsta() const =0; protected: virtual void pisi(ostream& it) const { it << vrsta() << ": "; } friend ostream& operator<<(ostream &it, const Ucesnik& u) { u.pisi(it); return it; } }; #endif
// turista.h #ifndef _turista_h_ #define _turiska_h_ #include "ucesnik.h" #include <iostream&> #include <cstring&> using namespace std; class Turista: public Ucesnik { char* ime; void pisi(ostream& it) const { Ucesnik::pisi(it); it << ime; } public: Turista(const char* i) { ime = strcpy(new char [strlen(i)+1], i); } ~Turista() { delete [] ime; } char vrsta() const { return 'T'; } }; #endif
// grupa.h #ifndef _grupa_h_ #define _grupa_h_ #include "ucesnik.h" #include "zbirka.h" #include <iostream&> using namespace std; class Grupa: public Ucesnik { static int posId; int id; Zbirka<Ucesnik*, 1&> grupa; void pisi(ostream& it) const { Ucesnik::pisi(it); it << id << " vvvvv" << endl; it << grupa; Ucesnik::pisi(it); it << id << " ^^^^^"; } public: Grupa(int k=5): grupa(k) { id = ++posId; } Grupa& dodaj(Ucesnik& u) { grupa.dodaj(&u); return *this; } char vrsta() const { return 'G'; } }; #endif
// grupa.cpp #include "grupa.h" int Grupa::posId = 0;
// program.cpp #include "turista.h" #include "grupa.h" #include <iostream&> using namespace std; int main() { Turista marko("Marko"), zoran("Zoran"), neda("Neda"), mira("Mira"), stefan("Stefan"), sonja("Sonja"); Grupa g1(2), g2(2), g3(4); try { g1.dodaj(marko).dodaj(zoran); g2.dodaj(neda).dodaj(mira); g3.dodaj(stefan).dodaj(g1).dodaj(sonja).dodaj(g2).dodaj(Turista("X")); } catch (GPun g) { cout << g << endl; } cout << g3 << endl; }
*** Zbirka je puna! G: 3 vvvvv T: Stefan G: 1 vvvvv T: Marko T: Zoran G: 1 ^^^^^ T: Sonja G: 2 vvvvv T: Neda T: Mira G: 2 ^^^^^ G: 3 ^^^^^
(садржај)
         
Аутор: Ласло Краус
Е-пошта: kraus@etf.rs

Copyright © 2014, Laslo Kraus
Последња ревизија: 15.6.2014.