Lecture notes on



Download 232,82 Kb.
bet21/45
Sana07.07.2022
Hajmi232,82 Kb.
#755880
1   ...   17   18   19   20   21   22   23   24   ...   45
Bog'liq
285 OOPS lecture notes Complete-конвертирован

operator -- ()


{
--m;
--n;
}
};
void main()
{
abc x; x.show();
--x;
x.show();
}

Unary – - operator overloading(using friend function):


class abc
{
int m,n; public:
abc()
{ m=8; n=9;
}
void show()
{
cout<}
friend operator --(abc &p);
};

operator -- (abc &p)


{
--p.m;
--p.n;
}
};
void main()
{
abc x; x.show();
operator--(x);
x.show();
}
Unary operator+ for adding two complex numbers (using member function)

class complex


{
float real,img; public: complex()
{
real=0; img=0;
}
complex(float r,float i)
{
real=r; img=i;
}
void show()
{
cout<}
complex operator+(complex &p)
{
complex w; w.real=real+q.real; w.img=img+q.img; return w;
}
};
void main()
{
complex s(3,4); complex t(4,5); complex m; m=s+t; s.show();
t.show();
m.show();
}

Unary operator+ for adding two complex numbers (using friend function)


class complex


{
float real,img; public: complex()
{
real=0; img=0;
}
complex(float r,float i)
{
real=r; img=i;
}
void show()
{
cout<}
friend complex operator+(complex &p,complex &q);
};
complex operator+(complex &p,complex &q)
{
complex w; w.real=p.real+q.real; w.img=p.img+q.img; return w;
}
};
void main()
{
complex s(3,4);complex t(4,5); complex m;
m=operator+(s,t); s.show();t.show(); m.show();
}
Overloading an operator does not change its basic meaning. For example assume the + operator can be overloaded to subtract two objects. But the code becomes unreachable.
class integer
{
intx, y;
public:
int operator + ( ) ;
}
int integer: : operator + ( )
{
return (x-y) ;
}
Unary operators, overloaded by means of a member function, take no explicit argument and return no explicit values. But, those overloaded by means of a friend function take one reference argument (the object of the relevant class).
Binary operators overloaded through a member function take one explicit argument and those which are overloaded through a friend function take two explicit arguments.



Download 232,82 Kb.

Do'stlaringiz bilan baham:
1   ...   17   18   19   20   21   22   23   24   ...   45




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

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish