0% found this document useful (0 votes)
26 views10 pages

Persoana Adresa Adr Char Nume: Agregare Has A Derivare Is A

This document provides an example of code reuse through class aggregation and derivation. It defines classes like Address, Person, and Employee where Employee derives from Person. Address is aggregated as an attribute of Person. Methods and operators are defined to allow interaction between the classes while reusing code. The main function demonstrates storing and interacting with objects of these classes polymorphically.

Uploaded by

florinhorop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views10 pages

Persoana Adresa Adr Char Nume: Agregare Has A Derivare Is A

This document provides an example of code reuse through class aggregation and derivation. It defines classes like Address, Person, and Employee where Employee derives from Person. Address is aggregated as an attribute of Person. Methods and operators are defined to allow interaction between the classes while reusing code. The main function demonstrates storing and interacting with objects of these classes polymorphically.

Uploaded by

florinhorop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Exemplu de reutilizarea codului prin agregare si derivare

Persoana
Adresa adr
char* nume
derivare
Angajat
+salariu

agregare
has a
is a

Adresa
char *str
int nr

#include <iostream>
using namespace std;
class Adresa {
private: char* str;
int nr;
public:
Adresa(){} //necesar la crearea obiectelor de tip Persoana
Adresa(char* c, int n):nr(n) //necesar in constructorul din Persoana
//se apeleaza pseudoconstructorul pentru nr de tip int;
//pseudoconstructorii se folosesc pentru initializarea tipurilor de
date de baza
{
str=new char[(strlen(c))+1];
strcpy(str,c);
}
Adresa(const Adresa&p)
//se foloseste la crearea de obiecte de tip Persoana
{
str=new char[strlen(p.str)+1];
strcpy(str,p.str);
nr=p.nr;
//sau:
*this=p; // folosesc operator= implementat pentru Adresa

Adresa& operator=(const Adresa &p)


{ //in Persoana se foloseste atribuirea de obiecte de tip Adresa
if(this!=&p)
{
if (!str) delete [] str;
str=new char[strlen(p.str)+1];
strcpy(str,p.str);
nr=p.nr;
}
return *this;
}
friend ostream& operator<<(ostream &dev,const Adresa &p)
{ // in operatorul de << din Persoana se foloseste operatorul
de << din Adresa
dev<<"Strada: "<<p.str<<endl;
dev<<"Nr. : "<<p.nr<<endl<<endl;
return dev;
}
~Adresa()
{
delete [] str;
}

class Persoana {
protected: Adresa adr; //agregare
char *nume;
public: Persoana(){}
//apel automat adr()
Persoana(const Adresa &d, char* n):adr(d) //construiesc atributu
adr de tip Adresa
{
nume= new char[strlen(n)+1];
// altfel se apela
automat adr()
strcpy(nume,n);
}
Persoana(char* s, int nr, char* n):adr(s,nr) //construiesc atributul
de tip Adresa
{
nume= new char[strlen(n)+1];
strcpy(nume,n);
}
//
Persoana(const Persoana & p)
// se apeleaza automat adr()
//
{
//
adr = p.adr;
//
nume= new char[strlen(p.nume)+1];
//
strcpy(nume,p.nume);
//
}
//sau:
Persoana(const Persoana & p):adr(p.adr) //construiesc atributul a
de tip Adresa

Persoana& operator=(const Persoana & p)


{
adr = p.adr;
//atribuire intre obiecte de tip Adresa folosind
Adresa::operator=
nume= new char[strlen(p.nume)+1];
strcpy(nume,p.nume);
return *this;
}
void afis(){
cout<<"Nume: "<<nume<<endl;
cout<<"Adresa: "<<endl;
cout<<adr;
//afisare obiect de tip Adresa folosind operator<< din
Adresa
}
//sau: se supradefineste operator<<
~Persoana()
{
delete [] nume;
} //se apeleaza automat destructorul din Adresa

Reutilizare
cod

class Angajat:public Persoana {


private:
//mosteneste toate atributele din Persoana si are in plus
double salariu;
public:
Angajat()//apel automat constructor Persoana()
{
}
Angajat(const Adresa &d, char* n,double
s):Persoana(d,n),salariu(s)
//apelez constructorul clasei de baza Persoana, cu parametrii
{
}
Angajat(char* s, int nr, char* n, double sal):Persoana(s, nr,
n),salariu(sal)
{
}
Angajat(const Angajat & p):Persoana(p) //apel explicit
constructor copiere din Persoana
{

Angajat& operator=(const Angajat & p)


{
(Persoana&)(*this)=p;
// convertesc obiectul de la adresa lui this la tipul
de date Persoana&
//folosesc operatorul de atribuire din clasa
Persoana ; automat p este
//convertit la tipul de date de
baza; puteam sa scriu si (Persoana)p;
salariu=p.salariu;
return *this;
}
void afis(){
((Persoana*)(this))->afis();
// il convertesc pe this la tipul de date Persoana*
//apelez functia afis din Persoana
//sau Persoana::afis();
cout<<"Salariu: "<<salariu<<endl;
}
};
Reutilizare
cod derivare

int main()
{
//ideea
Persoana *c;
Adresa a("str",10);
Angajat ang(a,ang",10);
c=&ang;
//un pointer de tip Persoana poate sa pointeze catre o zona de
memorie de tip Angajat
((Angajat*)c)->afis();
//convertesc ce gasesc la adresa la care pointeaza c - la un pointer de
tip Angajat
//apelez functia de afisare din clasa Angajat => str, 10 ang 10
int n;
cin>>n;
char *num=new char[2];
char *str=new char[2];
int nr;
double sal;
bool *t=new bool[n];
//un vector in care memorez daca persoana e angajata sau nu

Persoana **evidenta=new Persoana*[n];


//un vector in care vreau sa stochez atat obiecte de tip Persoana* cat si
Angajat*
for (int i=0;i<n;i++){
cout<<"Adaugati salariat sau nesalariat?";
bool tip;cin>>tip;
t[i]=tip;
if (tip==true) {
cout<<"Nume: "; cin>>num;
cout<<"Str. "; cin>>str;
cout<<"Nr."; cin>>nr;
cout<<"Salariu."; cin>>sal;
evidenta[i]=new Angajat(str,nr,num,sal);
}else
{
cout<<"Nume: "; cin>>num;
cout<<"Str. "; cin>>str;
cout<<"Nr."; cin>>nr;
evidenta[i]=new Persoana(str,nr,num);
}
}

cout<<"_______________________________"<<endl;
for (int i=0;i<n;i++)
if (t[i]==true) {
((Angajat*)(evidenta[i]))->afis();
cout<<"____________________"<<endl;
}else {
evidenta[i]->afis();
cout<<"____________________"<<endl;
}
system("PAUSE");
return 1;
}

You might also like