Sunteți pe pagina 1din 17

Ministerul Educaiei al Republicii Moldova

Universitatea Tehnic a Moldovei

Catedra: Automatica i Tehnologii Informaionale

RAPORT
Lucrare de laborator Nr.3
la Programarea in Limbajul C++

A efectuat:
A verificat:

LUCRARE DE LABORATOR NR.

Tema: Suprancrcarea operatorilor


Scopul lucrrii:
Studierea necesitii suprancrcrii operatorilor;
Studierea sintaxei de definire a operatorilor;
Studierea tipurilor de operatori;
Studierea formelor de suprancrcare;
Noiuni de baz
Avantajul utilizrii operatorilor (reducerea codului) n acelai timp complic nelegerea
codului, deoarece nu este posibil ntotdeauna sau este complicat s se urmreasc dac se
utilizeaz un operator predefinit sau suprancrcat. ns suprancrcarea operatorilor este
necesar. Sunt probleme care se soluioneaz numai prin suprancrcarea operatorilor,cum ar fi
operatorul de atribuire. Desigur

Varianta 4
) S se creeze clasa Date data, care conine cmpurile: ziua, luna, anul. S se defineasc
operatorii "+" i "-" ca metode ale clasei, iar operatorii "++" i "--" n ambele variante(prefix i
postfix) ca funcii prietene. Operatorul "+" trebuie s permit realizarea operaiilor numai cu
variabilele de tip predefinit int (x=y+5). De prevzut prelucrarea corect a anilor biseci.
Textul Programului:
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <iomanip>
using namespace std;
class Date
{
int day,year,month;
public:
Date();
Date(int,int,int);
Date(const Date&);
~Date();
Date& operator + (int);
Date& operator - (int);
friend Date& operator ++ (Date&); //Prefix ++

friend Date operator ++ (Date&, int); //Postfix ++


friend Date& operator -- (Date&);
friend Date operator -- (Date&, int);
friend istream& operator >> (istream&, Date&);
friend ostream& operator << (ostream&, Date&);
};
Date::Date():day(0),month(0),year(0){};
Date::Date(int day, int month, int year)
{
this->day=day;
this->month=month;
this->year=year;
}
Date::Date(const Date &obj)
{
day=obj.day;
month=obj.month;
year=obj.year;
}
Date::~Date()
{
day=0;
month=0;
year=0;
}
Date& Date::operator + (int add)
{
this->day=day+add;
this->year=year+add;
this->month=month;
if(this->month == 1)
{
this->month=month+add;
if(this->day > 31) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }

}
this->month=month;
if(this->month == 2)
{
this->month=month+add;
if(this->day == 29) { this->day=day-add; this->day=day+add; }
else if(this->day > 28) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }
}
this->month=month;
if(this->month == 3)
{
this->month=month+add;
if(this->day > 31) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }
}
this->month=month;
if(this->month == 4)
{
this->month=month+add;
if(this->day > 30) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }
}
this->month=month;
if(this->month == 5)
{
this->month=month+add;
if(this->day > 31) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }
}
this->month=month;
if(this->month == 6)
{
this->month=month+add;
if(this->day > 30) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }

}
this->month=month;
if(this->month == 7)
{
this->month=month+add;
if(this->day > 31) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }
}
this->month=month;
if(this->month == 8)
{
this->month=month+add;
if(this->day > 31) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }
}
this->month=month;
if(this->month == 9)
{
this->month=month+add;
if(this->day > 30) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }
}
this->month=month;
if(this->month == 10)
{
this->month=month+add;
if(this->day > 31) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }
}
this->month=month;
if(this->month == 11)
{
this->month=month+add;
if(this->day > 30) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }

}
this->month=month;
if(this->month == 31)
{
this->month=month+add;
if(this->day > 31) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }
}
return *this;
}
Date& Date::operator - (int add)
{
this->day=day-add;
this->year=year-add;
this->month=month-add;
if(this->day < 0) { this->day=day+add; }
if(this->month < 0) { this->month=month+add; }
if(this->year < 0) { this->year=year+year; }
return *this;
}
//Prefix++
Date& operator ++ (Date& obj)
{
++obj.day;
++obj.month;
++obj.year;
return obj;
}
//Postfix ++
Date operator ++ (Date& obj, int in)
{ Date copy_(obj);
obj.day++;
obj.month++;
obj.year++;
return copy_;
}

//Prefix -Date& operator -- (Date& obj)


{
--obj.day;
--obj.month;
--obj.year;
return obj;
}
//Postfix -Date operator -- (Date& obj,int var)
{ Date copy_(obj);
obj.day--;
obj.month--;
obj.year--;
return copy_;
}
istream& operator >> (istream& in,Date& obj)
{
cout<<"Data: "; in >> obj.day;
if(!(obj.day > 0 && obj.day < 31)) { cout<<"Eroare!"; return in; }
cout<<"Luna:"; in >> obj.month;
if(!(obj.month > 0 && obj.month < 12)) { cout<<"Eroare!"; return in; }
cout<<"An:"; in >> obj.year;
if(!(obj.year > 0)) { cout<<"Eroare!"; return in; }
return in;
}
ostream& operator << (ostream& out, Date& obj)
{
out<<obj.day<<"."<<obj.month<<"."<<obj.year;
}
int main()
{
int k=1;
Date one, two, res1, res2;
while(k)
{
system("cls");
cout<<"[1]Introduceti data ";

cout<<"\n[2]Afisarea datei ";


cout<<"\n[3]Modificati data prin adunare";
cout<<"\n[4]Modificati data prin scadere";
cout<<"\n[5]Data++";
cout<<"\n[6]Data--";
cout<<"\n[0]Iesire";
cout<<"\n\nOptiunea:";
int op;
cin>>op;
switch(op)
{
default: k=1;
case 0: system("cls");
exit(0);
case 1: system("cls");
cout<<"Introduceti:\n";
cin>>one;
cout<<"\nData:"<<one;
getch();
break;
case 2: system("cls");
cout<<"Data:"<<one;
getch();
break;
case 3: system("cls");
cout<<"Data:"<<one;
res1=one+5;
cout<<"\nDupa adunare:"<<res1;
getch();
break;
case 4: system("cls");
cout<<"Data:"<<one;
res1=one-5;
cout<<"\nDupa scadere:"<<res1;
getch();
break;
case 5: system("cls");
cout<<one;

/*cout<<"\n\nPrefix increment >> "<<one++;*/


cout<<"\nIncrement:"<<++one;
getch();
break;
case 6: system("cls");
cout<<one;
cout<<"\nDecrement:"<<--one;
/*cout<<"\n\nPostfix decrement >> "<<one--;*/
getch();
break;
}
}
return 0;
}

b) S se creeze clasa List coad. S se defineasc operatorii "+" de adunare a listelor, "=" de
atribuire ca metode ale clasei. S se defineasc operatorii de comparare "==", "!=", "<", ">" ca
funcii prietene. S se suprancarce operatorii "<<" i ">>" pentru ieiri/intrri de obiecte,
precum i pentru inserarea/eliminarea elementelor n/din coad. Clasa trebuie s fie absolut
funcional, adic s conin toi constructorii necesari i destructorul. Pentru simplificarea
lucrului s se utilizeze clasa sau structura ListItem pentru reprezentarea elementelor listei spre
care refer List.
Textul Programului:
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
class Queue
{
private:
struct element
{
int var;
element *next;
};
element *head;
element *tail;
int nr;
public:
Queue()
{
head=tail=NULL;
nr=0;
};
~Queue();
void add_el(int);
void del_el();
int if_head() { if(if_empty()) cout<<"Eroare"; return head->var; }
int if_tail() { if(if_empty()) cout<<"Eroare"; return tail->var; }
int nr_el() const {return nr;};
bool if_empty() { return head==NULL;}
Queue& operator + (int);
Queue& operator = (Queue&);

friend int operator==(const Queue&,const Queue&);


friend int operator!=(const Queue&,const Queue&);
friend int operator<(const Queue&,const Queue&);
friend int operator>(const Queue&,const Queue&);
friend ostream& operator<<(ostream&, Queue&);
friend istream& operator>>(istream&, Queue&);

};
Queue::~Queue()
{
while(head != NULL)
{
element *el=head;
head=head->next;
delete el;
}
}
void Queue::add_el(int obj)
{
if(if_empty())
{
head=new element;
head->var=obj;
head->next=NULL;
tail=head;
nr=1;
}
else
{
element *el= new element;
el->var=obj;
el->next=NULL;
tail->next=el;
tail=el;
++nr;
}
}
void Queue::del_el()
{
if(if_empty()) { cout<<"No queue!!!";}

element *el= head;


head = head->next;
delete el;
--nr;
}
Queue& Queue::operator + (int n)
{
Queue temp;
while(!this->if_empty())
{
temp.add_el(this->if_head()+n);
this->del_el();
}
while(!temp.if_empty())
{
this->add_el(temp.if_head());
temp.del_el();
}
return *this;
}
Queue& Queue::operator=(Queue& obj){
Queue temp,temp2;
while(!obj.if_empty())
{
temp.add_el(obj.if_head());
temp2.add_el(obj.if_head());
obj.del_el();
}
while(!this->if_empty())
{
this->del_el();
}
while(!temp.if_empty())
{
this->add_el(temp.if_head());
temp.del_el();
}
while(!temp2.if_empty())
{
obj.add_el(temp2.if_head());
temp2.del_el();
}
return *this;

}
int operator==(Queue& obj1,Queue& obj2){
if(obj1.nr_el()==obj2.nr_el()) { cout<<"Sunt egale! "; return 1;}
else { cout<<"Nu sunt egale "; return 0;}
}
int operator!=(Queue&obj1,Queue&obj2){
if(obj1.nr_el()!=obj2.nr_el()){ cout<<"Nu sunt egale "; return 1;}
else { cout<<"Sunt egale "; return 0;}
}
int operator<(Queue&obj1,Queue&obj2){
if(obj1.nr_el()<obj2.nr_el()){ cout<<"Prima lista este mai mica "; return 1;}
else { cout<<"A 2-a lista este mai mare"; return 0;}
}
int operator>(Queue&obj1,Queue&obj2){
if(obj1.nr_el()>obj2.nr_el()){ cout<<"Prima lista este mai mare "; return 1;}
else { cout<<"A 2-a lista este mai mica"; return 0;}
}
ostream& operator<<(ostream& out, Queue& obj)
{
Queue temp;
while (!obj.if_empty())
{
out << obj.if_head() << ' ';
temp.add_el(obj.if_head());
obj.del_el();
}
while (!temp.if_empty()){
obj.add_el(temp.if_head());
temp.del_el();
}
return out;
}
istream& operator>>(istream& in, Queue& obj)
{
int ob,n;
cout<<"Introduceti nr.de elemente a listei:";
in>>n;
cout<<"Introduceti lista:\n";
for(int i = 1; i <= n; i++){
cout<<"["<<i<<"]=";

in>>ob;
obj.add_el(ob);
}
return in;
}
int main()
{
Queue el1,el2,el3;
int k, op;
while(k=1)
{
system("cls");
cout<<"[1]Introducerea datelor primei liste";
cout<<"\n[2]Introducerea datelor a 2-a liste";
cout<<"\n[3]Afisarea listei";
cout<<"\n[4]Adunarea listei";
cout<<"\n[5]Comparare < ";
cout<<"\n[6]Comparare > ";
cout<<"\n[7]Operatia ==";
cout<<"\n[8]Atribuirea";
cout<<"\n\nComanda: ";
cin>>op;
switch(op)
{
case 0: system("cls");
getch(); exit(0);
case 1: system("cls");
cin>>el1;
getch();
break;
case 2: system("cls");
cin>>el2;
getch();
break;
case 3: system("cls");
cout<<"Prima lista:";
cout<<el1;

cout<<"\nA 2-a lista:";


cout<<el2;
getch();
break;
case 4: system("cls");
cout<<"Prima lista +10:";
cout<<el1+10;
cout<<"\n\nA 2-a lista +15:";
cout<<el2+15;
getch();
break;
case 5: system("cls");
cout<<(el1<el2);
getch();
break;
case 6: system("cls");
cout<<(el1>el2);
getch();
break;
case 7: system("cls");
cout<<(el1==el2);
getch();
break;
case 8: system("cls");
cout<<(el1=el2);
getch();
break;
default: k=1;
}
}

return 0;
}

Concluzie
In lucrarea data am obtinut deprinderi practice de utilizare functiilor de supraincarcarea
operatorilor. Am aflat ce operatori trebuie de supraincarcat si ce operatori nu se
recomanda de supraincarcat .Au fost obtinute cunostinte noi in limbajul de programare
C++ si anume supraincarcarea operatorilor si necesitatea lor.