2
-
Upload
vlad-oftici -
Category
Documents
-
view
3 -
download
1
description
Transcript of 2
MINISTERUL EDUCAȚIEI al REPUBLICII MOLDOVA
UNIVERSITATEA TEHNICĂ a MOLDOVEI
FACULTATEA CALCULATOARE, INFORMATICĂ
și MICROELECTRONICĂ
CATEDRA AUTOMATICĂ și TEHNOLOGII INFORMAȚIONALE
RAPORTDisciplina: APPOO
Lucrare de laborator Nr. 2
Tema: Supraîncărcarea
A elaborat: Oftici V. st.gr. SI-121
A verificat: lest.sup. Balan M.
Chișinău 20151
Scopul lucrării: Studierea principiilor folosirii supraîncărcării; Supraîncărcarea funcțiilor și operatorilor; Studierea tipurilor de operatori; Definirea operatorilor de diferite tipuri.
Sarcina:Sarcina1: Crearea clasei numerelor intregi. Definirea operatorului --, ca funcție membru și operatorul ++, ca funcție prieten. Definirea operatorului -, ca funcție membru și ca funcție prieten.Sarcina 2: Crearea clasei Coord De definit operatorul +, ca functie membru si – ca functie prieten. Inmultit si de impartit coordonatele unul cu altul si cu numere intregi. Atribut coordinate, comparat coordonatele (==, !=). Determinat coodonatele =, +=, -=, *= cu obiectele acestei clasei si cu numere intregi. Supraincarcat operatorii >>, <<.
Codul sursă 1:
#include <iostream>#include <math.h>#include<conio.h>#define cls system("CLS");using namespace std;
class Intreg{
int x;public:
Intreg(int x = 0) :x(x){}Intreg& operator--(int);friend Intreg& operator++(Intreg&, int);Intreg& operator-(Intreg&);friend Intreg& operator-(Intreg&, int);friend ostream& operator<<(ostream&, Intreg&);friend istream& operator>>(istream&, Intreg&);
};
Intreg& Intreg::operator-(Intreg& b){return *(new Intreg(x - b.x));//returnam un obiect cu valorea diferentei
}
Intreg& operator-(Intreg& a, int b){return *(new Intreg(a.x - b));//returnam un obiect cu valorea diferentei dintre un obiect si numar
//intreg}
Intreg& Intreg::operator--(int){//post decrementareIntreg* tmp = new Intreg(x);
2
x--;return *tmp;
}
Intreg& operator++(Intreg& a, int){//post incrementareIntreg *tmp = new Intreg(a.x);a.x++;return *tmp;
}
ostream& operator<<(ostream& out, Intreg& a){return out << a.x;//afisam valorea obiectului
}
istream& operator>>(istream& in, Intreg& a){return in >> a.x;//citim valorea obiectului
}
int main(){Intreg a, b;int c;cout << "Introduceti numarul a si b:" << endl;cin >> a;cin >> b;cout << "a=" << a-- << endl;cout << "b=" << b++ << endl;cout << "a--= " << a << endl;cout << "b++= " << b << endl;cout << "a-b= " << a - b << endl;cout << "a-5= " << a - 5 << endl;system("pause");return 0;
}
Codul sursă 2: #include<iostream>#include<cmath>#define cls system("CLS");
using namespace std;
class Coord {int x;int y;
public:Coord();Coord(int, int);
bool operator==(Coord);3
bool operator!=(Coord);
Coord& operator=(Coord);Coord& operator+=(Coord);Coord& operator-=(Coord);Coord& operator*=(Coord);Coord& operator=(int);Coord& operator+=(int);Coord& operator-=(int);Coord& operator*=(int);
Coord& operator+(Coord);Coord& operator+(int);friend Coord& operator-(Coord, Coord);friend Coord& operator-(Coord, int);
friend ostream& operator<<(ostream&, Coord&);friend istream& operator>>(istream&, Coord&);
};
Coord::Coord() :x(0), y(0){}//creem constructorul cu valorile initialeCoord::Coord(int x, int y) : x(x), y(y){}
Coord& Coord::operator + (Coord b){ return *(new Coord(x + b.x, y + b.y)); }//returnam obiectul cu valorea sumei
Coord& Coord::operator + (int b){ return *(new Coord(x + b, y + b)); }// returnam obiectul cu valorea sumei cu in intreg
Coord& operator - (Coord a, Coord b){ return *(new Coord(a.x - b.x, a.y - b.y)); }//returnam obiectul cu valorea diferentei
Coord& operator - (Coord a, int b){ return *(new Coord(a.x - b, a.y - b)); }//returnam obiectul cu valorea diferentei cu un int
Coord& Coord::operator=(Coord a){ x = a.x; y = a.y; return *this; }Coord& Coord::operator=(int a){ x = a; y = a; return *this; }Coord& Coord::operator+=(Coord a){ x += a.x; y += a.y; return *this; }Coord& Coord::operator+=(int a){ x += a; y += a; return *this; }Coord& Coord::operator-=(Coord a){ x -= a.x; y -= a.y; return *this; }Coord& Coord::operator-=(int a){ x -= a; y -= a; return *this; }Coord& Coord::operator*=(Coord a){ x *= a.x; y *= a.y; return *this; }//returnam obiectul cu valorea inmultireiCoord& Coord::operator*=(int a){ x *= a; y *= a; return *this; }// returnam obiectul cu valorea inmultirei cu un int
bool Coord::operator==(Coord a){ return (x == a.x && y == a.y) ? true : false; }//comparam obiectelebool Coord::operator!=(Coord a){ return !(*this == a); }//comparam obiectele
ostream& operator<<(ostream& out, Coord& a){ return out << "("<<a.x<<","<<a.y<<")";}//afisam valorile obiecteloristream& operator>>(istream& in, Coord& a){ return in >> a.x>>a.y;}//citim valorile obiectelor
4
int main() {Coord A, B; int x, y, C;cout << "Introduceti coordonata A:\n";cin >> x; cin >> y; A = *(new Coord(x, y));cout << "Introduceti coordonata B:\n";cin >> x; cin >> y; B = *(new Coord(x, y));cout << "Introduceti un numar C:\n";cin >> C;clscout << "\nA+B= " << A << " + " << B << " = " << (A + B);cout << "\nA-B= " << A << " - " << B << " = " << (A - B);cout << "\nA+C= " << A << " + " << C << " = " << (A + C);cout << "\nB+C= " << B << " + " << C << " = " << (B + C);cout << "\nA-C= " << A << " - " << C << " = " << (A - C);cout << "\nB-C= " << B << " - " << C << " = " << (B - C);cout << "\nA+=B= " << A << " += " << B << " = ";cout << (A += B);cout << "\nA-=B= " << A << " -= " << B << " = ";cout << (A -= B);cout << "\nA*=B= " << A << " *= " << B << " = ";cout << (A *= B);cout << "\nB+=C= " << B << " += " << C << " = ";cout << (B += C);cout << "\nB-=C= " << B << " -= " << C << " = ";cout << (B -= C);cout << "\nB*=C= " << B << " *= " << C << " = ";cout << (B *= C);cout << "\nA==B - " << (A == B);cout << "\nA!=B - " << (A != B);cout << endl;system("pause");
}
Concluzie:Efectuind lucrarea data, am facut cunostinta cu metodele de supraincarcare a operatorilor. Cunostintele obtinute ne vor ajuta la crearea mai usoara unor programe si aplicatii, deasemeni, la scrierea unui cod program mai lizibil si usor de inteles.
5