Aliyev o. A, Xo‘jamuratov b. «C++ da dasturlash» fanidan tajriba ishlarini bajarish bo‘yicha uslubiy ko‘rsatma


inline complex operator +(complex b)


Download 2.01 Mb.
bet23/27
Sana20.11.2023
Hajmi2.01 Mb.
#1787789
1   ...   19   20   21   22   23   24   25   26   27
Bog'liq
file8

inline complex operator +(complex b)
{return complex(re+b.re, im+b.im)}
va this ko’rsatkichidan foydalanish mumkin:
inline complex operator+=(complex b)
{re+=b.re, im+=b.im; return *this}
Funksiya – amalni chaqirish:
complex c = a + b; //qisqa yozuv
complex d = a.operator+(b); // oshkora chaqirish
Misol:
class complex
{
double re, im;
public:
complex(double r, double i) { re=r; im=i; }
friend complex operator+( complex , complex);
friend complex operator+( complex ,double);
friend complex operator+( double, complex);
};
inline complex operator+(complex a, complex b) // complex+complex
{complex c; c.re=a.re+b.re; c.im=a.im+b.im; return c;}
inline complex operator+(complex a, double x) //complex+double
{ complex c; c.re=a.re+x; c.im=im; return c;}
inline complex operator+(double x, complex a) // double + complex
{complex c; c.re=a.re+x; c.im=im; return c;}
Funksiya – amalni chaqirish:
complex c = a + b; //qisqa yozuv
complex d = operator+(a,b); //oshkora chaqirish
Unar amallarni qo’shimcha yuklash
Ixtiyoriy  unar amali ikkita usulda tavsiflanish mumkin: yoki parametrsizli funksiya komponentasi sifatida, yoki bitta parametrli global (do’stona bo’lishi mumkin) funksiya sifatida. Birinchi xolatda  Z ifoda Z.operator  (), ikkinchi xolatda esa operator (Z) chaqirilishini bildiradi.
Aniq sinf doirasida qayta yuklangan unar operatsiyalar faqat parametrsizli nostatik komponentli funksiya orqali qayta yuklanadi. Sinfdagi chaqiriladigan obyekt avtomatik ravishda operand sifatida qabul qilinadi.
Sinf doirasidan tashqarida qayta yuklangan unar operatsiyalar (global funksiya kabi) sinf tipdagi bitta parametrga ega bo’lishi lozim. Shu parametr orqali uzatiladigan obyekt operand sifatida qabul qilinadi.
Sintaksis:
a) birinchi xolda (sinf doirasida tavsiflash):

Download 2.01 Mb.

Do'stlaringiz bilan baham:
1   ...   19   20   21   22   23   24   25   26   27




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling