Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Tema 6
- #include <iostream>
- #include <vector>
- #include <typeinfo>
- #include <exception>
- using namespace std;
- int op;
- class Abonament
- {
- protected:
- string nume_abonament;
- float pret;
- int perioada;
- public:
- Abonament(string nume="nume necunoscut", float pr=-1, int pe=-1)
- {
- nume_abonament=nume;
- pret=pr;
- pe=perioada;
- }
- Abonament(const Abonament &ob1)
- {
- nume_abonament=ob1.nume_abonament;
- pret=ob1.pret;
- perioada=ob1.perioada;
- }
- ~Abonament()
- {
- cout<<"un obiect de tip abonament a fost distrus"<<'\n';
- }
- string get_nume_abonament()
- {
- return nume_abonament;
- }
- float get_pret()
- {
- return pret;
- }
- int get_perioada()
- {
- return perioada;
- }
- void set_nume_abonament(string nume)
- {
- nume_abonament=nume;
- }
- void set_pret(float pre)
- {
- pret=pre;
- }
- void set_perioada(int per)
- {
- perioada=per;
- }
- friend ostream& operator <<(ostream &out, const Abonament& ob)
- {
- out<<"-Nume Abonament: "<<ob.nume_abonament<<'\n'<<"-Pret: "<<ob.pret<<'\n'<<"-Perioada: "<<ob.perioada<<'\n';
- return out;
- }
- friend istream& operator>>(istream& in, Abonament &ob)
- {
- cout<<"-Nume Abonament: ";
- in>>ob.nume_abonament;
- cout<<'\n';
- cout<<"-Pret: ";
- in>>ob.pret;
- cout<<'\n';
- cout<<"-Perioada: ";
- in>>ob.perioada;
- cout<<'\n';
- return in;
- }
- Abonament &operator =(Abonament &ob2)
- {
- if(this!=&ob2)
- {
- nume_abonament=ob2.nume_abonament;
- pret=ob2.pret;
- perioada=ob2.perioada;
- }
- return *this;
- }
- };
- class Abonament_Premium:public Abonament
- {
- protected:
- int reducere;
- public:
- Abonament_Premium(string nume="nume necunoscut", float pr=-1, int pe=-1, int re=0): Abonament(nume,pr,re)
- {
- reducere=re;
- }
- Abonament_Premium(const Abonament_Premium &ob1): Abonament(ob1)
- {
- reducere=ob1.reducere;
- }
- ~Abonament_Premium()
- {
- cout<<"Un obiect de tip abonament premium a fost distrus"<<'\n';
- }
- friend ostream& operator<<(ostream &out, const Abonament_Premium &ob)
- {
- out<<"-Nume Abonament: "<<ob.nume_abonament<<'\n'<<"-Pret: "<<ob.pret<<'\n'<<"-Perioada: "<<ob.perioada<<'\n'<<"-Reducere: "<<ob.reducere<<'\n';
- return out;
- }
- friend istream& operator>>(istream& in, Abonament_Premium &ob)
- {
- cout<<"-Nume Abonament: ";
- in>>ob.nume_abonament;
- cout<<'\n';
- cout<<"-Pret: ";
- in>>ob.pret;
- cout<<'\n';
- cout<<"-Perioada: ";
- in>>ob.perioada;
- cout<<'\n';
- cout<<"-Reducere: ";
- in>>ob.reducere;
- cout<<'\n';
- return in;
- }
- Abonament_Premium &operator =(Abonament_Premium &ob2)
- {
- if(this!=&ob2)
- {
- nume_abonament=ob2.nume_abonament;
- pret=ob2.pret;
- perioada=ob2.perioada;
- reducere=ob2.reducere;
- }
- return *this;
- }
- void set_reducere(int red)
- {
- reducere=red;
- }
- int get_reducere()
- {
- return reducere;
- }
- };
- class Persoana
- {
- protected:
- int id;
- string nume;
- string cnp;
- public:
- Persoana(int i=-1, string n="Nume necunoscut", string c="cnp necunoscut")
- {
- id=i;
- nume=n;
- cnp=c;
- }
- Persoana(const Persoana &ob1)
- {
- id=ob1.id;
- nume=ob1.nume;
- cnp=ob1.cnp;
- }
- ~Persoana()
- {
- cout<<"Un obiect de tip persoana a fost distrus"<<'\n';
- }
- int get_id()
- {
- return id;
- }
- string get_nume()
- {
- return nume;
- }
- string get_cnp()
- {
- return cnp;
- }
- void set_id(int i)
- {
- id=i;
- }
- void set_nume(string n)
- {
- nume=n;
- }
- void set_cnp(string c)
- {
- cnp=c;
- }
- friend ostream& operator<<(ostream &out, const Persoana &ob1)
- {
- out<<"-Id persoana: "<<ob1.id<<'\n'<<"-Nume: "<<ob1.nume<<'\n'<<"-CNP: "<<ob1.cnp<<'\n';
- return out;
- }
- friend istream& operator>>(istream &in, Persoana &ob1)
- {
- cout<<"-Id: ";
- in>>ob1.id;
- cout<<'\n'<<"-Nume: ";
- in>>ob1.nume;
- cout<<'\n'<<"-CNP: ";
- in>>ob1.cnp;
- cout<<'\n';
- return in;
- }
- Persoana &operator=(Persoana &ob2)
- {
- if(this!=&ob2)
- {
- id=ob2.id;
- nume=ob2.nume;
- cnp=ob2.cnp;
- }
- return *this;
- }
- };
- class Abonat:public Persoana
- {
- protected:
- string numar_telefon;
- Abonament x;
- public:
- Abonat(Abonament ab, int i=-1, string n="Nume necunoscut", string c="cnp necunoscut", string tel="Numar necunoscut"): Persoana(i,n,c)
- {
- numar_telefon=tel;
- x=ab;
- }
- Abonat(Abonat &ob1):Persoana(ob1)
- {
- numar_telefon=ob1.numar_telefon;
- x=ob1.x;
- x.set_nume_abonament(ob1.x.get_nume_abonament());
- x.set_pret(ob1.x.get_pret());
- x.set_perioada(ob1.x.get_perioada());
- }
- ~Abonat()
- {
- cout<<"Un obiect de tip Abonat a fost distrus"<<'\n';
- }
- friend ostream& operator<<(ostream &out, const Abonat &ob1)
- {
- out<<"-Id persoana: "<<ob1.id<<'\n'<<"-Nume: "<<ob1.nume<<'\n'<<"-CNP: "<<ob1.cnp<<'\n'<<"-Numar telefon: "<<ob1.numar_telefon<<'\n'<<"-Abonament: "<<ob1.x<<'\n';
- return out;
- }
- friend istream& operator>>(istream &in, Abonat &ob1)
- {
- cout<<"-Id: ";
- in>>ob1.id;
- cout<<'\n'<<"-Nume: ";
- in>>ob1.nume;
- cout<<'\n'<<"-CNP: ";
- in>>ob1.cnp;
- cout<<'\n';
- cout<<"-Numar de telefon: ";
- in>>ob1.numar_telefon;
- cout<<'\n';
- cout<<"-Abonament: ";
- in>>ob1.x;
- cout<<'\n';
- return in;
- }
- Abonat &operator=(Abonat &ob2)
- {
- if(this!=&ob2)
- {
- id=ob2.id;
- nume=ob2.nume;
- cnp=ob2.cnp;
- numar_telefon=ob2.numar_telefon;
- x=ob2.x;
- }
- return *this;
- }
- void set_numar_telefon(string num)
- {
- numar_telefon=num;
- }
- void set_Abonament(Abonament &ob1)
- {
- x=ob1;
- }
- string get_numar_telefon()
- {
- return numar_telefon;
- }
- Abonament get_x()
- {
- return x;
- }
- };
- class Clienti
- {
- protected:
- //Abonament **abon = new Abonament *[200]();
- vector<Abonament*> abon[200];
- int nr_ab = 0;
- static int suma;
- public:
- static void suma_init(int val)
- {
- suma=val;
- }
- void nr_abb_inc(int n)
- {
- nr_ab=nr_ab+n;
- }
- void add_ab()
- {
- nr_ab++;
- Abonament *a;
- Abonament aux;
- cin>>aux;
- a = new Abonament(aux);
- //cout << nr_ab;
- cout<<"ajuns ";
- //cout<<abon[nr_ab];
- try
- {
- //abon[nr_ab]=a;
- abon[nr_ab].push_back(a);
- }
- catch(...)
- {
- cout<<"Probleme la alocarea memoriei. Adaugarea nu s-a putut efectua."<<'\n';
- }
- }
- void add_ab_prem()
- {
- nr_ab++;
- Abonament_Premium *b;
- Abonament_Premium aux;
- cin>>aux;
- b= new Abonament_Premium(aux); ///Pointerii mei sunt inacesibili. abon[nr_abb] face figuri
- try
- {
- abon[nr_ab].push_back(b);
- }
- catch(...)
- {
- cout<<"Probleme la alocarea memoriei. Adaugarea nu s-a putut efectua."<<'\n';
- }
- }
- int nr_abon_premium()
- {
- int k=0;
- Abonament_Premium aux;
- for(int i=1; i<=nr_ab; i++)
- if(typeid(abon[i])==typeid(aux))
- k++;
- return k;
- }
- int get_suma()
- {
- for(int i=1; i<=nr_ab; i++)
- ///suma=suma+abon[i]->pret*abon[i]->perioada;
- return suma;
- }
- };
- int Clienti::suma;
- int main()
- {
- Clienti c;
- c.suma_init(0);
- cout<<"Acesta este meniul de navigare. Selectati optiunea pe care o doriti:"<<'\n';
- cout<<"1. Adauga abonati"<<'\n';
- cout<<"2. Afisare numar abonati premium"<<'\n';
- cout<<"3. Afisare suma de bani incasata de la toti abonatii curenti"<<'\n';
- cout<<"4. Inchide programul"<<'\n';
- while(op!=4)
- {
- cout<<"Optiunea dumneavoastra: ";
- cin>>op;
- cout<<'\n';
- if(op==1)
- {
- cout<<"Cati abonati doriti sa adaugati: ";
- int n;
- cin>>n;
- cout<<"Tipuri de abonamente:"<<'\n'<<"Standard = 1"<<'\n'<<"Premium = 2"<<'\n';
- for(int i=1; i<=n; i++)
- {
- cout<<"Abonatul "<<i<<" are abonament de tip: ";
- int tip;
- cin>>tip;
- try
- {
- switch(tip)
- {
- case 1:
- {
- c.add_ab();
- break;
- }
- case 2:
- {
- c.add_ab_prem();
- break;
- }
- }
- }
- catch(exception &e)
- {
- cout<<"S-a incercat si nu a mers. Eroare: "<<e.what()<<'\n';
- }
- ///suma
- }
- }
- if(op==2)
- {
- cout<<"Sunt "<<c.nr_abon_premium()<<" abonati premium"<<'\n';
- }
- if(op==3)
- {
- cout<<"Suma de bani incasata de la toti abonatii curenti este: "<<c.get_suma()<<'\n';
- }
- if(op==4)
- {
- cout<<"Programul s-a incheiat cu succes"<<'\n';
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement