![]() Ласло Краус |
Објектно програмирање - колоквијуми Други колоквијум 14. 6. 2008. |
|---|
I – Радник у трговачкој фирми има име и као плату добија задати проценат од вредности оствареног прихода. Написати на језику C++ апстрактну класу за раднике. Предвидети:
it<<radnik) у облику име/плата.
II – Продавац је радник који може да продаје робу. Ознака врсте радника је P. Приход је једнака укупној вредности продате робе. Написати на језику C++ класу за продавце као изведену класу из класе радника. Поред могућности основне класе предвидети:
III – Шеф је радник коме може бити потчињен задати број других радника. Ознака врсте радника је S. Приход шефа се рачуна као збир прихода потчињених. Написати на језику C++ класу за шефове као изведену класу из класе радника. Поред могућности основне класе предвидети:
IV – Трговачка фирма има задат број радних места и послује са задатом маржом (процентом од вредности продате робе која остаје фирми). Написати на језику C++ класу за фирме. Предвидети:
it<<firma – по један радник у сваком реду и добит фирме у последњем реду).
V – Написати на језику C++ програм који направи фирму с једним шефом и два продавца који су му потчињени, обави по једну продају са оба продавца и испише податке о фирми. Користити константне параметре (не треба ништа учитавати).
Грешке пријављивати изузецима типа једноставних класа које могу да уписују поруке о грешкама у излазни ток.
ispitx (x је број радне станице за којим се ради). Лозинка на свакој радној станици је student.I: (Nastava on \\Xs200-2).
// greske.h
#ifndef _greske_h_
#define _greske_h_
#include <iostream>
using namespace std;
class GPrevise {};
inline ostream& operator<<( ostream& it, const GPrevise&)
{ return it << "*** Previse radnika!"; }
class GNema {};
inline ostream& operator<<( ostream& it, const GNema&)
{ return it << "*** Nema radnika!"; }
#endif
// radnik.h
#ifndef _radnik_h_
#define _radnik_h_
#include <cstring>
#include <iostream>
using namespace std;
class Radnik {
char* ime;
double procenat;
Radnik( const Radnik&) {}
void operator=( const Radnik&) {}
public:
Radnik( const char* iime, double pproc) {
ime = strcpy( new char [strlen(iime)+1], iime);
procenat = pproc;
}
virtual ~Radnik() { delete [] ime; }
virtual char vrsta() const =0;
virtual double prihod() const =0;
double plata() const { return prihod() * procenat / 100; }
friend ostream& operator<<( ostream& it, const Radnik& r)
{ return it << r.ime << "/" << r.plata(); }
};
#endif
// prodavac.h
#ifndef _prodavac_h_
#define _prodavac_h_
#include "radnik.h"
class Prodavac: public Radnik {
double prih;
public:
Prodavac( const char* ime, double procenat): Radnik( ime, procenat) { prih = 0; }
char vrsta() const { return 'P'; }
Prodavac& prodao( double vrednost) { prih += vrednost; return *this; };
double prihod() const { return prih; }
};
#endif
// sef.h
#ifndef _sef_h_
#define _sef_h_
#include "radnik.h"
#include "greske.h"
class Sef: public Radnik {
Radnik** potcinjeni;
int kap, brPot;
public:
Sef( const char* ime, double procenat, int k=3): Radnik( ime, procenat) {
potcinjeni = new Radnik* [kap = k];
brPot = 0;
}
~Sef() { delete [] potcinjeni; }
char vrsta() const { return 'S'; }
Sef& dodeli( Radnik* r) {
if (brPot == kap) throw GPrevise();
potcinjeni[brPot++] = r;
return *this;
}
double prihod() const;
};
#endif
// sef.cpp
#include "sef.h"
double Sef::prihod() const {
double p = 0;
for (int i=0; i<brPot; p+=potcinjeni[i++]->prihod());
return p;
}
// firma.h
#ifndef _firma_h_
#define _firma_h_
#include "radnik.h"
#include "greske.h"
#include <iostream>
using namespace std;
class Firma {
Radnik** radnici;
int kap;
double marza;
Firma( const Firma&) {}
void operator=( const Firma&) {}
public:
Firma( double mar, int k=10);
~Firma();
Firma& zaposli (Radnik* r);
Firma& otpusti (int i) {
if (i<0 || i>=kap || !radnici[i]) throw GNema();
delete radnici[i]; radnici[i] = 0;
return *this;
}
double dobit() const;
friend ostream& operator<<( ostream& it, const Firma& fir);
};
#endif
// firma.cpp
#include "firma.h"
Firma::Firma( double mar, int k) {
marza = mar;
radnici = new Radnik* [kap = k];
for (int i=0; i<kap; radnici[i++]=0);
}
Firma::~Firma() {
for (int i=0; i<kap; delete radnici[i++]);
delete [] radnici;
}
Firma& Firma::zaposli (Radnik* r) {
int i=0; while (radnici[i]) i++;
if (i == kap) throw GPrevise();
radnici[i] = r;
return *this;
}
double Firma::dobit() const {
double d = 0, p = 0;
for (int i=0; i<kap; i++)
if (radnici[i]) {
if (radnici[i]->vrsta() == 'P') d += radnici[i]->prihod();
p += radnici[i]->plata();
}
return d * marza / 100 - p;
}
ostream& operator<<( ostream& it, const Firma& fir) {
for (int i=0; i<fir.kap; i++)
if (fir.radnici[i])
it << *fir.radnici[i] << endl;
return it << "Dobit firme: " << fir.dobit() << endl;
}
// test.cpp
#include "prodavac.h"
#include "sef.h"
#include "firma.h"
#include <iostream>
using namespace std;
int main () {
try {
Firma firma( 30, 5);
Prodavac* marko = new Prodavac( "Marko", 5);
Prodavac* zoran = new Prodavac( "Zoran", 4);
Sef* nenad = new Sef ( "Nenad", 6, 2);
firma.zaposli( marko);
firma.zaposli( zoran);
firma.zaposli( nenad);
nenad->dodeli( marko);
nenad->dodeli( zoran);
marko->prodao( 5000);
zoran->prodao( 8000);
cout << firma;
} catch (GPrevise g) { cout << g << endl; }
catch (GNema g) { cout << g << endl; }
}
Marko/250
Zoran/320
Nenad/780
Dobit firme: 2550
(садржај)
Copyright © 2010, Laslo Kraus
Последња ревизија: 4.10.2010.