Rectangle - Mustaqil ishi Bajardi: Abdusoatov j tekshirdi: Qurbonov N

|
Mustaqil ishi Bajardi: Abdusoatov j tekshirdi: Qurbonov N
|
bet | 3/5 | Sana | 13.05.2024 | Hajmi | 0,54 Mb. | | #230132 |
Bog'liq Abdusoatov Javlonbek.Rectangle sinfi uning ichidagi maxsus xususiyatlarga murojaat qilish uchun friend deb belgilanadi.
#include
using namespace std;
class Rectangle {
private:
int length;
int width;
public:
Rectangle(int l, int w) : length(l), width(w) {}
friend void displayArea(Rectangle obj);
};
void displayArea(Rectangle obj) {
cout << "Area of Rectangle: " << obj.length * obj.width << endl;
}
int main() {
Rectangle rect(5, 4);
displayArea(rect);
return 0;
}
4-mavzuga oid dasturlar
1. Ushbu dasturda, "Person" va "Student" nomli ikkita sinf yaratilgan. "Person" sinfi bir kishining ismini, yoshini va kasbi (profession)ni saqlaydi. "Student" sinfi "Person" sinfiga meros qiladi va qo'shimcha ravishda talaba ID-sini saqlaydi. Student sinfi, Person sinfini meros qilib, Person sinfning xususiyatlariga murojaat qiladi.
#include
#include
using namespace std;
class Person {
private:
string name;
int age;
protected:
string profession;
public:
Person(string n, int a, string p) : name(n), age(a), profession(p) {}
string getName() const { return name; }
void setName(string n) { name = n; }
int getAge() const { return age; }
void setAge(int a) { age = a; }
string getProfession() const { return profession; }
void setProfession(string p) { profession = p; }
};
class Student : public Person {
private:
int studentID;
public:
Student(string n, int a, string p, int id) : Person(n, a, p), studentID(id) {}
int getStudentID() const { return studentID; }
};
int main() {
Student student("Javlon", 20, "Engineering", 934974);
cout << "Name: " << student.getName() << endl;
cout << "Age: " << student.getAge() << endl;
cout << "Profession: " << student.getProfession() << endl;
cout << "Student ID: " << student.getStudentID() << endl;
return 0;
}
2.Ushbu dasturda "Animal" va "Dog" nomli ikkita sinf yaratilgan. "Animal" sinfi hayvonning turlarini va yoshini saqlaydi, "Dog" sinfi esa "Animal" sinfini meros qilib, qo'shimcha ravishda itning tuyulgan turlarini saqlaydi.
#include
#include
using namespace std;
class Animal {
private:
string species;
protected:
int age;
public:
Animal(string sp, int ag) : species(sp), age(ag) {}
string getSpecies() const { return species; }
void setSpecies(string sp) { species = sp; }
int getAge() const { return age; }
void setAge(int ag) { age = ag; }
};
class Dog : public Animal {
private:
string breed;
public:
Dog(string sp, int ag, string br) : Animal(sp, ag), breed(br) {}
string getBreed() const { return breed; }
void setBreed(string br) { breed = br; }
};
int main() {
Dog myDog("Canine", 5, "Labrador");
cout << "Species: " << myDog.getSpecies() << endl;
cout << "Age: " << myDog.getAge() << " years old" << endl;
cout << "Breed: " << myDog.getBreed() << endl;
return 0;
}
5-mavzuga oid dasturlar
Ushbu dasturda "Animal", "Dog" va "Cat" nomli sinflar yaratilgan. "Animal" sinfi boshqa sinflarga meros qiladi va "sound" funksiyasini virtual e'tibor qiladi. "Dog" va "Cat" sinflari esa "Animal" sinfini meros qilib, ularning o'zlariga xos "sound" funksiyalarini qayta yozishadi (
|
| |