masalalari.
Variantda berilgan class, konstruktor va class maydonlari yordamida noma’lum nomlar fazosini yarating. Class da obyektlar massivi ustida quyidagi funksiyalarni bajaruvchi 3 ta static metod yozing:
Yuqoridagi metodlar uchun class maydonlaridan ixtiyoriysi tanlansin.
#include
#include
#include
#include
using namespace std;
namespace mySpace
{
class Kalit{
string nomi, turi;
int yili, soni;
public:
friend void qidirish(mySpace::Kalit k[], int n);
friend void saralash(mySpace::Kalit k[], int n);
friend void ozgartirish(mySpace::Kalit k[], int n);
void set_malumot(string N, string T, int Y){
nomi=N;
turi=T;
yili=Y;
srand(time(NULL));
soni=rand()%20+5;
}
void get_malumot(){
cout << setw(15) << nomi << setw(15) << turi << setw(15) << yili << setw(15) << soni << endl;;
}
void cha(){
cout << setw(5) << "N" << setw(15) << "Nomi" << setw(15) << "Turi" << setw(15) << "Yili" << setw(15) << "Soni" << endl;
}
void qidirish(mySpace::Kalit k[], int n){
cout << "Qidirayotgan kalit nomini kiriting : ";
string s; cin >> s; bool bor=false;
cha();
for(int i=0; i
if(k[i].nomi.compare(s)==0){
int b=i;
bor=true;
cout << setw(5) << ++b;
k[i].get_malumot();
}
}
if(!bor){
cout << "Bunday nomdagi kalit topilmadi.\n";
}
}
void saralash(mySpace::Kalit k[], int n){
cout << "Nomi bo`yicha saralash holati.\n\n";
mySpace::Kalit templ;
for(int i=0; i
for(int j=0; j
if(k[i].nomi.compare(k[j].nomi)<0){
templ=k[i];
k[i]=k[j];
k[j]=templ;
}
}
}
cha();
int b=1;
for(int i=0; i
cout << setw(5) << b;
k[i].get_malumot();
b++;
}
}
void ozgartirish(mySpace::Kalit k[], int n){
int a;
cout << "Qaysi kalit malumotlarini o`zgartirishni hohlaysiz : ";
string s; cin >> s; bool bor=false;
for(int i=0; i
if(k[i].nomi.compare(s)==0){
bor=true;
cout << "1 : Harid qilish\n" << "2 : Sotish\n";
cin >> a;
switch(a){
case 1 :
{
int b=i;
k[i].soni++;
cha();
cout << setw(5) << ++b;
k[i].get_malumot();
cout << endl;
break;
}
case 2 : {
if(k[i].soni==0){
cout << "Bunday kalitdan qolmagan.\n";
} else{
int b=i;
k[i].soni--;
cha();
cout << setw(5) << ++b;
k[i].get_malumot();
cout << endl;
}
break;
}
default : cout << "Bunday amalni bajarib bo`lmaydi.\n";
}
}
}
if(!bor){
cout << "Bunday nomdagi kalit topilmadi.\n";
}
}
};
class Qulif{
string nomi, turi;
int yili, soni;
public:
friend void qidirish(mySpace::Qulif q[], int n);
friend void saralash(mySpace::Qulif q[], int n);
friend void ozgartirish(mySpace::Qulif q[], int n);
void set_malumot(string N, string T, int Y){
nomi=N;
turi=T;
yili=Y;
srand(time(0));
soni=rand()%20+5;
}
void get_malumot(){
cout << setw(15) << nomi << setw(15) << turi << setw(15) << yili << setw(15) << soni << endl;
}
void cha(){
cout << setw(5) << "N" << setw(15) << "Nomi" << setw(15) << "Turi" << setw(15) << "Yili" << setw(15) << "Soni" << endl;
}
void qidirish(mySpace::Qulif q[], int n){
cout << "Qidirayotgan qulif nomini kiriting : \n";
string s; cin >> s; bool bor=false;
for(int i=0; i
if(q[i].nomi.compare(s)==0){
bor=true;
int b=i;
cha();
cout << setw(5) << ++b;
q[i].get_malumot();
}
}
if(!bor){
cout << "Bunday nomdagi qulif topilmadi.\n";
}
}
void saralash(mySpace::Qulif q[], int n){
cout << "Nomi bo`yicha saralash holati.\n\n";
mySpace::Qulif templ;
for(int i=0; i
for(int j=0; j
if(q[i].nomi.compare(q[j].nomi)<0){
templ=q[i];
q[i]=q[j];
q[j]=templ;
}
}
}
cha();\
int b=1;
for(int i=0; i
cout << setw(5) << b;
q[i].get_malumot();
b++;
}
}
void ozgartirish(mySpace::Qulif q[], int n){
int a;
cout << "Qaysi qulif malumotlarini o`zgartirishni hohlaysiz.\n";
string s; cin >> s; bool bor=false;
for(int i=0; i
if(q[i].nomi.compare(s)==0){
bor=true;
cout << "1 : Harid qilish\n" << "2 : Sotish\n";
cin >> a;
switch(a){
case 1 : {
cha();
int b=i;
cout << setw(5) << ++b;
q[i].get_malumot();
break;
}
case 2 : {
if(q[i].soni==0){
cout << "Bu qulifdan boshqa qolmagan.\n";
} else {
int b=i;
cha();
cout << setw(5) << ++b;
q[i].get_malumot();
}
break;
}
default : cout << "Bunday amalni bajarib bo`lmaydi.\n";
}
}
}
if(!bor){
cout << "Bunday nomdagi qulif topilmadi.\n";
}
}
};
}
using namespace mySpace;
int main(){
string nomi[]={"cassl", "franch", "china", "coss"};
string turi[]={"po`lat", "temir", "qattiq metal", "sensrli"};
int yili[]={2005, 2003, 20010, 2018};
cout << setw(45) << "<>\n\n";
Kalit k[100];
Kalit ka;
ka.cha();
int t=1;
for(int i=0; i<4; i++){
k[i].set_malumot(nomi[i], turi[i], yili[i]);
cout << setw(5) << t;
k[i].get_malumot();
t++;
}
cout << endl;
ka.qidirish(k,4);
ka.saralash(k,4);
ka.ozgartirish(k,4);
cout << setw(45) << "<>\n\n";
Qulif q[100];
Qulif qa;
qa.cha();
int b=1;
for(int i=0; i<4; i++){
q[i].set_malumot(nomi[i], turi[i], yili[i]);
cout << setw(5) << b;
q[i].get_malumot();
b++;
}
cout << endl;
qa.qidirish(q,4);
qa.saralash(q,4);
qa.ozgartirish(q,4);
}
Classlar o’rtasida vorislikni tashkil qilish. Overriding va overloading bo’ladigan funksiyalarni yaratish
Ishning maqsadi: Merosxo’rlik imkoniyatlaridan foydalanib dasturlash ko’nikmalarini shakllantirish.
Topshiriq: 1 - jadvaldagi berilgan vazifalar uchun ajdod class va voris class larni yarating. Voris class da quyidagi funksiyalarni yozing:
Konstruktor.
Uchinchi ustundagi qo’yilgan vazifani bajaruvchi funksiya.
Avlod obyektlari haqida ma’lumotlarini chop qiluvchi funksiya.
Ajdod va voris class larning obyektlari haqida ma’lumotlarni kiritish va chiqarishni bajaruvchi friend funksiyalar va ularning shablonini yarating.
13
|
Kitob:
nomi, sahifalar soni, narxi
|
Chop etilgan kitob:
nomi, sahifalar soni, narxi, kitob muallifi, nashriyotga berilgan sanasi
|
Bugungi sana bilan kitobni nashriyotga berilganiga necha kun bo’lgan.
|
#include
#include
#include
#include
#include
using namespace std;
namespace mySpace
{
class Kitob
{
public:
string nomi;
int sah_soni;
int narxi;
};
class Chop_Et_Kitob : public Kitob{
public:
string mualifi;
int chop_yilli;
int chop_oyi;
int chop_kuni;
friend void disply(mySpace::Chop_Et_Kitob k[], int n);
friend void hisobla(mySpace::Chop_Et_Kitob k[],int n);
void set_malumot(string N, int S, int Na, string M, int Y,int O, int K){
nomi=N;
sah_soni=S;
narxi=Na;
mualifi=M;
chop_yilli=Y;
chop_oyi=O;
chop_kuni=K;
}
void ch(){
cout << setw(5) << "N" << setw(20) << "Nomi" << setw(20) << "Sahifalari soni" << setw(20) << "Narxi" << setw(20) << "Mualifi" << setw(27) << "chopetilgan sanasi";
}
void get_malumot()
{
cout << setw(20) << nomi << setw(20) << sah_soni << setw(20) << narxi << setw(20) << mualifi << setw(20) << chop_kuni << "." << chop_oyi << "." << chop_yilli;
}
void disply(mySpace::Chop_Et_Kitob k[], int n)
{
mySpace::Chop_Et_Kitob ka;
ka.ch(); cout << endl;
for(int i=0; i
int b=i;
cout << setw(5) << ++b;
k[i].get_malumot();
cout << endl;
}
}
void hisobla(mySpace::Chop_Et_Kitob k[],int n){
mySpace::Chop_Et_Kitob ka;
int x, y, z, day[n];
cout << "Bugingi sanani kiriting : ";
cin >> x >> y >> z;
ka.ch();
cout << setw(25) << "Kun" << endl;
for(int i=0; i
for(int j=k[i].chop_yilli; j
if(j%4==0){
day[i]+=366;
} else day[i]+=365;
}
}
for(int i=0; i
for(int j=k[i].chop_oyi; j
if(j%12==(1||3||5||7||10||12)){
day[i]+=31;
}
else if(j%12==(4||6||8||9||11)){
day[i]+=30;
} else if(j%12==2&&k[i].chop_yilli%4==0){
day[i]+=29;
} else day[i]+=28;
}
}
for(int i=0; i
day[i]+=k[i].chop_kuni;
day[i]-=x;
}
int b=1;
for(int i=0; i
cout << setw(5) << b;
k[i].get_malumot(); cout << setw(25) << day[i] << endl;
b++;
}
}
};
}
using namespace mySpace;
int main(){
string nomi[]={"Xamsa","Kimyo","Milliy G`oya","Geografiya","Ona tili"};
int sah_soni[]={32456,204,125,174,141};
int narxi[]={24500,23300,23100,12200,17600,};
string mualifi[]={"Navoiy", "Asqarov", "Nishonova", "Musayev", "Qodirov"};
int chop_yilli[]={2000,2004,2001, 2005, 2008};
int chop_oyi[]={5,2,6,9,7};
int chop_kuni[]={12,1,10,19,18};
Chop_Et_Kitob ch[100],cho;
cout << setw(55) <<"<<>>" << "\n\n";
for(int i=0; i<5; i++){
ch[i].set_malumot(nomi[i],sah_soni[i],narxi[i],mualifi[i],chop_yilli[i],chop_oyi[i],chop_kuni[i]);
}
cho.disply(ch,5);
cho.hisobla(ch,5);
}
OYD ning merosxo’rlik va polimorfizm xususiyatlaridan foydalangan holda vazifaga muvofiq dasturlar yaratish
Ishning maqsadi: Polimorfizm(overload va override) metodlarni qayta e’lon qilish va qayta yuklash orqali dasturlash ko’nikmalarini shakllantirish.
Topshiriq: OYD ning merosxo’rlik va polimorfizm xususiyatlaridan foydalangan holda vazifaga muvofiq dasturni yozing.
Barcha classlarda class obyektlari yaratilishi kerak bo'lgan konstruktorlarni yozing.
Yaratiladigan obyektlarning parametrlari klaviaturadan (talaba xohlaganidek) kiritiladi va parametr sifatida konstruktorga uzatiladi.
Obyektlar haqida barcha ma'lumotlar chiqarilsin.
Virtual va do’st funksiyalar hamda do’st class lar yaratilsin.
13
|
“Po’lat plita” classini yarating. Maydonlari: Plita qatlamining qalinligi (mm) va po'lat zichligi (kg / m3) bilan ifodalanadi. Plitalar “Kvadrat” (shakli, kengligi va uzunligi bir xil beriladi, mm da), “To’rtburchak” (shakli, kengligi va uzunligi beriladi, mm da) va “Uchburchak” (shakli, to’g’ri burchakli uchburchak ko’rinishida ikkita kateti beriladi, mm da). Har bir plita uchun “Yuzasi” nomli virtual metod yozing. Shuningdek, “Og’irlik” metodini yozing, unda plitani og’irligi plita yuzasini * qalinligi * zichligi formula orqali hisoblanadi. “Ma’lumot” nomli virtual metod plitalar haqida ma’lumotni jadval ko’rinishida chop qiling.
Asosiy dasturda 15 ta po'lat plitani yaratib, 5 tasi kvadrat, 7 tasi burchakli va 3 ta uchburchak shaklli bo’lib o’lchamlari tasodifiy o'lchamlar orqali yaratasiz. Plitalar haqida ma'lumotni jadval ko’rinishida chop eting.
| Shakli | Kengligi | Uzunligi | Yuzasi | Og’irligi |
| Uchburchak | 4 | 3 | 6 | 9kg/m3 |
|
#include
#include
#include
#include
#include
using namespace std;
namespace mySpace
{
class Plita{
public:
int qalin=40;
int zichligi=13600;
void set_malumot(int Q, int Z){
qalin=Q;
zichligi=Z;
}
int kvadrat(int a){
return a;
}
int tortburchak(int a, int b){
return a*b;
}
int uchburchak(int a, int b){
return (float)a*b/2;
}
int squerK(int a){
int S=1;
mySpace::Plita p;
S=p.kvadrat(a)*p.kvadrat(a);
return S;
}
int squerT(int a, int b){
int S=1;
mySpace::Plita p;
S=p.tortburchak(a,b);
return S;
}
float squerU(int a, int b){
int S=1;
mySpace::Plita p;
S=p.uchburchak(a,b);
return S;
}
int massaK(int a){
int M=1;
mySpace::Plita p;
M=p.squerK(a)*qalin*zichligi;
return M;
}
int massaT(int a, int b){
int M=1;
mySpace::Plita p;
M=squerT(a,b)*p.qalin*p.zichligi;
return M;
}
int massaU(int a, int b){
int M=1;
mySpace::Plita p;
M=squerU(a,b)*p.qalin*p.zichligi;
return M;
}
void ch(){
cout << setw(5) << "N" << setw(15) << "Shakl" << setw(15) << "Uzunligi" << setw(15) << "Kengligi" << setw(15) << "Yuzasi" << setw(15) << "Massasi" << endl;
}
};
}
using namespace mySpace;
int main(){
mySpace::Plita p[100],pi;
int a;
pi.ch();
srand(time(0));
for(int i=0; i<5;i++){
int j=i;
a=rand()%25+30;
cout << setw(5) << ++j << setw(15) << "Kvadrat" << setw(15) << a << setw(15) << a << setw(15) << p[i].squerK(a) << setw(15) << p[i].massaK(a) << endl;
}
cout << "________________________________________" << endl;
int x;
int y;
pi.ch();
for(int i=0; i<7; i++){
int j=i;
x=rand()%5+30;
y=rand()%5+25;
cout << setw(5) << ++j << setw(15) << "To`rtburchak" << setw(15) << x << setw(15) << y << setw(15) << p[i].squerT(x,y) << setw(15) << p[i].massaT(x,y) << endl;
}
cout << "________________________________________" << endl;
int b;
int c;
pi.ch();
for(int i=0; i<3; i++){
int j=i;
b=rand()%15+5;
c=rand()%5+15;
cout << setw(5) << ++j << setw(15) << "Uchburchak" << setw(15) << b << setw(15) << c << setw(15) << p[i].squerU(b,c) << setw(15) << p[i].massaU(b,c) << endl;
}
}