1-masala: Car (Mashina) va Truck(Yuk mashina), LigthCar (Yengil mashina) sinflari hosil qiling. Avlod sinflar ajdod sinfidan meros olgan olgan holda qo’shimcha metodlar hosil qiling.
Kod qismi:
#include
using namespace std;
class Car {
private:
string model;
public:
string color;
void set_model( string x){
model = x;
}
string get_model() {
return model;
}
void display1 (){
cout << "Mashinaning rangi " << color << endl;
cout << "Mashinaning modeli " << get_model() << endl << endl << endl;
}
};
class LightCar: public Car{
public:
int orindiqlar_soni;
void display(){
cout << "Ferrarining rangi " << color << endl;
cout << "Ferrarining o'rindiqlari soni " << orindiqlar_soni << " ta" << endl;
cout << "Ferrarining modeli " << get_model() << endl << endl << endl;
}
};
class Truck: public LightCar{
public:
int yuk_sigimi;
void display3(){
cout << "mercedesning o'rindiqlari soni " << orindiqlar_soni << " ta" << endl;
cout << "mercedesning rangi " << color << endl;
cout << "mercedesning modeli " << get_model() << endl;
cout << "mercedesning yuk sig'mi " << yuk_sigimi << " tonnagacha";
}
};
int main () {
Car obj;
obj.set_model("Ford");
obj.color = "yashil";
obj.display1();
LightCar obj2;
obj2.color = "qora";
obj2.set_model("Ferrari");
obj2.orindiqlar_soni = 2;
obj2.display();
Truck obj3;
obj3.color = "oq";
obj3.orindiqlar_soni = 3;
obj3.set_model("mercedes");
obj3.yuk_sigimi = 25;
obj3.display3();
return 0;
}
Natijasi:
#include //92 masala
using namespace std;
class Circle{
public:
double area(double r){
return M_PI*r*r;
}
double diometr(double r){
return 2*r;
}
};
int main()
{
ofstream file("input.txt");
int n;
cin >> n;
Circle obj;
for(int i=0;i
double r;
cin >> r;
file << obj.area(r) << " " << obj.diometr(r) << endl;
}
return 0;
}
#include //44-masala
using namespace std;
class Triangle{
public:
virtual int area(int a,int b,int c){
int p = (a + b + c)/2;
return sqrt(p*(p-a)*(p-b)*(p-c));
}
virtual int peremetr(int a,int b,int c){
return (a + b + c);
}
};
class Uchburchak:virtual public Triangle {
public:
int a,b,c;
void print(){
cout << Triangle::area(a,b,c) << " " << Triangle::peremetr(a,b,c);
}
};
int main()
{
Uchburchak obj;
obj.a = 3;
obj.b = 4;
obj.c = 5;
obj.print();
return 0;
}
#include //76-masala
using namespace std;
class Line{
public:
double solve(int x1,int y1,int x2,int y2){
return sqrt(pow(x1 - x2,2)+pow(y1-y2,2));
}
};
int main()
{
Line obj;
try{
double o = obj.solve(1,2,1,2);
if(o == 0){
throw o;
}
cout << o << endl;
}
catch(double o){
cout << "Ustma ust tushadi.";
}
return 0;
}
#include //56-masala
using namespace std;
void Parallelogram(int a,int b,int alfa,int beta){
int d1 = sqrt(a*a+b*b-2*a*b*cos(alfa*M_PI/180));
int d2 = sqrt(a*a+b*b-2*a*b*cos(beta*M_PI/180));
int s = a * b * sin(alfa*M_PI/180);
int p = (a + b)*2;
cout << d1 << " " << d2 << endl;
cout << s << " " << p << endl;
}
int main()
{
int a,b,alfa,beta;
cin >> a >> b >> alfa >> beta;
Parallelogram(a,b,alfa,beta);
return 0;
}
Do'stlaringiz bilan baham: |