15-16-LABORATORIYA MASHG’ULOTI. SINFNING VIRTUAL METODLARI VA ULARNI ISHLATISH MEXANIZMLARI. ABSTRAKT SINFLAR, ULARNING MO’LJALLANISHI VA XUSUSIYATLARI
Pul summalari bilan ishlash uchun Currency (valyuta) abstrakt sinfini yarating. Rublga konvertatsiya qilishning virtual funktsiyalarini aniqlang va chop eting. Dollar va Evro sinflarini o'zlarining konvertatsiya va chop etish funktsiyalari bilan amalga oshiring.
#include
using namespace std;
class Currency{
public:
float sum;
Currency(float summ)
{
sum = summ;
}
float get_sum()
{
return sum;
}
virtual const string get_value()
{
return "Rubl";
}
virtual const float conversation(float summ)
{
return summ / 150;
}};
class Dollar: public Currency{
public:
Dollar(float sum):Currency(sum)
{
}
virtual const string get_value()
{
return "Dollar";
}
virtual const float conversation(float summ)
{
return summ / 1100;
}}
class Euro: public Currency{
public:
Euro(float sum):Currency(sum)
{
}
virtual const string get_value()
{
return "Euro";
}
virtual const float conversation(float summ)
{
return sum / 1150;
}
};
void Conversation(Currency ¤cy)
{
cout << currency.get_sum() << " = " << currency.conversation(currency.sum) << " " << currency.get_value() << endl;
}
int main()
{
Currency c(200000);
Dollar dollar1(200000);
Euro euro1(200000);
Conversation(c);
Conversation(dollar1);
Conversation(euro1);
return 0;
}
Virtual metodlar - arifmetik amallar bilan Number abstrakt ajdod sinfini yarating. Avlod sinflar Integer va Real sinflaridir.
#include
using namespace std;
class Number{
public:
int number1;
int number2;
Number(int num1, int num2)
{
number1 = num1;
number2 = num2;
}
get_number1()
{
return number1;
}
get_number2()
{
return number2;
}
virtual const int add(int num1, int num2) = 0;
virtual const int sub(int num1, int num2) = 0;
virtual const int dev(int num1, int num2) = 0;
virtual const int inc(int num1, int num2) = 0;
};
class Integer: public Number{
public:
int number1;
int number2;
Integer(int num1, int num2): Number(number1, number2)
{
}
virtual const int add(int num1, int num2)
{
return num1 + num2;
}
virtual const int sub(int num1, int num2)
{
return num1 - num2;
}
virtual const int dev(int num1, int num2)
{
return num1 / num2;
}
virtual const int inc(int num1, int num2)
{
return num1 * num2;
}
};
class Real: public Number{
public:
float number1f;
float number2f;
Real(float num1, float num2):Number(number1,number2)
{
number1f = num1;
number2f = num2;
}
virtual const float add(float num1, float num2)
{
return num1 + num2;
}
virtual const float sub(float num1, float num2)
{
return num1 - num2;
}
virtual const float dev(float num1, float num2)
{
return num1 / num2;
}
virtual const float inc(float num1, float num2)
{
return num1 * num2;
}
};
void add(Number &number)
{
cout << number.add(number.number1, number.number2) << endl;
}
void sub(Number &number)
{
cout << number.sub(number.number1, number.number2) << endl;
}
void dev(Number &number)
{
cout << number.dev(number.number1, number.number2) << endl;
}
void inc(Number &number)
{
cout << number.inc(number.number1, number.number2) << endl;
}
int main()
{
Integer integer1(18, 19);
sub(integer1);
cout << endl;
add(integer1);
return 0;
}
17-18-LABORATORIYA MASHG’ULOTI. PARAMETRLANGAN SINFLARGA KIRISH. PARAMETRLANGAN SINFLAR VA METODLAR, ULARNING XUSUSIYATLARI. PARAMETRIZATSIYA VA VORISLIK TAMOYILLARINI HAMKORLIKDA QO’LLASH
Student (Talaba) nomli sinf hosil qiling. Talabaning id raqami, baholari, kursi uning maydonlari bo’lsin. GPAni hisoblash metodini aniqlang. Uning kursdan - kursga o’tish/o’tmasligini belgilaydigan metod hosil qiling. Sinf shablonidan foydalaning.
#include
using namespace std;
template
class Student{
public:
I id_num ;
I kurs ;
I ball;
I GPA ;
I kredit ;
};
int main(){
Student obj;
int m = 0 , S =0 ;
for(int i=0 ; i<8; i++){
cout << i+1 <<"- fandan nechi baho oldingiz : " ; cin >> obj.ball;
cout << "Fan nechi kredit : " ; cin >> obj.kredit ;
m+=obj.kredit*obj.ball;
S+=obj.kredit ;
}
obj.GPA = m / S ;
cout << obj.GPA ;
return 0;
}
Do'stlaringiz bilan baham: |