Documente Academic
Documente Profesional
Documente Cultură
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
#include <iostream>
class Integer
{
public:
int nr;
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);
}
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;
#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];
}
}
}
return *this;
}
bool operator==(Set& right);
bool belong(int right) const;
~Set() { if (a) delete[] a; }
};
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;
}
if (right.belong(val))
common_elts++;
}
return common_elts;
}
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;
return temp;
}
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);
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)