31.12.2014 Views

Paskaitų konspektai - Matematikos ir Informatikos fakultetas ...

Paskaitų konspektai - Matematikos ir Informatikos fakultetas ...

Paskaitų konspektai - Matematikos ir Informatikos fakultetas ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

6. Operatorių perkrovimas<br />

Motyvacija<br />

Realizuokime kompleksinius skaičius su realiąja <strong>ir</strong> menamąja dalimis. Apibr÷žkime<br />

kompleksinių skaičių sud÷tį, daugybą, išvedimą į srautą:<br />

// operator1.cpp<br />

class Complex {<br />

protected:<br />

double r;<br />

double i;<br />

public:<br />

Complex (double re=0, double im=0) : r(re), i(im) {}<br />

Complex add const (const Complex& c);<br />

Complex multiply const (const Complex& c);<br />

void print const (ostream& os);<br />

};<br />

Apskaičiuokime reiškinį d=a + b*c:<br />

Rezultatas:<br />

int main ()<br />

{<br />

Complex a (2.5, 1.5);<br />

Complex b (10, 2);<br />

Complex c (4);<br />

Complex d;<br />

d = b.multiply(c);<br />

d = a.add(d);<br />

d.print(cout);<br />

}<br />

(42.5+i9.5)<br />

O ar negal÷tume mes to paties reiškinio užrašyti įprastiniu matematiniu pavidalu<br />

int main ()<br />

{<br />

...<br />

d = a + b*c;<br />

cout

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!