Hellko

asdasd

May 14th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>         //rand(), srand()
  3. #include <time.h>           //time() for srand()
  4. #include <math.h>           //sqrt()
  5. #include <sstream>          //for convert double to string. std::ostringstream
  6. #define PI 3.1415926
  7. using namespace std;
  8.  
  9. class figure {
  10. public:
  11.     virtual double V()=0;
  12.     virtual double S()=0;
  13.     virtual void scale(double)=0;
  14.     friend ostream& operator<<(ostream&,const figure&);
  15. protected:
  16.     virtual string show() const = 0;
  17. };
  18. ostream& operator<<(ostream& out, const figure&A) {
  19.     out<<A.show();
  20.     return out;
  21. }
  22.  
  23. class ball: public figure {
  24. private:
  25.     double r;
  26.     string show() const {std::ostringstream ost; ost<<r;  return "object Ball:     r="+ost.str();}
  27. public:
  28.     ball(double R=1.0): r(R) {}
  29.     double V() {return 4*PI/3*r*r*r;}
  30.     double S() {return 4*PI*r*r;}
  31.     void scale(double k) {r*=k;}
  32. };
  33.  
  34. class cone: public figure {
  35. private:
  36.     double r,h;
  37.     string show() const {std::ostringstream ost1,ost2; ost1<<r; ost2<<h;  return "object Cone:     r="+ost1.str()+"\th="+ost2.str();}
  38. public:
  39.     cone(double R=1.0, double H=1.0): r(R), h(H) {}
  40.     double V() {return PI*r*r*h/3;}
  41.     double S() {return PI*r*(r+sqrt(r*r+h*h));}
  42.     void scale(double k) {r*=k; h*=k;}
  43. };
  44.  
  45. class cylinder: public figure {
  46. private:
  47.     double r,h;
  48.     string show() const {std::ostringstream ost1,ost2; ost1<<r; ost2<<h;  return "object Cylinder: r="+ost1.str()+"\th="+ost2.str();}
  49. public:
  50.     cylinder(double R=1.0, double H=1.0): r(R), h(H) {}
  51.     double V() {return PI*r*r*h;}
  52.     double S() {return 2*PI*r*(r+h);}
  53.     void scale(double k) {r*=k; h*=k;}
  54. };
  55.  
  56. figure* createObject(int x, double r=1.0, double h=1.0) {
  57.     figure* p = NULL;
  58.     switch(x) {
  59.     case 0: p = new ball(r); break;
  60.     case 1: p = new cone(r,h); break;
  61.     case 2: p = new cylinder(r,h); break;
  62.     }
  63.     return p;
  64. }
  65.  
  66. int main() {
  67.     srand(time(NULL));
  68.     const int N=5;
  69.     figure *arr[N];
  70.     cout<<"Create objects:"<<endl;
  71.     for(int i=0; i<N; i++) {
  72.         arr[i]=createObject(rand()%3,(rand()%9999)/100.0,(rand()%9999)/100.0);
  73.         cout<<*arr[i]<<endl<<"Square: "<<arr[i]->S()<<"\tVolume: "<<arr[i]->V()<<endl<<endl;
  74.     }
  75.     cout<<endl<<"After scaling:"<<endl<<endl<<endl;
  76.     for(int i=0; i<N; i++) {
  77.         arr[i]->scale(0.1);
  78.         cout<<*arr[i]<<endl<<"Square: "<<arr[i]->S()<<"\tVolume: "<<arr[i]->V()<<endl<<endl;
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment