1.6
Copied!
#include "stdafx.h"
#include <iostream>
#include <typeinfo>
#include <cstdlib>
using namespace std;
template <class T>
class figure {
protected:
T x, y;
public:
figure(T i, T j) {
x = i;
y = j;
}
virtual T area() = 0;
};
template <class T>
class triangle : public figure<T> {
public:
triangle(T i, T j) : figure<T>(i, j) {}
T area() {
return x * 0.5 * y;
}
};
template <class T>
class rectangle : public figure<T> {
public:
rectangle(T i, T j) : figure<T>(i, j) {}
T area() {
return x * y;
}
};
template <class T>
class circle : public figure<T> {
public:
circle(T i, T j = 0) : figure<T>(i, j) {}
T area() {
return 3.14 * x * x;
}
};
figure<double>* generator() {
switch (rand() % 3) {
case 0: return new circle<double>(rand() % 28);
case 1: return new triangle<double>(rand() % 28, rand() % 28);
case 2: return new rectangle<double>(rand() % 28, rand() % 28);
}
return 0;
}
int main() {
figure<double>* p;
int i;
int t = 0, c = 0, r = 0;
for (i = 0; i < 15; i ) {
p = generator();
cout << "Obyekt tipi: " << typeid(*p).name();
cout << ". ";
if (typeid(*p) == typeid(triangle<double>)) t ;
if (typeid(*p) == typeid(rectangle<double>)) r ;
if (typeid(*p) == typeid(circle<double>)) c ;
cout << " S= " << p->area() << endl;
}
cout << endl;
cout << "Obyektlarni sanash:\n";
cout << " uchburshaklar: " << t << endl;
cout << " toʻrtburchaklar: " << r << endl;
cout << " doiralar: " << c << endl;
system("pause");
return 0;
}
Run on cpp.sh
Copied!