Created
June 12, 2011 23:13
-
-
Save aleksmk/1022107 to your computer and use it in GitHub Desktop.
Random C++ code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstring> | |
#include <sstream> | |
using namespace std; | |
class Telefon { | |
private: | |
int godproiz; | |
int pocetnacena; | |
char model[20]; | |
public: | |
Telefon(int g=0, int pc=0, char *m=(char *)"") { | |
godproiz = g; | |
pocetnacena = pc; | |
strcpy(model, m); | |
//cout << "Povikan konstruktorot na osnovnata klasa" << endl; | |
}; | |
Telefon(Telefon &t) { | |
godproiz = t.GetGodinaNaProizvodstvo(); | |
pocetnacena = t.GetPocetnaCena(); | |
}; | |
int GetGodinaNaProizvodstvo() { return godproiz; }; | |
int GetPocetnaCena() { return pocetnacena; }; | |
char *GetModel() { return model; }; | |
void SetGodinaNaProizvodstvo(int g=0) { godproiz = g; }; | |
void SetPocetnaCena(int pc=0) { pocetnacena = pc; }; | |
void SetModel(char *m=(char *)"") { strcpy(model, m); }; | |
virtual float Presmetaj(int) = 0; | |
virtual string GetInfo() = 0; | |
friend ostream &operator<<(ostream &o, Telefon &t) { | |
//o << t.GetGodinaNaProizvodstvo() << "\t" << t.GetPocetnaCena() << "\t" << t.GetModel() << endl; | |
o << t.GetInfo(); | |
return o; | |
}; | |
}; | |
class Mobilen: public Telefon { | |
private: | |
float sirina; | |
float visina; | |
public: | |
Mobilen(float s=0, float v=0, int g=0, int pc=0, char *m=(char *)""): Telefon(g, pc, m) { | |
sirina=s; | |
visina=v; | |
//cout << "Povikan Mobilen() konstruktorot" << endl; | |
}; | |
Mobilen(Mobilen &m){ | |
SetGodinaNaProizvodstvo(m.GetGodinaNaProizvodstvo()); | |
SetPocetnaCena(m.GetPocetnaCena()); | |
SetModel(m.GetModel()); | |
sirina=m.GetSirina(); | |
visina=m.GetVisina(); | |
}; | |
void SetSirina(float s){ | |
sirina=s; | |
}; | |
void SetVisina(float v){ | |
visina=v; | |
}; | |
float GetVisina() {return visina;}; | |
float GetSirina() {return sirina;}; | |
float Presmetaj(int tekGod) { | |
int pom; | |
int i; | |
float cena=GetPocetnaCena(); | |
pom=tekGod-GetGodinaNaProizvodstvo(); | |
for (i=0; i<pom; i++) { | |
cena=cena*0.95; | |
}; | |
// cout << "Povikana Presmetaj() na Mobilen()" << endl; | |
return cena; | |
}; | |
string GetInfo() { | |
stringstream ss; | |
ss << GetGodinaNaProizvodstvo() << "\t" << GetPocetnaCena() << "\t" << GetModel() << "\t" << GetVisina() << "\t" << GetSirina() << endl; | |
string s; | |
s = ss.str(); | |
return s; | |
} | |
}; | |
class Fiksen: public Telefon { | |
private: | |
int tezina; // vo gramovi | |
public: | |
Fiksen(int t=0, int g=0, int pc=0, char *m=(char *)""): Telefon(g, pc, m) { | |
tezina=t; | |
//cout << "Povikan Fiksen() konstruktorot" << endl; | |
}; | |
Fiksen(Fiksen &f){ | |
SetGodinaNaProizvodstvo(f.GetGodinaNaProizvodstvo()); | |
SetPocetnaCena(f.GetPocetnaCena()); | |
SetModel(f.GetModel()); | |
tezina=f.GetTezina(); | |
}; | |
void SetTezina(int t){ | |
tezina=t; | |
}; | |
int GetTezina(){ return tezina; }; | |
float Presmetaj(int tekGod) { | |
int pom; | |
int i; | |
float cena=GetPocetnaCena(); | |
pom=tekGod-GetGodinaNaProizvodstvo(); | |
for (i=0; i<pom; i++) { | |
cena=cena*0.98; | |
}; | |
// cout << "Povikana Presmetaj() na Fiksen()" << endl; | |
return cena; | |
}; | |
string GetInfo() { | |
stringstream ss; | |
ss << GetGodinaNaProizvodstvo() << "\t" << GetPocetnaCena() << "\t" << GetModel() << "\t" << GetTezina() << endl; | |
string s; | |
s = ss.str(); | |
return s; | |
} | |
}; | |
class TelefonHolder { | |
private: | |
Telefon** Telefoni; | |
int BrojNaTelefoni; | |
const static int TekovnaGodina = 2011; | |
public: | |
TelefonHolder() { | |
BrojNaTelefoni = 0; | |
Telefoni = NULL; | |
//if (!tek) | |
//TekovnaGodina = 2011; | |
} | |
~TelefonHolder() { | |
if (BrojNaTelefoni) | |
delete [] Telefoni; | |
} | |
int Clear() { | |
if (BrojNaTelefoni) { | |
delete [] Telefoni; | |
BrojNaTelefoni = 0; | |
} | |
return 0; | |
}; | |
int Add(Telefon *t) { | |
Telefon** tmp; | |
tmp = new Telefon * [BrojNaTelefoni+1]; | |
for(int i=0; i<BrojNaTelefoni; i++) { | |
tmp[i] = Telefoni[i]; | |
} | |
tmp[BrojNaTelefoni] = t; | |
BrojNaTelefoni++; | |
if (Telefoni != NULL) | |
delete [] Telefoni; | |
Telefoni = tmp; | |
return 0; | |
}; | |
void Get() { | |
if (BrojNaTelefoni != 0) { | |
cout << "Broj na telefoni : " << BrojNaTelefoni << endl; | |
for(int i=0; i<BrojNaTelefoni; i++) { | |
cout << (*(Telefoni[i])) << endl; | |
} | |
} | |
}; | |
int GetTekovnaGodina() { return TekovnaGodina; }; | |
// da se dopravat | |
Telefon *NajEftin(int tek=TekovnaGodina) { | |
Telefon *najeftin = NULL; | |
if (Telefoni) { | |
najeftin = Telefoni[0]; | |
for(int i=1; i<BrojNaTelefoni; i++) { | |
if(Telefoni[i]->Presmetaj(tek) < najeftin->Presmetaj(tek)) { | |
najeftin = Telefoni[i]; | |
}; | |
}; | |
} | |
else | |
throw -1; | |
cout << "ok ?" << endl; | |
return najeftin; | |
}; | |
void GetMobilni() { | |
Telefon* tmp; | |
for(int i=0; i<BrojNaTelefoni; i++) { | |
if((tmp = dynamic_cast<Mobilen*>(Telefoni[i]))) { | |
cout << *tmp << endl; | |
} | |
} | |
}; | |
void GetFiksni() { | |
Telefon* tmp; | |
for(int i=0; i<BrojNaTelefoni; i++) { | |
if((tmp = dynamic_cast<Fiksen*>(Telefoni[i]))) { | |
cout << *tmp << endl; | |
} | |
} | |
}; | |
}; | |
int main() { | |
TelefonHolder Podatoci; | |
Telefon *f1 = new Fiksen(250, 1998, 1000, (char *)"Alcatel"); | |
//Telefon *f2 = new Fiksen(239, 1997, 1200, (char *)"Motorola"); | |
Telefon *f3 = new Fiksen(180, 2001, 2500, (char *)"BEKO"); | |
//Telefon *m1 = new Mobilen(20.0, 40.0, 2001, 6000, (char *)"NOKIA"); | |
Mobilen *m2 = new Mobilen(21.0, 35.0, 2010, 100, (char *)"HTC"); | |
//Telefon *m3 = new Mobilen(10.0, 23.0, 2011, 35000, (char *)"HTC"); | |
//Mobilen poi(10.0, 23.0, 2011, 35000, (char *)"HTC"); | |
//cout << poi.GetInfo(); | |
Podatoci.Add(f1); | |
Podatoci.Add(f3); | |
Podatoci.Add(m2); | |
Podatoci.Add(new Mobilen(10.0, 23.0, 2011, 35000, (char *)"HTC")); | |
Podatoci.Get(); | |
Podatoci.Clear(); | |
Podatoci.Get(); | |
/* try { | |
//cout << *(Podatoci.NajEftin()) << endl; | |
} | |
catch (int e) { | |
cout << "Fativme : " << ((e==-1) ? "Null point" : "" ) << endl; | |
} */ | |
//Podatoci.GetFiksni(); | |
return 0; // we didn't dun goofed | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment