Mashina nomli sinf yarating. Tarkibida public, private spetsifikatorlari orqali metodlar va xossalar e’lon qiling. Ulardan asosiy dasturda foydalaning.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#include
/*4.1 - masala By -> (@TBCLbot and @Lazizbek_Karimov2002) 203 - guruh(maxsus)*/
using namespace std;
class Mashina
{
private:
int m_Road;
int m_Speed;
public:
string m_Name;
int m_Year;
void setValue(string Name, int Year, int Road, int Speed)//To'rt parametrli Set metodi
{
string m_Name = Name;
int m_Year = Year;
int m_Road = Road;
int m_Speed = Speed;
}
int AgeCar(int NowYear)// AgeCar() metodi mashina ishlab chiqarilganinga necha yil bo'lganini aniqlash
{
return NowYear - m_Year;
}
int getRoad(){return m_Road;}//Road (bosib o'tilgan yo'l) qiymatini olish get metodi
int getSpeed(){return m_Speed;}//Speed(tezlik) qiymatini olish get metodi
void Print()//Obyektning ma'lumotlarini ekranga chop etuvchi Print() (funksiya_)
{
cout << m_Name << " mashinasi " << m_Year << " yilda ishlab chiqarilgan.";
cout << m_Name << " tezligini " << getSpeed() << " km/soat gacha chiqara oladi va a"<< getRoad() <<" km masofani bosib o'tgan.\n\n";
}
};
int main()
{
Mashina Car;
string Name;
int Speed, Road, Year, NowYear;
cout << "Mashina nomi : ";cin >> Name;
cout << "Ishlab chiqarilgan yili : "; cin >> Year;
cout << "Bosib o'tgan masofasi : "; cin >> Road;
cout << "Maksimal tezlik : "; cin >> Speed;
Car.setValue( Name, Year, Road, Speed);//setValue metodiga qiymat yuborish
Car.Print();//Ekranga Klass ma'lumotlarini chop etish
cout << "Mashina ishlab chiqarilganinga" << Car.AgeCar(NowYear) << "yil bo'lgan.";
return 0;
}
|
5-TOPSHIRIQ
Mashina nomli sinf yarating. Mashina raqamiga qarab uning qaysi viloyat mashinasini aniqlash metodni hosil qiling va undan asosiy dasturda foydalaning.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#include
/*5.1 - masala By -> (@TBCLbot and @Lazizbek_Karimov2002) 203 - guruh(maxsus)*/
using namespace std;
class Mashina
{
private:
string m_Name;
string m_Num;
int m_Speed;
public:
Mashina()//Jimlik bo’yicha konstruktor
{
m_Name = "No Name";
m_Num = "No Avto Number";
m_Speed = 0;
}
Mashina(string Name,string Num,int Speed)//Uch parametrli konstruktor
{
m_Name = Name;
m_Num = Num;
m_Speed = Speed;
}
void setValue(string Name,int Num,int Speed)//Uch parametrli Set metodi
{
m_Name = Name;
m_Num = Num;
m_Speed = Speed;
}
string getName(){return m_Name;}//Name(nom) qiymatini olish get metodi
string getNum(){return m_Num;}//Year(chiqarilgan yil) qiymatini olish get metodi
int getSpeed(){return m_Speed;}//Speed(tezlik) qiymatini olish get metodi
string getViloyat()//Viloyatni aniqlash metodi
{
int n1 , n2 , n;
string m , region;
m = getNum();
n1 = m[0] - 48;
n2 = m[1] -48;
n = n1*10 + n2;
if(1 <= n and n <= 9)
region = "Toshkent shahri";
else if (10 <= n and n <= 19)
region = "Toshkent viloyati";
else if (20 <= n and n <= 24)
region = "Sirdaryo viloyati";
else if (25 <= n and n <= 29)
region = "Jizzax viloyati";
else if (30 <= n and n <= 39)
region = "Samarqand viloyati";
else if (40 <= n and n <= 49)
region = "Farg'ona viloyati";
else if (50 <= n and n <= 59)
region = "Namangan viloyati";
else if (60 <= n and n <= 69)
region = "Andijon viloyati";
else if (70 <= n and n <= 74)
region = "Qashqadaryo viloyati";
else if (75 <= n and n <= 79)
region = "Surxondaryo viloyati";
else if (80 <= n and n <= 84)
region = "Buxoro viloyati";
else if (85 <= n and n <= 89)
region = "Navoiy viloyati";
else if (90 <= n and n <= 94)
region = "Xorazim viloyati";
else if (95 <= n and n <= 99)
region = "Qoraqolpog'iston Respublikasi";
return region;
}
void Print()//Obyektning ma'lumotlarini ekranga chop etuvchi Print() (funksiya_)
{
cout << getName() << " mashinasing avtoraqami < " << getNum() << " > .";
cout << getName() << " tezligini " << getSpeed() << " km/soat gacha chiqara oladi."<< "\n\n";
}
};
int main(){
Mashina Car1, Car2 = {"Damas" , "30 B221AO", 160};// 2 ta Car1 va Car2 obyectlari hosil qilindi
Car1.Print(); //Birinchi obyektni ma'lumotlarini ekranga Print() metodi orqali chop etish
Car2.Print(); //Ikkinchi obyektni ma'lumotlarini ekranga Print() metodi orqali chop etish
cout << Car2.getName() << " mashinasi " << Car2.getViloyat() << " mashinasi.";
return 0;
}
|
Do'stlaringiz bilan baham: |