#include
#include
#include
using namespace std;
class figure {
protected:
double x, y;
public:
figure(int i, double j) {
x = i;
y = j;
}
virtual double yuza() = 0;
};
class uchburchak : public figure {
public:
uchburchak(double i, double j) : figure(i, j) {}
double yuza() {
return x * 0.5 * y;
}
};
class turtburchak : public figure {
public:
turtburchak(double i, double j) : figure(i, j) {}
double yuza() {
return x * y;
}
};
class doira : public figure {
public:
doira(double i, double j = 0) : figure(i, j) {}
double yuza() {
return 3.14 * x * x;
}
};
figure* factory() {
switch (rand() % 3) {
case 0:
return new doira(rand() % 15);
case 1:
return new uchburchak(rand() % 15, rand() % 15);
case 2:
return new turtburchak(rand() % 15, rand() % 15);
}
return 0;
}
int main() {
figure* p;
int i;
int t = 0, r = 0, c = 0;
for (i = 0; i < 15; i++) {
p = factory();
cout << "Obyektning tipi: " << typeid(*p).name() << ". ";
if (typeid(*p) == typeid(uchburchak))
t++;
if (typeid(*p) == typeid(turtburchak))
r++;
if (typeid(*p) == typeid(doira))
c++;
cout << "S= " << p->yuza() << endl;
}
cout << endl;
cout << "Quyidagi obyektlar hosil qilindi:\n";
cout << "Uchburchaklar: " << t << endl;
cout << "To'rtburchaklar: " << r << endl;
cout << "Doiralar: " << c << endl;
system("pause");
return 0;
}
|
1. figure sinfdagi xatolikni aniqlang.
Dasturda tuzatish kiriting.
Javobi: figure sinfi konstruktorida i va j parametrlarini o'zgaruvchilarni x va y ga tenglash uchun ishlatilgan. Lekin x va y o'zgaruvchilarini k va l deb nomlangan o'zgaruvchilar bilan bir hilayda o'zgartirishga urilgan. To'g'rilash uchun kodni quyida ko'rsatilgan holatda o'zgartirishingiz kerak:
3. Dasturda obyektlarni yaratish sonini dinamik almashtirish uchun o‘zgartirish kiriting va fragment aniq yozing.
|