Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #define N 3
- using namespace std;
- class Figure {
- protected:
- double r;
- double v;
- public:
- void show() {
- cout << "Info about figure:" << endl;
- calculate();
- info();
- }
- virtual void info() = 0;
- virtual double calculate() = 0;
- };
- class Sphere : public Figure {
- public:
- Sphere() {r = v = 0;}
- Sphere(double _r) {r = _r;}
- void info() {
- cout << "Info about sphere:" << endl;
- cout << " r = " << r << endl << endl;
- }
- double calculate() {
- v = (4. / 3) * M_PI * pow(r, 3);
- cout << "Volume of sphere = " << v << endl << endl;
- return v;
- }
- friend istream& operator >> (istream& is, Sphere& x);
- bool operator <= (const Sphere& x) {return r <= x.r;}
- };
- class Cylinder : public Figure {
- private:
- double h;
- public:
- Cylinder() {r = h = v = 0;}
- Cylinder(double _r, double _h) {
- r = _r;
- h = _h;
- }
- void info() {
- cout << "Info about cylinder:" << endl;
- cout << " r = " << r << endl;
- cout << " h = " << h << endl << endl;
- }
- double calculate() {
- v = h * M_PI * pow(r, 2);
- cout << "Volume of сylinder = " << v << endl << endl;
- return v;
- }
- friend istream& operator >> (istream& is, Cylinder& x);
- bool operator <= (const Cylinder& x) {return r <= x.r && h <= x.h;}
- };
- istream& operator >> (istream& is, Sphere& x) {
- cout << "Enter info about sphere:" << endl;
- cout << " r = "; is >> x.r;
- cout << endl;
- return is;
- }
- istream& operator >> (istream& is, Cylinder& x) {
- cout << "Enter info about cylinder:" << endl;
- cout << " r = "; is >> x.r;
- cout << " h = "; is >> x.h;
- cout << endl;
- return is;
- }
- int main() {
- string res;
- Sphere spheres[N];
- Cylinder cylinders[N];
- cout << "Enter " << N << " spheres:" << endl << endl;
- for (int i = 0; i < N; i++) {
- cout << "Enter info about " << i + 1 << " sphere:" << endl;
- cin >> spheres[i];
- }
- cout << "Comparing:" << endl << endl;
- for (int i = 1; i < N; i++) {
- res = "false";
- cout << i << " sphere <= " << i + 1 << " sphere" << endl;
- if (spheres[i - 1] <= spheres[i]) res = "true";
- cout << "result: " << res << endl;
- cout << endl;
- }
- cout << endl;
- cout << "Enter " << N << " cylinders:" << endl << endl;
- for (int i = 0; i < N; i++) {
- cout << "Enter info about " << i + 1 << " cylinder:" << endl;
- cin >> cylinders[i];
- }
- cout << "Comparing:" << endl << endl;
- for (int i = 1; i < N; i++) {
- res = "false";
- cout << i << " cylinder <= " << i + 1 << " cylinder" << endl;
- if (cylinders[i - 1] <= cylinders[i]) res = "true";
- cout << "result: " << res << endl;
- cout << endl;
- }
- cout << endl;
- cout << "Pointer using base class:" << endl << endl;
- Figure* figr;
- figr = new Sphere(3);
- figr->show();
- figr = new Cylinder(3, 2);
- figr->show();
- delete figr;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment