Na’muna №1:
Yosh xususiyatga ega bo'lgan sinfni aniqlang. Odamning yoshini oshirish uchun ++
operatorini qayta yuklang:
public
class
Person
{
public
int
Age {
get
;
set
; }
public
static
Person
operator
++(Person person)
{
person.Age++;
return
person;
}
}
Na’muna №2:
Numerator va Denominator xossalariga ega Fraction sinfini aniqlang. Bir kasrni
boshqasiga bo'lish natijasini qaytarish uchun / operatorini qayta yuklang:
public
class
Fraction
{
public
int
Numerator {
get
;
set
; }
public
int
Denominator {
get
;
set
; }
public
static
Fraction
operator
/(Fraction a, Fraction b)
{
Fraction result =
new
Fraction();
result.Numerator = a.Numerator * b.Denominator;
result.Denominator = a.Denominator * b.Numerator;
return
result;
}
}
Na’muna №3:
Ikki o'lchovli int massivli Elementlar sinfi Matritsasini aniqlang. Ikki matritsa
qo'shish uchun + operatorini qayta yuklang:
public
class
Matrix
{
public
int
[,] Elements {
get
;
set
; }
public
static
Matrix
operator
+(Matrix a, Matrix b)
{
int
rows = a.Elements.GetLength(0);
int
cols = a.Elements.GetLength(1);
Matrix result =
new
Matrix();
result.Elements =
new
int
[rows, cols];
for
(
int
i = 0; i < rows; i++)
{
for
(
int
j = 0; j < cols; j++)
{
result.Elements[i, j] = a.Elements[i, j] + b.Elements[i, j];
}
}
return
result;
}
}
Na’muna №4:
Raqamli xususiyatga ega telefon raqami sinfini aniqlang. Ikki telefon raqamini
solishtirish uchun == operatorini qayta yuklang:
public
class
PhoneNumber
{
public
string
Number {
get
;
set
; }
public
static
bool
operator
==(PhoneNumber a, PhoneNumber b)
{
if
(ReferenceEquals(a, b))
{
return
true
;
}
if
(a
is
null
|| b
is
null
)
{
return
false
;
}
return
a.Number == b.Number;
}
public
static
bool
operator
!=(PhoneNumber a, PhoneNumber b)
{
return
!(a == b);
}
}
Na’muna №5:
Kenglik va balandlik xossalariga ega To'rtburchaklar sinfini belgilang. Ikki
toʻrtburchakni maydon boʻyicha solishtirish uchun == operatorini qayta yuklang:
public
class
Rectangle
{
public
int
Width {
get
;
set
; }
public
int
Height {
get
;
set
; }
public
static
bool
operator
==(Rectangle a, Rectangle b)
{
if
(ReferenceEquals(a, b))
{
return
true
;
}
if
(a
is
null
|| b
is
null
)
{
return
false
;
}
return
a.Width * a.Height == b.Width * b.Height;
}
public
static
bool
operator
!=(Rectangle a, Rectangle b)
{
return
!(a == b);
}
}
Mustaqil bajarish uchun savol va topshiriqlar.
Operatorni haddan tashqari yuklash mashqlari:
1. X va y xossalariga ega 2D vektorni ifodalash uchun Vector2 sinfini yarating.
Vektorni inkor qilish uchun unary - operatorni ortiqcha yuklang.
2. Numerator va maxraj xususiyatlariga ega kasrni ifodalash uchun Fraction sinfini
yarating. Kasrni o'zgarmagan holda qaytarish uchun unary + operatorini ortiqcha
yuklang.
3. Tselsiy bo'yicha qiymat xususiyatiga ega haroratni ifodalash uchun Harorat
sinfini yarating. Haroratni bir daraja Selsiyga oshirish uchun unary ++ operatorini
ortiqcha yuklang.
4. Dollarda qiymat xususiyatiga ega pul summasini ifodalash uchun Money sinfini
yarating. Miqdorni bir dollarga kamaytirish uchun unary -- operatorni ortiqcha
yuklang.
5. Haqiqiy va xayoliy xususiyatlarga ega kompleks sonni ifodalash uchun
Kompleks sinf yarating. Kompleks sonning konjugatini hisoblash uchun unary ~
operatorini ortiqcha yuklang.
6. Ma'lumotlar xususiyatiga ega 2D matritsani ifodalash uchun Matrix sinfini
yarating. Unaryni haddan tashqari yuklang! matritsaning transpozitsiyasini
hisoblash uchun operator.
7. Qizil, yashil va ko'k xususiyatlarga ega rangni ifodalash uchun Color sinfini
yarating. Har bir rang komponentini bittaga oshirish uchun unary ++ operatorini
ortiqcha yuklang.
8. Herts chastotasi xususiyatiga ega signalni ifodalash uchun Signal sinfini
yarating. Chastotani bir gertsga kamaytirish uchun unary -- operatorni ortiqcha
yuklang.
9. 0 dan 6 gacha qiymat xususiyati bilan haftaning bir kunini ifodalash uchun
DayOfWeek sinfini yarating (yakshanbadan shanbagacha). Hafta kunini bir marta
oldinga o'tkazish uchun unary ++ operatorini ortiqcha yuklang.
10. Qiymat xususiyatiga ega ikkilik sonni ifodalash uchun BinaryNumber sinfini
yarating. Unaryni haddan tashqari yuklang! ikkilik sonning bir to'ldiruvchisini
hisoblash uchun operator.
|