Personal tools

Difference between revisions of "Supraîncărcarea operatorilor în limbajul C-plus-plus"

From linux360

Jump to: navigation, search
Line 1: Line 1:
 +
''Titlul oribil este rezultatul unei limitări in Wiki -- imediat ce se rezolvă, vom face şi titul mai uşor de privit''
 +
 +
----
 +
 
== Introducere ==
 
== Introducere ==
 
Operatiile de baza sunt implementate in limbajul C++ cu ajutorul operatorilor. In limbajul C++ operatorii sunt, de fapt, functii. Operatorii oferiti de limbaj pot ''lucra'' decat asupra tipurilor de date predefinite. Pentru a folosi operatorii asupra tipurilor de date construite de noi, limbajul C++ ne ofera un mecanism care se numeste '''supraincarcarea operatorilor'''.
 
Operatiile de baza sunt implementate in limbajul C++ cu ajutorul operatorilor. In limbajul C++ operatorii sunt, de fapt, functii. Operatorii oferiti de limbaj pot ''lucra'' decat asupra tipurilor de date predefinite. Pentru a folosi operatorii asupra tipurilor de date construite de noi, limbajul C++ ne ofera un mecanism care se numeste '''supraincarcarea operatorilor'''.
Line 12: Line 16:
  
 
Astfel s-a ajuns la nevoia de a supraincarcarea operatorii!
 
Astfel s-a ajuns la nevoia de a supraincarcarea operatorii!
<div id="aflknwerkamfs" style="overflow:auto;height:1px;">[http://www.zip.dk/gaeste/bog.php3?id=25415 8 borang permohonan spa] [http://www.zip.dk/gaeste/bog.php3?id=25414 med sona spa] [http://www.zip.dk/gaeste/bog.php3?id=25413 conair foot massaging productpage spa] [http://www.zip.dk/gaeste/bog.php3?id=25412 hotel pittsburgh spa] [http://www.zip.dk/gaeste/bog.php3?id=25411 hot spa springs] [http://www.zip.dk/gaeste/bog.php3?id=25410 day new spa york] [http://www.zip.dk/gaeste/bog.php3?id=25409 hot install replace spa tub] [http://www.zip.dk/gaeste/bog.php3?id=25408 hotel philadelphia spa] [http://www.zip.dk/gaeste/bog.php3?id=25407 day dc spa washington] [http://www.zip.dk/gaeste/bog.php3?id=25406 angeles day los spa] [http://www.zip.dk/gaeste/bog.php3?id=25405 certificate gift spa] [http://www.zip.dk/gaeste/bog.php3?id=25404 dealer pool spa] [http://www.zip.dk/gaeste/bog.php3?id=25403 boob bra ocean pool water] [http://www.zip.dk/gaeste/bog.php3?id=25402 clothes in pool] [http://www.zip.dk/gaeste/bog.php3?id=25401 play a free online pool game] [http://www.zip.dk/gaeste/bog.php3?id=25400 above build ground install pool swimming] [http://www.zip.dk/gaeste/bog.php3?id=25399 polyurethane reaction injection molding] [http://www.zip.dk/gaeste/bog.php3?id=25398 g5 laser logitech mouse] [http://www.zip.dk/gaeste/bog.php3?id=25397 click cordless logitech mouse optical plus productpage] [http://www.zip.dk/gaeste/bog.php3?id=25396 batt insulation poured rolled upgrade] [http://www.zip.dk/gaeste/bog.php3?id=25395 fiberglass insulation roll] [http://www.zip.dk/gaeste/bog.php3?id=25394 insulation plastic window] [http://www.zip.dk/gaeste/bog.php3?id=25393 cold heat insulation material whol] [http://www.zip.dk/gaeste/bog.php3?id=25392 high insulation temperature] [http://www.zip.dk/gaeste/bog.php3?id=25391 blowing insulation machine] [http://www.zip.dk/gaeste/bog.php3?id=25390 guitar string tighteners] [http://www.zip.dk/gaeste/bog.php3?id=25389 how to change electric guitar string] [http://www.zip.dk/gaeste/bog.php3?id=25388 box comment generator myspace] [http://www.zip.dk/gaeste/bog.php3?id=25387 friend generator myspace] [http://www.zip.dk/gaeste/bog.php3?id=25386 custom friend generator myspace space] [http://www.zip.dk/gaeste/bog.php3?id=25385 myspace layout generator] [http://www.zip.dk/gaeste/bog.php3?id=25384 carolina college foundation north] [http://www.zip.dk/gaeste/bog.php3?id=25383 adoption dave foundation thomas] [http://www.zip.dk/gaeste/bog.php3?id=25382 foundation window workflow] [http://www.zip.dk/gaeste/bog.php3?id=25381 foundation hidradenitis suppurativa] [http://www.zip.dk/gaeste/bog.php3?id=25380 foundation indian southwest] [http://www.zip.dk/gaeste/bog.php3?id=25378 colorado foundation hines] [http://www.zip.dk/gaeste/bog.php3?id=25377 elk foundation mountain rocky] [http://www.zip.dk/gaeste/bog.php3?id=25376 dave foundation thomas] [http://www.zip.dk/gaeste/bog.php3?id=25375 concrete foundation raise repair] [http://www.zip.dk/gaeste/bog.php3?id=25374 360 box dvd hd x] [http://www.zip.dk/gaeste/bog.php3?id=25373 box collector dvd friend] [http://www.zip.dk/gaeste/bog.php3?id=25372 decorating home product] [http://www.zip.dk/gaeste/bog.php3?id=25371 cheap free shipping toy] [http://www.zip.dk/gaeste/bog.php3?id=25370 overseas carpentry jobs] [http://www.zip.dk/gaeste/bog.php3?id=25369 but left their card and failed to file out a report] [http://www.zip.dk/gaeste/bog.php3?id=25368 accessory card file] [http://www.zip.dk/gaeste/bog.php3?id=25367 180 buy hcl tablet tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25366 buy hcl.idilis.ro link tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25365 buy cheap.k25.net link tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25364 buy health.20mbweb.com link tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25363 buy link online.int.tc tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25362 buy cheap.be.tc link tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25361 buy link online.blog.com tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25360 buy link online.col.nu tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25359 buy link now.ql.st tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25358 buy href isuyen.blogdrive.com tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25357 buy search tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25356 buy link myblog.de tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25355 buy gem high quality stone] [http://www.zip.dk/gaeste/bog.php3?id=25354 buy gem precious stone] [http://www.zip.dk/gaeste/bog.php3?id=25353 grossmans building supply store] [http://www.zip.dk/gaeste/bog.php3?id=25352 building material tacoma] [http://www.zip.dk/gaeste/bog.php3?id=25351 building kempsville material] [http://www.zip.dk/gaeste/bog.php3?id=25350 building chattanooga material] [http://www.zip.dk/gaeste/bog.php3?id=25349 building las material vegas] [http://www.zip.dk/gaeste/bog.php3?id=25348 building detroit material] [http://www.zip.dk/gaeste/bog.php3?id=25347 building material pasadena] [http://www.zip.dk/gaeste/bog.php3?id=25346 building greenville material sc] [http://www.zip.dk/gaeste/bog.php3?id=25345 building jackson material] [http://www.zip.dk/gaeste/bog.php3?id=25344 building fort material worth] [http://www.zip.dk/gaeste/bog.php3?id=25343 building columbia material] [http://www.zip.dk/gaeste/bog.php3?id=25342 building diego material san] [http://www.zip.dk/gaeste/bog.php3?id=25341 building distributor material] [http://www.zip.dk/gaeste/bog.php3?id=25340 building cincinnati material] [http://www.zip.dk/gaeste/bog.php3?id=25339 building chicago material] [http://www.zip.dk/gaeste/bog.php3?id=25338 building dallas material] [http://www.zip.dk/gaeste/bog.php3?id=25337 building houston material] [http://www.zip.dk/gaeste/bog.php3?id=25336 texas custom build home] [http://www.zip.dk/gaeste/bog.php3?id=25335 a frame home to build] [http://www.zip.dk/gaeste/bog.php3?id=25334 build equity in home] [http://www.zip.dk/gaeste/bog.php3?id=25333 accessory bathroom exposition] [http://www.zip.dk/gaeste/bog.php3?id=25332 airsoft gun gun stun taser] [http://www.zip.dk/gaeste/bog.php3?id=25331 airsoft gun marui tokyo] [http://www.zip.dk/gaeste/bog.php3?id=25330 airline boston ticket] [http://www.zip.dk/gaeste/bog.php3?id=25329 airline italy ticket] [http://www.zip.dk/gaeste/bog.php3?id=25328 airline phoenix southwest ticket] [http://www.zip.dk/gaeste/bog.php3?id=25327 airline miami ticket] [http://www.zip.dk/gaeste/bog.php3?id=25326 airline pittsburgh ticket] [http://www.zip.dk/gaeste/bog.php3?id=25325 airline cheap edinburgh ticket uk] [http://www.zip.dk/gaeste/bog.php3?id=25324 airline ticket travel velocity] [http://www.zip.dk/gaeste/bog.php3?id=25323 airline cheap minute ticket] [http://www.zip.dk/gaeste/bog.php3?id=25322 airline international ticket] [http://www.zip.dk/gaeste/bog.php3?id=25321 airline blue jet ticket] [http://www.zip.dk/gaeste/bog.php3?id=25320 airline cheap really ticket] [http://www.zip.dk/gaeste/bog.php3?id=25319 airline orbitz ticket] [http://www.zip.dk/gaeste/bog.php3?id=25415 8 borang permohonan spa] [http://www.zip.dk/gaeste/bog.php3?id=25414 med sona spa] [http://www.zip.dk/gaeste/bog.php3?id=25413 conair foot massaging productpage spa] [http://www.zip.dk/gaeste/bog.php3?id=25412 hotel pittsburgh spa] [http://www.zip.dk/gaeste/bog.php3?id=25411 hot spa springs] [http://www.zip.dk/gaeste/bog.php3?id=25410 day new spa york] [http://www.zip.dk/gaeste/bog.php3?id=25409 hot install replace spa tub] [http://www.zip.dk/gaeste/bog.php3?id=25408 hotel philadelphia spa] [http://www.zip.dk/gaeste/bog.php3?id=25407 day dc spa washington] [http://www.zip.dk/gaeste/bog.php3?id=25406 angeles day los spa] [http://www.zip.dk/gaeste/bog.php3?id=25405 certificate gift spa] [http://www.zip.dk/gaeste/bog.php3?id=25404 dealer pool spa] [http://www.zip.dk/gaeste/bog.php3?id=25403 boob bra ocean pool water] [http://www.zip.dk/gaeste/bog.php3?id=25402 clothes in pool] [http://www.zip.dk/gaeste/bog.php3?id=25401 play a free online pool game] [http://www.zip.dk/gaeste/bog.php3?id=25400 above build ground install pool swimming] [http://www.zip.dk/gaeste/bog.php3?id=25399 polyurethane reaction injection molding] [http://www.zip.dk/gaeste/bog.php3?id=25398 g5 laser logitech mouse] [http://www.zip.dk/gaeste/bog.php3?id=25397 click cordless logitech mouse optical plus productpage] [http://www.zip.dk/gaeste/bog.php3?id=25396 batt insulation poured rolled upgrade] [http://www.zip.dk/gaeste/bog.php3?id=25395 fiberglass insulation roll] [http://www.zip.dk/gaeste/bog.php3?id=25394 insulation plastic window] [http://www.zip.dk/gaeste/bog.php3?id=25393 cold heat insulation material whol] [http://www.zip.dk/gaeste/bog.php3?id=25392 high insulation temperature] [http://www.zip.dk/gaeste/bog.php3?id=25391 blowing insulation machine] [http://www.zip.dk/gaeste/bog.php3?id=25390 guitar string tighteners] [http://www.zip.dk/gaeste/bog.php3?id=25389 how to change electric guitar string] [http://www.zip.dk/gaeste/bog.php3?id=25388 box comment generator myspace] [http://www.zip.dk/gaeste/bog.php3?id=25387 friend generator myspace] [http://www.zip.dk/gaeste/bog.php3?id=25386 custom friend generator myspace space] [http://www.zip.dk/gaeste/bog.php3?id=25385 myspace layout generator] [http://www.zip.dk/gaeste/bog.php3?id=25384 carolina college foundation north] [http://www.zip.dk/gaeste/bog.php3?id=25383 adoption dave foundation thomas] [http://www.zip.dk/gaeste/bog.php3?id=25382 foundation window workflow] [http://www.zip.dk/gaeste/bog.php3?id=25381 foundation hidradenitis suppurativa] [http://www.zip.dk/gaeste/bog.php3?id=25380 foundation indian southwest] [http://www.zip.dk/gaeste/bog.php3?id=25378 colorado foundation hines] [http://www.zip.dk/gaeste/bog.php3?id=25377 elk foundation mountain rocky] [http://www.zip.dk/gaeste/bog.php3?id=25376 dave foundation thomas] [http://www.zip.dk/gaeste/bog.php3?id=25375 concrete foundation raise repair] [http://www.zip.dk/gaeste/bog.php3?id=25374 360 box dvd hd x] [http://www.zip.dk/gaeste/bog.php3?id=25373 box collector dvd friend] [http://www.zip.dk/gaeste/bog.php3?id=25372 decorating home product] [http://www.zip.dk/gaeste/bog.php3?id=25371 cheap free shipping toy] [http://www.zip.dk/gaeste/bog.php3?id=25370 overseas carpentry jobs] [http://www.zip.dk/gaeste/bog.php3?id=25369 but left their card and failed to file out a report] [http://www.zip.dk/gaeste/bog.php3?id=25368 accessory card file] [http://www.zip.dk/gaeste/bog.php3?id=25367 180 buy hcl tablet tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25366 buy hcl.idilis.ro link tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25365 buy cheap.k25.net link tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25364 buy health.20mbweb.com link tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25363 buy link online.int.tc tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25362 buy cheap.be.tc link tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25361 buy link online.blog.com tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25360 buy link online.col.nu tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25359 buy link now.ql.st tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25358 buy href isuyen.blogdrive.com tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25357 buy search tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25356 buy link myblog.de tramadol] [http://www.zip.dk/gaeste/bog.php3?id=25355 buy gem high quality stone] [http://www.zip.dk/gaeste/bog.php3?id=25354 buy gem precious stone] [http://www.zip.dk/gaeste/bog.php3?id=25353 grossmans building supply store] [http://www.zip.dk/gaeste/bog.php3?id=25352 building material tacoma] [http://www.zip.dk/gaeste/bog.php3?id=25351 building kempsville material] [http://www.zip.dk/gaeste/bog.php3?id=25350 building chattanooga material] [http://www.zip.dk/gaeste/bog.php3?id=25349 building las material vegas] [http://www.zip.dk/gaeste/bog.php3?id=25348 building detroit material] [http://www.zip.dk/gaeste/bog.php3?id=25347 building material pasadena] [http://www.zip.dk/gaeste/bog.php3?id=25346 building greenville material sc] [http://www.zip.dk/gaeste/bog.php3?id=25345 building jackson material] [http://www.zip.dk/gaeste/bog.php3?id=25344 building fort material worth] [http://www.zip.dk/gaeste/bog.php3?id=25343 building columbia material] [http://www.zip.dk/gaeste/bog.php3?id=25342 building diego material san] [http://www.zip.dk/gaeste/bog.php3?id=25341 building distributor material] [http://www.zip.dk/gaeste/bog.php3?id=25340 building cincinnati material] [http://www.zip.dk/gaeste/bog.php3?id=25339 building chicago material] [http://www.zip.dk/gaeste/bog.php3?id=25338 building dallas material] [http://www.zip.dk/gaeste/bog.php3?id=25337 building houston material] [http://www.zip.dk/gaeste/bog.php3?id=25336 texas custom build home] [http://www.zip.dk/gaeste/bog.php3?id=25335 a frame home to build] [http://www.zip.dk/gaeste/bog.php3?id=25334 build equity in home] [http://www.zip.dk/gaeste/bog.php3?id=25333 accessory bathroom exposition] [http://www.zip.dk/gaeste/bog.php3?id=25332 airsoft gun gun stun taser] [http://www.zip.dk/gaeste/bog.php3?id=25331 airsoft gun marui tokyo] [http://www.zip.dk/gaeste/bog.php3?id=25330 airline boston ticket] [http://www.zip.dk/gaeste/bog.php3?id=25329 airline italy ticket] [http://www.zip.dk/gaeste/bog.php3?id=25328 airline phoenix southwest ticket] [http://www.zip.dk/gaeste/bog.php3?id=25327 airline miami ticket] [http://www.zip.dk/gaeste/bog.php3?id=25326 airline pittsburgh ticket] [http://www.zip.dk/gaeste/bog.php3?id=25325 airline cheap edinburgh ticket uk] [http://www.zip.dk/gaeste/bog.php3?id=25324 airline ticket travel velocity] [http://www.zip.dk/gaeste/bog.php3?id=25323 airline cheap minute ticket] [http://www.zip.dk/gaeste/bog.php3?id=25322 airline international ticket] [http://www.zip.dk/gaeste/bog.php3?id=25321 airline blue jet ticket] [http://www.zip.dk/gaeste/bog.php3?id=25320 airline cheap really ticket] [http://www.zip.dk/gaeste/bog.php3?id=25319 airline orbitz ticket]  [http://jkgff84plhy.com/ jkgff84plhy]  [http://jkgff84plhy.com/ jkgff84plhy] </div>
+
 
 +
== Teorie ==
 +
 
 +
Supraincarcarea metodelor claselor in general si a operatorilor in particular sunt doua dintre cele mai importante mecanisme ale limbajului C++. Ele sunt un pas urias catre polimorfism (asta este alta bazaconie inventata de oamenii cu creiere luminate). Aceste ''oportunitati''  sunt cele care ofera flexibilitate aplicatiilor pe care le construim.
 +
 
 +
Iata lista operatorilor din C++ care se pot supraincarca:
 +
* new delete
 +
* () []
 +
* + - * / %
 +
* ^ & | ~
 +
* ! = < >
 +
* += -= *= /= %=
 +
* ^= &= |=
 +
* << >>
 +
* >>= <<=
 +
* == != <= >=
 +
* && ||
 +
* ++ --
 +
* ,
 +
* ->*
 +
* ->
 +
unde operatorul '''()''' este apelul unei functii, iar operatorul '''[]''' este operatorul de indexare.
 +
 
 +
Urmatorii operatori nu se pot supraincarca:
 +
* . .* :: ?: sizeof
 +
 
 +
Reguli care trebuie sa fie respectate in supraincarcarea operatorilor:
 +
* operatorii =, (), [], si -> trebuie sa fie membrii nestatici ai clasei
 +
* operatorul = nu poate fi mostenit
 +
* operatorii pentru tipurile predefinite ale limbajului nu se pot supraincarca
 +
* operatorii nu pot avea argumente implicite
 +
 
 +
Operatorii pentru un anumit tip definit de utilizator (clasa) pot sa fie sau nu membru al clasei. In cazul in care se supraincarca un operator pentru o clasa, dar acel operator nu apartine clasei, trebuie sa fie declarat '''friend''' in clasa respectiva si sa aiba cel putin un argument de tipul clasei respective. Exceptie de la aceasta regula fac operatorii '''= () [] ->''' care nu pot fi supraincarcati folosind functii de tipul friend ale unei anumite clase.
 +
 
 +
== Exemplu ==
 +
 
 +
Un exemplu: voi da ca exemple supraincarcarea operatorilor ++ pre si post indexat.
 +
<code><cpp/>class MyPoint
 +
{
 +
public:
 +
    MyPoint(); // constructor implicit
 +
    MyPoint( double a, double b ); // constructor de initializare
 +
    MyPoint( MyPoint &r ); //constructor de copiere
 +
 +
    MyPoint operator+( MyPoint point ); //suma a 2 vectori
 +
    MyPoint operator-( MyPoint point ); //diferenta a 2 vectori
 +
    double operator*( MyPoint point ); //produsul scalar a 2 vectori
 +
    MyPoint& operator*( double dVal ); //multiplicare cu o constanta
 +
 +
    MyPoint& operator=( MyPoint &point );
 +
    MyPoint& operator=( double a, double b );
 +
 +
    MyPoint operator++(); // ++ prefixat
 +
    MyPoint operator++( int a ); /* ++postfixat - de retinut ca a va avea INTOTDEAUNA valoarea 0 (zero) */
 +
 +
    friend ostream& operator<<( ostream &stream, const MyPoint &pt );
 +
    friend istream& operator>>( istream &stream, const MyPoint &pt );
 +
 +
    int getX();
 +
    int getY();
 +
 +
private:
 +
    double x;
 +
    double y;
 +
};
 +
 +
MyPoint::MyPoint()
 +
{
 +
    x = 0;
 +
    y = 0;
 +
}
 +
 +
MyPoint::MyPoint( MyPoint &r )
 +
{
 +
    x = r.x;
 +
    y = r.y;
 +
}
 +
 +
MyPoint::MyPoint( double a, double b )
 +
{
 +
    x = a;
 +
    y = b;
 +
}
 +
 +
MyPoint MyPoint::operator+( MyPoint point )
 +
{
 +
    MyPoint temp;
 +
    temp.x = x + point.x;
 +
    temp.y = y + point.y;
 +
    return temp;
 +
}
 +
 +
MyPoint MyPoint::operator-( MyPoint point )
 +
{
 +
    MyPoint temp;
 +
    temp.x = x - point.x;
 +
    temp.y = y - point.y;
 +
    return temp;
 +
}
 +
 +
double MyPoint::operator*( MyPoint point )
 +
{
 +
    return x * point.y + y * point.x;
 +
}
 +
 +
MyPoint& MyPoint::operator*( double dVal )
 +
{
 +
    x *= dVal;
 +
    y *= dVal;
 +
    return *this;
 +
}
 +
 +
MyPoint& MyPoint::operator=( MyPoint &point )
 +
  {
 +
    x = point.x;
 +
    y = point.y;
 +
    return *this;
 +
}
 +
 +
MyPoint& MyPoint::operator=( double a, double b )
 +
{
 +
    x = a;
 +
    y = b;
 +
    return *this;
 +
}
 +
 +
MyPoint MyPoint::operator++()
 +
{
 +
    x++;
 +
    y++;
 +
    return *this;
 +
}
 +
 +
MyPoint MyPoint::operator++( int a )
 +
{
 +
    ++x;
 +
    ++y;
 +
    return *this;
 +
}
 +
 +
ostream& operator<<( ostream &stream, const MyPoint &pt )
 +
{
 +
    stream << "( " << pt.x << ", " << pt.y << " )\n";
 +
    return stream;
 +
}
 +
 +
istream& operator>>( istream &stream, const MyPoint &pt )
 +
{
 +
    stream >> pt.x >> pt.y;
 +
    return stream;
 +
}
 +
 +
int MyPoint::getX()
 +
{
 +
    return x;
 +
}
 +
 +
int MyPoint::getY()
 +
{
 +
    return y;
 +
}
 +
 +
int main( int argc, char *argv[] )
 +
{
 +
    MyPoint pt( 5, 7 );
 +
    cout << pt << endl; //afiseaza la consola ( 5, 7 )
 +
    cout << pt++ << endl; //afiseaza la consola ( 5, 7 )
 +
    cout << pt << endl; //afiseaza la consola ( 6, 8 )
 +
    cout << ++pt << endl; //afiseaza la consola ( 7, 9 )
 +
}</code>
 +
 
 +
----
 +
 
 +
Versiunea originală de [[User:Radubolovan|Radu Bolovan]] 12:04, 22 November 2005 (EET)
 +
 
 +
[[Category:Tutorial]]
 +
[[Category:Programming]]

Revision as of 12:30, 13 February 2006

Titlul oribil este rezultatul unei limitări in Wiki -- imediat ce se rezolvă, vom face şi titul mai uşor de privit


Introducere

Operatiile de baza sunt implementate in limbajul C++ cu ajutorul operatorilor. In limbajul C++ operatorii sunt, de fapt, functii. Operatorii oferiti de limbaj pot lucra decat asupra tipurilor de date predefinite. Pentru a folosi operatorii asupra tipurilor de date construite de noi, limbajul C++ ne ofera un mecanism care se numeste supraincarcarea operatorilor.

De ce am avea nevoie de asa ceva? Dupa cum se stie se poate crea o functie a unei clase care sa faca exact ce ar face un operator si care sa o definim (de exemplu) suma. Atunci am apela acea metoda astfel:

Obiect ob1;
Obiect ob2;
Obiect ob3 = ob1.suma( ob2 );

Cam greoi! Ar fi mai simplu daca am scrie aşa:

Obiect ob3 = ob1 + ob2;

Astfel s-a ajuns la nevoia de a supraincarcarea operatorii!

Teorie

Supraincarcarea metodelor claselor in general si a operatorilor in particular sunt doua dintre cele mai importante mecanisme ale limbajului C++. Ele sunt un pas urias catre polimorfism (asta este alta bazaconie inventata de oamenii cu creiere luminate). Aceste oportunitati sunt cele care ofera flexibilitate aplicatiilor pe care le construim.

Iata lista operatorilor din C++ care se pot supraincarca:

  • new delete
  • () []
  • + - * / %
  • ^ & | ~
  •  ! = < >
  • += -= *= /= %=
  • ^= &= |=
  • << >>
  • >>= <<=
  • == != <= >=
  • && ||
  • ++ --
  • ,
  • ->*
  • ->

unde operatorul () este apelul unei functii, iar operatorul [] este operatorul de indexare.

Urmatorii operatori nu se pot supraincarca:

  • . .* :: ?: sizeof

Reguli care trebuie sa fie respectate in supraincarcarea operatorilor:

  • operatorii =, (), [], si -> trebuie sa fie membrii nestatici ai clasei
  • operatorul = nu poate fi mostenit
  • operatorii pentru tipurile predefinite ale limbajului nu se pot supraincarca
  • operatorii nu pot avea argumente implicite

Operatorii pentru un anumit tip definit de utilizator (clasa) pot sa fie sau nu membru al clasei. In cazul in care se supraincarca un operator pentru o clasa, dar acel operator nu apartine clasei, trebuie sa fie declarat friend in clasa respectiva si sa aiba cel putin un argument de tipul clasei respective. Exceptie de la aceasta regula fac operatorii = () [] -> care nu pot fi supraincarcati folosind functii de tipul friend ale unei anumite clase.

Exemplu

Un exemplu: voi da ca exemple supraincarcarea operatorilor ++ pre si post indexat.

<cpp/>class MyPoint
{
public:
   MyPoint(); // constructor implicit
   MyPoint( double a, double b ); // constructor de initializare
   MyPoint( MyPoint &r ); //constructor de copiere

   MyPoint operator+( MyPoint point ); //suma a 2 vectori
   MyPoint operator-( MyPoint point ); //diferenta a 2 vectori
   double operator*( MyPoint point ); //produsul scalar a 2 vectori
   MyPoint& operator*( double dVal ); //multiplicare cu o constanta

   MyPoint& operator=( MyPoint &point );
   MyPoint& operator=( double a, double b );

   MyPoint operator++(); // ++ prefixat
   MyPoint operator++( int a ); /* ++postfixat - de retinut ca a va avea INTOTDEAUNA valoarea 0 (zero) */

   friend ostream& operator<<( ostream &stream, const MyPoint &pt );
   friend istream& operator>>( istream &stream, const MyPoint &pt );

   int getX();
   int getY();

private:
   double x;
   double y;
};

MyPoint::MyPoint()
{
   x = 0;
   y = 0;
}

MyPoint::MyPoint( MyPoint &r )
{
   x = r.x;
   y = r.y;
}

MyPoint::MyPoint( double a, double b )
{
   x = a;
   y = b;
}

MyPoint MyPoint::operator+( MyPoint point )
{
   MyPoint temp;
   temp.x = x + point.x;
   temp.y = y + point.y;
   return temp;
}

MyPoint MyPoint::operator-( MyPoint point )
{
   MyPoint temp;
   temp.x = x - point.x;
   temp.y = y - point.y;
   return temp;
}

double MyPoint::operator*( MyPoint point )
{
   return x * point.y + y * point.x;
}

MyPoint& MyPoint::operator*( double dVal )
{
   x *= dVal;
   y *= dVal;
   return *this;
}

MyPoint& MyPoint::operator=( MyPoint &point )
{
   x = point.x;
   y = point.y;
   return *this;
}

MyPoint& MyPoint::operator=( double a, double b )
{
   x = a;
   y = b;
   return *this;
}

MyPoint MyPoint::operator++()
{
   x++;
   y++;
   return *this;
}

MyPoint MyPoint::operator++( int a )
{
   ++x;
   ++y;
   return *this;
}

ostream& operator<<( ostream &stream, const MyPoint &pt )
{
   stream << "( " << pt.x << ", " << pt.y << " )\n";
   return stream; 
}

istream& operator>>( istream &stream, const MyPoint &pt )
{
   stream >> pt.x >> pt.y;
   return stream;
}

int MyPoint::getX()
{
   return x;
}

int MyPoint::getY()
{
   return y;
}

int main( int argc, char *argv[] ) 
{
   MyPoint pt( 5, 7 );
   cout << pt << endl; //afiseaza la consola ( 5, 7 )
   cout << pt++ << endl; //afiseaza la consola ( 5, 7 )
   cout << pt << endl; //afiseaza la consola ( 6, 8 )
   cout << ++pt << endl; //afiseaza la consola ( 7, 9 )
}

Versiunea originală de Radu Bolovan 12:04, 22 November 2005 (EET)