Sunteți pe pagina 1din 8

Universitatea Tehnic a Moldovei

Ministerul educaiei i tineretului

Universitatea Tehnic a Moldovei


Lucrarea de laborator Nr. 4
La disciplina C++
Tema: Motenirea i compoziia.

A efectuat:
studentu gr. TI-142

Chifa Vladislav

A controlat:

Potlog Mihail

Chisinau 2015

1. Sarcina lucrrii:
a) S se defineasc clasa Om, care are nume (pointer spre cmp) , vrst, greutate. S se defineasc
constructorii, destructorul i operatorul de atribuire. S se defineasc o clas derivat Major , care are
numrul paaportului . S se defineasc constructorii implicit i cu numr diferit de parametri, destructorul ,
operatorii de extragere n flux. S se defineasc funcii de setare a vrstei i a numrului paaportului.
b)S se defineasc o clas Vaca care are urmtoarele cmpuri: numr de identificare treSbuie s fie
garantat unic (pentru aceasta s se foloseasc un contor static), mulsoare medie, vrsta, porecl i soi. Pentru
iniializarea cmpurilor s se foloseasc operatorul new. S se defineasc o clas Ciread, alctuit dintr-un
numr nelimitat de vaci. S se defineasc metode de introducere, de determinare a cantitii medii de lapte
pe ciread i a cantitii totale de lapte, constructorii, destructorii i alte funcii necesare.
Listingul programului:
a)

#include <iostream>
#include <stdlib.h>
#include <string.h>

using namespace std;


class Om{
protected:
char *name;
int age,kg;
public:
Om();
Om(char*,int,int);
Om(const Om&);
virtual ~Om();
Om & operator =(const Om &);
friend istream&operator >>(istream&, Om&);
friend ostream&operator <<(ostream &, Om&);
};
class Major : public Om{
protected:
long long num;
public:
Major();
Major(char *,int,int,long long);
Major(long long,Om&);
Major(const Major &);
~Major();
Major & operator =(const Major&);
friend istream&operator >>(istream&, Major&);
friend ostream&operator <<(ostream &, Major&);
void setNrPassport(long long pass){num=pass;}
long long getNrPassport(){return num;}
};
Om::Om():name(NULL),age(0),kg(0){}
Om::Om(char *name,int age,int kg):name(name),age(age),kg(kg){
this->name=new char[strlen(name)+1];
strcpy(this->name,name);

}
Om::Om(const Om & obj):age(obj.age),kg(obj.kg),name(new char[strlen(obj.name)+1]){
strcpy(name,obj.name);
}
Om::~Om(){
if(name) delete[] name;
name=NULL;
age=0;
kg=0;
}
Om&Om::operator =( const Om & obj){
if(this==&obj) return *this;
name=new char[strlen(obj.name)+1];
strcpy(name,obj.name);
age=obj.age;
kg=obj.kg;
return *this;
}
istream & operator >>(istream & in, Om & obj){
cout<<"Introduceti numele:"<<endl;
in>>obj.name;
cout<<"Introduceti virsta:"<<endl;
in>>obj.age;
cout<<"Introduceti kg:"<<endl;
in>>obj.kg;
return in;
}
ostream & operator <<(ostream & out, Om & obj){
return
out<<"Numele:"<<obj.name<<"\nVirsta:"<<obj.age<<"\nMasa:"<<obj.kg<<"kg"<<endl;
}
Major::Major():Om(),num(0){}
Major::Major(char *name,int age,int kg,long long num):Om(name,age,kg),num(num){}
Major::Major(long long pass,Om& obj):Om(obj){ num=pass;}
Major::Major(const Major&obj):Om(obj),num(obj.num){}
Major::~Major(){ num=0;}
Major&Major:: operator =(const Major & obj){
if(this==&obj) return *this;
this->name=new char[strlen(obj.name)+1];
strcpy(this->name,obj.name);
this->age=obj.age;
this->kg=obj.kg;
this->num=obj.num;
return *this;
}
istream & operator >>(istream & in, Major & obj){
in>>*((Om *)(&obj));
cout<<"Introduceti numarul pasaportului: "<<endl;
in>>obj.num;
return in;
}
ostream & operator <<(ostream & out, Major & obj){

out<<*((Om*)(&obj));
out<<"Numarul pasaportului:"<<obj.num<<endl;
return out;
}
int main()
{
Om p("Chifa_Vlad",19,64);
Major m(325346,p);
Major m2=m;
cout<<"Clasa Om:"<<endl;
cout<<(cout,p);
cout<<endl;
cout<<"Clasa mostenitoare:"<<endl;
cout<<(cout,m);
cout<<endl;
system("pause");
return 0;
}

b)
#include <iostream>
#include <string.h>
//#include <conio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
// Clasa de baza
class vaca {
protected:
string name;
string rasa;
float vol;
unsigned age;
int id;
public:
vaca() :name("noname"), rasa("X"), vol(0), age(0) {

id = (rand() % 20) + 1;
}
vaca(string name,string rasa,int vol,int
age):name(name),rasa(rasa),vol(vol),age(age){ id = (rand() % 20) + 1; }
vaca(const vaca & obj) :name(obj.name), rasa(obj.rasa), vol(obj.vol),
age(obj.age), id(obj.id) {}
~vaca() { name = "?"; rasa = "?"; vol = 0;age = 0;id = 0; }
float getVol() { return vol; }
int getId() { return id; }
vaca & operator =(vaca &);
friend ostream& operator <<(ostream &, vaca &);
friend istream& operator >>(istream &, vaca &);

};
vaca&vaca::operator =(vaca & obj) {
if (this == &obj) return *this;
name = obj.name;
rasa = obj.rasa;
vol = obj.vol;
age = obj.age;
id = obj.id;
return *this;
}
istream & operator >>(istream & in, vaca & v) {
cout<<"Name:"; in>>v.name;
cout<<"rasa:"; in>>v.rasa;
cout<<"V lapte [l/zi]:"; in>>v.vol;
cout<<"Virsta:"; in>>v.age;
return in;
}
ostream & operator <<(ostream & out, vaca & v) {
out<<"\nID:"<<v.id;
out<<"\nName: "<<v.name;
out<<"\nRasa: "<<v.rasa;
out<<"\nVol lapte [l/zi]: "<<v.vol;
out<<"\nVirsta: "<<v.age<<" ani";
return out;
}
//

Clasa mostenotoare (care va mosteni toate cimpurile din clasa vaca)

class cireada : public vaca {


protected:
int size;
public:
vaca **ptr;
cireada() :vaca(), size(0) {
ptr = NULL;
}
cireada(int size) :vaca(), size(size) {
ptr = new vaca *[size];
for (int i = 0; i < size; ++i)

ptr[i] = new vaca;


}
cireada(const cireada & obj) :size(size), vaca(obj) {
size = obj.size;
ptr = new vaca*[this->size];
for (int i = 0;i<this->size;i++)
ptr[i] = new vaca(*(obj.ptr[i]));
}
~cireada() {
for (int i = 0;i<size;i++)
delete ptr[i];
delete ptr;
size = 0;
}
int getSize() { return size; }
float volMed();
cireada & operator =(cireada &);
friend istream & operator >>(istream &, cireada &);
friend ostream & operator <<(ostream &, cireada &);
cireada& add(vaca &);
cireada & elimin(int);
};
cireada&cireada::operator =(cireada & obj) {
if (this == &obj) return *this;
size = obj.size;
name = obj.name;
rasa = obj.rasa;
vol = obj.vol;
age = obj.age;
id = obj.id;
return *this;
}
istream & operator >>(istream & in, cireada & obj) {
in>> *((vaca*)(&obj));
return in;
}
ostream & operator <<(ostream & out, cireada & obj) {
out<< "Numarul:"<<obj.size;
out<< *((vaca*)(&obj));
return out;
}
//Eliminarea unei vaci :)
cireada& cireada::elimin(int id) {
cireada *c1 = new cireada(*this);
int i;
this->size = 0;
for (i = 0;i<c1->size;i++) {
delete this->ptr[i];
if (c1->ptr[i]->getId() != id)
this->ptr[this->size++] = new vaca(*(c1->ptr[i]));
}

delete c1;
return *this;

// Adaugarea unei noi vaci in cireada :)


cireada& cireada::add(vaca & c2) {
cireada *c1 = new cireada(*this);
delete this;
ptr = new vaca*[size++];
for (int i = 0;i<size - 1;i++)
this->ptr[i] = new vaca(*(c1->ptr[i]));
delete c1;
this->ptr[size - 1] = new vaca(c2);
return *this;
}
float cireada::volMed() {
float n = 0;
for (int i = 0;i<size;++i)
n += ptr[i]->getVol();
return n / size;
}
int main() {

int i;
int nr, ad;
cireada c(2);
cout<< "\nClasa cireada:"<<endl;
cout<< "Introducerea datelro:"<<endl;
for (i = 0;i<c.getSize();++i) {
cin>>*(c.ptr[i]);
cout<<endl;
}
system("cls");
cout<<"=================================="<<endl;
cout<<"Volumul mediu de lapte:"<<c.volMed()<<endl;;
cout<<"==================================\n";
cout<< "Afisarea:"<<endl;
for (i = 0;i<c.getSize();++i) {
cout<<*(c.ptr[i]);
cout<<endl;
}
cout<<"Dati ID vacii care doriti sa o excludeti:"<<endl;
cin>>nr;
c.elimin(nr);
for (i = 0;i<c.getSize();++i) {
cout<<*(c.ptr[i]);
cout<<endl;
}
return 0;

Concluzia:
Efectund aceast lucrare de laborator am luat cunotin de posibilitile pecare le ofer programatorului
mecanismele de motenire i de compoziie. Aceste mecanisme nu numai c permit reutilizarea codului , dar
l fac mai simplificat i permit i nelegerea mai bun a programului.