O‘zbekiston respublikasi oliy va o‘rta maxsus ta’lim vazirligi tоshkеnt dаvlаt iqtisоdiyot universitеti


Binar amallarni qo‘shimcha yuklash


Download 1.96 Mb.
Pdf ko'rish
bet47/83
Sana08.11.2023
Hajmi1.96 Mb.
#1757524
1   ...   43   44   45   46   47   48   49   50   ...   83
Bog'liq
Obyektga yo`naltirilgan dasturlash tillari

9.2. Binar amallarni qo‘shimcha yuklash 
Ixtiyoriy 
binar amali ikkita usulda tavsiflanish mumkin: yoki bitta parametrli 
funksiya komponentasi sifatida, yoki ikkita parametrli global (do‘stona bo‘lishi 
mumkin) funksiya sifatida.
Birinchi xolatda x y ifoda x.operator (y), ikkinchi holda esa operator 
(x,y) 
chaqirilishini bildiradi.
Aniq sinf doirasida qayta yuklangan operatsiyalar faqat parametrli nostatik 
komponentli funksiya orqali qayta yuklanadi. Sinfdagi chaqiriladigan obyekt 
avtomatik ravishda birinchi operand sifatida qabul qilinadi. 
Misol: 
class complex

double re, im; 
public: 
complex(double r, double i) { re=r; im=i; } 
complex operator+(complex); 
complex operator+(double); 
complex& operator+=(complex); 
complex& operator+=(double x); 
}; 
inline complex operator+(complex b) //complex+complex 
{ complex c; c.re=re+b.re; c.im=im+b.im; return c;}


78 
inline complex operator+( double x) //complex+double 
{ return complex(re+x, im);}
inline complex& complex::operator+=(complex b) //complex+=complex 
{ complex c; re += b.re; im += b.im; c.re=re; c.im=im; return c; } 
inline complex& complex::operator+=(double x) //complex+=double 
{ re += x; return *this; } 
Funksiya – amalni chaqirish: 
complex c = a + b; // qisqa yozuv 
complex d = a.operator+(b); // oshkora chaqirish 
Shuni ta’kidlab o‘tish kerakki double+complex tipidagi qo‘shishni bu usulda 
qo‘shimcha yuklash mumkin emas. 
Sinfdan tashqari yuklanadigan operatsiyalar ikkita operandga ega bo‘lishi kerak. 
Ulardan biri sinf tipiga ega bo‘lishi lozim. 
Qiymat qaytarish uchun vaqtinchalik o‘zgaruvchidan foydalaniladi. Bu 
maqsadda konstruktordan: 
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}
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); 


79 
}; 
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 

Download 1.96 Mb.

Do'stlaringiz bilan baham:
1   ...   43   44   45   46   47   48   49   50   ...   83




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