Sunteți pe pagina 1din 9

Ministerul Educaţiei, Culturii şi Cercetării al Republicii Moldova

Universitatea Tehnică a Moldovei


Facultatea Calculatoare, Informatică şi Microelectronică
Departamentul Informatică şi Ingineria Sistemelor

Lucrare de laborator Nr.3


Programarea Orientată pe Obiecte
Tema: “Supraîncărcarea operatorilor”

Efectuat de st.gr. MI-181, Tutunaru Eugenia

Verificat de asist. univ., Mititelu Vitalii

Chişinău 2019

This study source was downloaded by 100000854172019 from CourseHero.com on 10-31-2022 13:41:02 GMT -05:00

https://www.coursehero.com/file/74187139/TUTUNARU-POO-LAB-3docx/
Scopul lucrării:
 Studierea necesităţii supraîncărcării operatorilor;
 Studierea sintaxei de definire a operatorilor;
 Studierea tipurilor de operatori;
 Studierea formelor de supraîncărcare;
Varianta 1

Sarcina individuală şi listingul programelor


a) Să se creeze o clasă de numere întregi. Să se definească operatorii "++" şi "+", ca
metode ale clasei, iar operatorii "- -" şi "-" ca funcţii prietene. Operatorii trebuie să permită
efectuarea operaţiilor atît cu variabilele clasei date, cît şi cu variabilele întregi de tip predefinit.

#include <iostream>

using namespace std;

class Integer
{
public:
int nr;

Integer (int n = 0){nr = n;}


~Integer(){ }
int getNr() {return nr;}

Integer operator+(Integer b){return Integer(nr + b.nr); }


Integer operator+(int b) {return Integer(this->nr + b);}
Integer operator++(int) {return Integer((this->nr)++);}
Integer operator++() {return Integer(++(this->nr));}

friend Integer operator-(Integer b);


friend Integer operator-(Integer a, int b);
friend Integer operator-(int b, Integer a);
friend Integer operator--(Integer &a, int);
friend Integer operator--(Integer &a);
};

Integer operator+(int left, Integer right)


{
return Integer(left + right.nr);
}

Integer operator-(Integer left, Integer right)


{
return Integer(left.nr - right.nr);
}

Integer operator-(Integer left, int right)


{
return Integer(left.nr - right);
}

Integer operator-(int left, Integer right)

This study source was downloaded by 100000854172019 from CourseHero.com on 10-31-2022 13:41:02 GMT -05:00

https://www.coursehero.com/file/74187139/TUTUNARU-POO-LAB-3docx/
{
return Integer(left - right.nr);
}

Integer operator--(Integer &a, int)


{
return Integer(a.nr--);
}

Integer operator--(Integer &a)


{
return Integer(--a.nr);
}

int main()
{
Integer first(5), second(-3), result;
cout<<" **********FIRST TASK**********"<<endl<<endl;
cout<<" First number is "<<first.nr<<" and the second is
"<<second.nr<<endl<<endl;
result = first + second;
cout<<" Their sum: "<<result.nr<<endl;
result = first + 3;
cout<<" First object + an int(3) = "<<result.nr<<endl;
result = 4 + first;
cout<<" An int(4) + first object = "<<result.nr<<endl;
cout<<" First object before post-incrementation: "<<(first+
+).nr<<endl;
cout<<" First object after post-incrementation: "<<first.nr<<endl;
cout<<" First object before pre-incrementation: "<<(++first).nr<<endl;
cout<<" First object after pre-incrementation:
"<<first.nr<<endl<<endl;

result = first - second;


cout<<" Difference between objects: "<<result.nr<<endl;
result = second - 3;
cout<<" Second object - an int(3) = "<<result.nr<<endl;
result = 4 - second;
cout<<" An int(4) - second object = "<<result.nr<<endl;
cout<<" First object before post-decrementation:
"<<(first--).nr<<endl;
cout<<" First object after post-decrementation: "<<first.nr<<endl;
cout<<" First object before pre-decrementation: "<<(--first).nr<<endl;
cout<<" First object after pre-decrementation: "<<first.nr<<endl;
return 0;
}

b) Să se creeze o clasă Set – mulţimea numerelor întregi, utilizînd memoria dinamică.


Să se definească operatorii de lucru cu mulţimile: "+" – uniunea, "*" – intersecţia, "-" scăderea,
ca funcţii prietene, iar "+=" – înserarea unui nou element în mulţime, "==" – comparare la
egalitate, ş. a. ca metode ale clasei. Să se definească operatorii "<<" şi ">>". Să se definească
funcţia de verificare a apartenenţei unui element la o mulţime.

#include <iostream>
#include <algorithm>
#include <vector>

This study source was downloaded by 100000854172019 from CourseHero.com on 10-31-2022 13:41:02 GMT -05:00

https://www.coursehero.com/file/74187139/TUTUNARU-POO-LAB-3docx/
using namespace std;

class Set
{
public:
int *a;
int size;
Set() {
this->size = 0;
this->a = NULL;
}
Set(const Set &other) {
this->size = other.size;
if (this->size > 0) {
this->a = new int[this->size];
for (int i = 0; i < this->size; i++) {
this->a[i] = other.a[i];
}
}
}

int compare(const Set &right) const;


friend Set operator+(Set const &left, Set const &right);
friend Set operator*(Set &left, Set &right);
friend Set operator-(Set &left, Set &right);
Set& operator+=(int right);
Set& operator=(const Set &right) {
size = right.size;
if (a)
delete[] a;
a = new int[size];
for (int i = 0; i < size; i++)
a[i] = right.a[i];

return *this;
}
bool operator==(Set& right);
bool belong(int right) const;
~Set() { if (a) delete[] a; }

};

istream &operator >> (istream &ist, Set &ob)


{
int nr_elts;
bool first = true;
do {
if (!first)
cout << "Invalid number entered" << endl;
cout << "\tEnter number of elements: ";
ist >> nr_elts;
first = false;
} while (nr_elts < 1);

int nr;
cout << "\tIntroduce the elements: " << endl;
for (int i = 0; i < nr_elts; i++)
{
first = true;
int oldsz = ob.size;

do {

This study source was downloaded by 100000854172019 from CourseHero.com on 10-31-2022 13:41:02 GMT -05:00

https://www.coursehero.com/file/74187139/TUTUNARU-POO-LAB-3docx/
if (!first)
cout << "Duplicate element entered" << endl;
cout << " Element [" << i + 1 << "] = ";
ist >> nr;
ob += nr;
first = false;
} while (oldsz == ob.size);
}
return ist;
}

ostream &operator<<(ostream &ost, Set &ob)


{
if (ob.a)
{
for (int i = 0; i < ob.size; i++)
ost << " " << ob.a[i];
ost << endl;
}
return ost;
}

int Set::compare(const Set &right) const


{
int common_elts = 0;
for (int i = 0; i < this->size; i++) {
int val = this->a[i];

if (right.belong(val))
common_elts++;
}
return common_elts;
}

Set operator+(Set const &left, Set const &right)


{
Set tmp;

for (int i = 0; i < left.size; i++) {


tmp += left.a[i];
}
for (int i = 0; i < right.size; i++) {
tmp += right.a[i];
}
return tmp;
}

Set operator*(Set &left, Set &right)


{
Set tmp;

for (int i = 0; i < left.size; i++) {


int val = left.a[i];
if (!right.belong(val))
continue;
tmp += val;
}
return tmp;
}

Set operator-(Set &left, Set &right)

This study source was downloaded by 100000854172019 from CourseHero.com on 10-31-2022 13:41:02 GMT -05:00

https://www.coursehero.com/file/74187139/TUTUNARU-POO-LAB-3docx/
{
Set temp;

for (int i = 0; i < left.size; i++) {


int val = left.a[i];
if (right.belong(val))
continue;
temp += val;
}

return temp;
}

Set& Set::operator+=(int right)


{
if (belong(right))
return *this;
int *tmp = this->a;

this->a = new int[this->size + 1];


for (int i = 0; i < this->size; i++)
this->a[i] = tmp[i];
this->a[this->size] = right;
this->size++;
if (tmp)
delete[] tmp;
return *this;
}

bool Set::operator==(Set &right)


{
if (this->size != right.size)
return false;
for (int i = 0; i < this->size; i++) {
int v = this->a[i];
if (!right.belong(v))
return false;
}
return true;
}

bool Set::belong(int right) const


{
for (int i = 0; i < this->size; i++)
if (this->a[i] == right)
return true;
return false;
}

int main()
{
cout << "\t**********Second task**********" << endl << endl;
int size_1, size_2;
cout << "\t First matrix: " << endl;
Set A;
cin >> A;
cout << "\n\t Second matrix: " << endl;
Set B;
cin >> B;
int s_n = A.compare(B);

int s_u = A.size + B.size - s_n, s_m = A.size - s_n, temp;

This study source was downloaded by 100000854172019 from CourseHero.com on 10-31-2022 13:41:02 GMT -05:00

https://www.coursehero.com/file/74187139/TUTUNARU-POO-LAB-3docx/
Set AUB, AnB, Minus;

AUB = A + B;
cout << "\n\tReunion: ";
cout << AUB;
cout << "\n\tIntersection: ";
AnB = A * B;
cout << AnB;
if (s_m != 0)
{
Minus = A - B;
cout << "\n\tDiference: " << Minus;
}
else
cout << "\n\tEach element from first set is in the second set."
<< Minus;
cout << endl << "\tIntroduce a number: ";
cin >> temp;
A += temp;
cout << "\tFirst array after insert new element: " << A;
Set C, D;
cin >> C;
cin >> D;
cout << "\n\tCompare arrays...";
cout << (C == D ? "arrays are equal." : "arrays are not equal.") <<
endl;
cout << "\n\tFind an element in first array.\n\t Introduce an element:
";
cin >> temp;

if (C.belong(temp))
cout << "\tElement is in the array" << endl;
else
cout << "\tElement is not in the array" << endl;
return 0;
}

Rezultatele afişărilor

This study source was downloaded by 100000854172019 from CourseHero.com on 10-31-2022 13:41:02 GMT -05:00

https://www.coursehero.com/file/74187139/TUTUNARU-POO-LAB-3docx/
a)

b)

Concluzii

This study source was downloaded by 100000854172019 from CourseHero.com on 10-31-2022 13:41:02 GMT -05:00

https://www.coursehero.com/file/74187139/TUTUNARU-POO-LAB-3docx/
Supraîncărcarea operatorilor a apărut din necesitatea de a reduce volumul de cod şi de a
siplifica lucrul cu obiectele clasei şi nu doar, prin utilizarea operatorilor deja existenţi. Astfel
specificând ca membrul clasei, unul şi acelaşi operator poate concomitent exista în mai multe
stări, adică în dependenţă de parametrii transmişi şi tipul de date returnat poate fi utilizat în
diferite scopuri.
Pentru a declara supraîncărcarea operatorului e nevoie să specificăm tipul datelor
returnat, cuvântul cheie „operator” şi simbolul care va fi utilizat (ales din simbolurile care
desemnează operatori în C++). În rest funcţiile de supraîncărcare a operatorilor nu diferenţiază
substanţial de oricare altă funcţie normală. Cele dintâi sunt apelate când opeartorul în cauză este
utilizat. Pentru ca operatorul supraîncărcat să lucreze e necesar ca cel puţin un operand să fie
obiect al clasei definite.

This study source was downloaded by 100000854172019 from CourseHero.com on 10-31-2022 13:41:02 GMT -05:00

https://www.coursehero.com/file/74187139/TUTUNARU-POO-LAB-3docx/
Powered by TCPDF (www.tcpdf.org)

S-ar putea să vă placă și