-MAVZU: Friend funksiyalar va sinflar

|
-MAVZU: Friend funksiyalar va sinflar
|
bet | 2/3 | Sana | 07.07.2024 | Hajmi | 269,98 Kb. | | #266872 |
3-MAVZU: Friend funksiyalar va sinflar.
1-masala: Student classini yarating. Private maydon hosil qiling va
unda studentning name, fam va kursi bo’lsin. Frienddan
foydalangan holatda chop e’tuvchi funksiyani class bilan
biriktiring.
(Ism, fam va kurs konsulda kiritilsin)
Kodi:
#include
using namespace std;
class Student{
private:
string name;
string fam;
int kurs;
public:
Student(){}
Student(string name, string fam, int kurs){
this->name=name;
this->fam=fam;
this->kurs=kurs;
}
string getn(){
return name;
}
string getf(){
return fam;
}
int getk(){
return kurs;
}
friend void print(Student*,int);
};
void print(Student *ob){
cout << ob->getn() << " " << ob->getf() << " " << ob->getk() << endl;
}
int main(){
Student ob;
int k;
string str1,str2;
cout<<"Ismi: ";
cin.ignore();
getline(cin,str1);
cout<<"Familiyasi: ";
getline(cin,str2);
cout<<"Kursi:";
cin>>k;
ob=Student(str1,str2,k);
print(&ob);
}
NATIJASI:
4-MAVZU: Inkapsulyatsiya va merosxo‘rlik.
1-masala: Car1 va Car2 deb nomlangan classlar yarating.
Car2 ga Car1 dan meros oling. Car1 da
mashina brandi, modeli va yili bo’lsin. Car2da
esa mashina rangi bo’lsin. Ularni chop
qiluvchi funksiya orqali ularni konsulga
chiqaring.
Kodi:
#include
#include
using namespace std;
class Car1 {
protected:
string brand;
string model;
int yil;
public:
Car1(string b, string m, int y) {
brand = b;
model = m;
yil = y;
}
void chop() {
cout << "Avtomobil 1: " << brand << " " << model << " (" << yil <<
")" << endl;
}
};
class Car2 : public Car1 {
private:
string color;
public:
Car2(string b, string m, int y, string c) : Car1(b, m, y) {
color = c;
}
void chop() {
cout << "Avtomobil 2: " << brand << " " << model << " (" << yil <<
")" << endl;
cout << "Rangi: " << color << endl;
}
};
int main() {
Car1 car1("Matiz", "MX", 2021);
Car2 car2("Honda", "Accord", 2022, "Qora");
car1.chop();
car2.chop();
return 0;
}
NATIJASI:___7-MAVZU:_Kontеynеrlar_bilan_ishlash_1-masala'>NATIJASI:_5-MAVZU:_Polimorfizm._1-masala'>NATIJASI:
5-MAVZU: Polimorfizm.
1-masala: Xodim va Mudir nomli class yaratilsin. Ularning ismi va yoshi
kiritilsin. Chop_qil nomli funksiya override qilinsin va u
yordamida ularning ma’lumotlarini konsulda chiqarilsin.
Kodi:
NATIJASI:
6-MAVZU: Funksiya va sinf shablon
(template) lari bilan ishlash
1-masala: Template yordamida ikkita sonni qo’shadigan
va ularni juft yoki toq ekanligini aniqlaydigan
funksiya shablonlari yaratilsin
Kodi: #include
using namespace std;
template
T summa(T a, T b) {
return a + b;
}
template
U tekshir(T a) {
U t = true;
if (a % 2 == 1) {
t = false;
}
return t;
}
int main() {
int a, b;
cout << "Birir son kiriting: ";
cin >> a;
cout << "Ikkinchi son kiriting: ";
cin >> b;
cout << "Sonlarning yig'indisi: " << summa(a, b) << endl;
cout << "Birinchi son ";
if (tekshir(a)) {
cout << "juft son" << endl;
}
else {
cout << "toq son" << endl;
}
cout << "Ikkinchi son ";
if (tekshir(b)) {
cout << "juft son" << endl;
}
else {
cout << "toq son" << endl;
}
return 0;
}
NATIJASI:
7-MAVZU: Kontеynеrlar bilan ishlash
1-masala: Deque konteyneridan foydalanib berilgan sonlarni chop
qiling. Undan so’ng esa shu sonlar orasidan eng katta
bo’lganini o’chirgan holatda qaytadan chop qiling.
Kodi:
#include
#include
using namespace std;
int main() {
deque dq = { 7,3,5,9,11,6 };
for (auto it = dq.begin(); it != dq.end(); it++) {
cout << *it << "\t";
}
cout << endl;
auto max = dq.begin();
for (auto it = dq.begin() + 1; it != dq.end(); it++) {
if (*max < *it) {
max = it;
}
}
dq.erase(max);
for (auto it = dq.begin(); it != dq.end(); it++) {
cout << *it << "\t";
}
cout << endl;
}
NATIJASI:
8-MAVZU: Visual Studio muhitida
komponentalardan unumli foydalangan
holda dasturlar yaratish
1-masala: O’quvchi ma’lumotlarini kirituvchi dastur yarating. Unda
o’quvchining ism familyasi kiritilgandan so’ng kirish
tugmasi bosilgancha xush kelibsiz degan yozuv chiqsin,
tozalash tugmasi bosilganda esa barcha ma’lumotlar
tozalansin.
Kodi: #pragma endregion
private: System::Void Form1_Load(System::Object^
sender, System::EventArgs^ e) {
}
private: System::Void
label1_Click(System::Object^ sender,
System::EventArgs^ e) {
}
private: System::Void
button1_Click(System::Object^ sender,
System::EventArgs^ e) {
label3->Text = "Xush kelibsiz!";
}
private: System::Void label3_Click(System::Object^
sender, System::EventArgs^ e) {
}
private: System::Void button2_Click(System::Object^
sender, System::EventArgs^ e) {
textBox1->Text = "";
textBox2->Text = "";
label3->Text = "";
}
|
| |