4. Device (Qurilma) nomli ajdod sinf hosil qiling. Uning tarkibida qurilma seril raqami va paroli xossalari bo’lsin. Computer va Telephone avlod sinflarini hosil qilib, uning qaysi operatsion sistemadan foydalanayotgani haqida xabarni chop etuvchi metodlarini yarating. Agar qurilma paroli to’g’ri kiritilganda tizim tushgani xabar chop etilsin.
#include
using namespace std;
class Device {
public:
int serial_number = 12345678;
protected:
string m_password = "A11111";
public:
void turn_on() {
cout << "Qurilma yoniq" << endl;
}
};
class Computer: private Device {
public:
void say_hello() {
turn_on();
cout << "Salom Windows 10 ishga tushdi !" << endl;
}
bool getCheckPassword(string password)
{
if(m_password == password)
return true;
else
return false;
}
};
int main() {
Device Device_instance;
Computer Computer_instance;
string password;
cout << "\t Qurilma" << endl;
cout << "Serial raqam: "<< Device_instance.serial_number << endl;
Device_instance.turn_on();
cout<<"Qurilmaga kirish parolini kiriting: ";
getline (cin, password);
if(Computer_instance.getCheckPassword(password))
{
cout<<"Parol to'g'ri kiritildi!";
cout << "\t Computer" << endl;
Computer_instance.say_hello();
}
else
{
cout<<"Parol xato kiritildi!";
}
return 0;
}
5. Human ajdod sinfini hosil qiling. Uning tarkibida ism, familiyasi, otasining ismi kabi maydonlar bo’lsin. Student avlod sinfini hosil qilib, talabaning o’rtacha balini hisoblaydigan metod yozing.
#include
#include
#include
#include
using namespace std;
class human {
private:
string name; // ism
string last_name; // familiya
string second_name; // otasining ismi
public:
// human sinfining konstruktori
human(string last_name, string name, string second_name)
{
this->last_name = last_name;
this->name = name;
this->second_name = second_name;
}
//Insonning FISH ni olish
string get_full_name()
{
ostringstream full_name;
full_name << this->last_name << " "
<< this->name << " "
<< this->second_name;
return full_name.str();
}
};
class student : public human {
private:
// Talabaning baholari
vector scores;
public:
// Student sinfining konstruktori
student(string last_name, string name, string second_name, vector scores) : human(
last_name, name, second_name)
{
this->scores = scores;
}
// Talabaning o'rtacha balini olish
float get_average_score()
{
// Baholarning umumiy soni
unsigned int count_scores = this->scores.size();
// Talabalarning barcha baholari yig'indisi
unsigned int sum_scores = 0;
// O'rtacha ball
float average_score;
for (unsigned int i = 0; i < count_scores; ++i) {
sum_scores += this->scores[i];
}
average_score = (float) sum_scores / (float) count_scores;
return average_score;
}
};
int main(int argc, char* argv[])
{
// Talabaning bahosi
vector scores;
int scienceCount = 0;
int ball;
cout<<"Fanlar sonini kiriting: "; cin>>scienceCount;
for(int i=1; i<=scienceCount; i++)
{
// Talaba baholarini vektorga qo'shish
cout<cin>>ball;
scores.push_back(ball);
}
// student sinfining obyektini yaratish
student *stud = new student("Anvar", "Samadov", "Negmatovich", scores);
// Talabaning toʻliq ismini koʻrsatish (inson sinfining meros usulidan foydalangan holda)
cout << stud->get_full_name() << endl;
// Talabaning o'rtacha balli bo'yicha xulosa
cout << "O'rtacha ball: " << stud->get_average_score() << endl;
return 0;
}
Do'stlaringiz bilan baham: |