Advertisement
Proff_Ust

Lab4_new

Dec 25th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <clocale>
  4.  
  5. using namespace std;
  6.  
  7. class Tbody
  8. {
  9. protected:
  10.     float R;
  11. public:
  12.     Tbody()
  13.     {
  14.         this->R = 0;
  15.     }
  16.  
  17.     Tbody(float newR)
  18.     {
  19.         this->R = newR;
  20.     }
  21.     virtual float volume(){}
  22.     virtual float area(){}
  23. };
  24.  
  25. class TBall: Tbody
  26. {
  27. public:
  28.     TBall():Tbody(){}
  29.     TBall(float newR):Tbody(newR){}
  30.  
  31.     virtual float volume()
  32.     {
  33.         return (4*3.1415926*pow(this->R,3))/3;
  34.     }
  35.  
  36.     virtual float area()
  37.     {
  38.         return (4*3.1415926*pow(this->R,2));
  39.     }
  40.     friend ostream &operator<<(ostream &stream, TBall* obj);
  41.     friend istream &operator>>(istream &stream, TBall** obj);
  42. };
  43.  
  44. ostream &operator<<(ostream &stream, TBall* obj)
  45. {
  46.     cout<<"Радиус сферы равен "<<obj->R<<endl;
  47.     return stream;
  48. }
  49.  
  50. istream &operator>>(istream &stream, TBall** obj)
  51. {
  52.     float newR;
  53.     cout<<"Введите радиус сферы"<<endl;
  54.     cout<<"R=";
  55.     cin>>newR;
  56.     *obj = new TBall(newR);
  57.     return stream;
  58. }
  59.  
  60. class TCone: Tbody
  61. {
  62. private:
  63.     float H;
  64. public:
  65.     TCone()
  66.     {
  67.         this->H = 0;
  68.     }
  69.  
  70.     TCone(float newR, float newH):Tbody(newR)
  71.     {
  72.         this->H = newH;
  73.     }
  74.  
  75.     virtual float volume()
  76.     {
  77.         return (3.1415926*pow(this->R,2)*this->H)/3;
  78.     }
  79.  
  80.     virtual float area()
  81.     {
  82.         return 3.1415926*this->R*(this->R+sqrt(pow(this->R,2)+pow(this->H,2)));
  83.     }
  84.     friend ostream &operator<<(ostream &stream, TCone* obj);
  85.     friend istream &operator>>(istream &stream, TCone** obj);
  86. };
  87.  
  88. ostream &operator<<(ostream &stream, TCone* obj)
  89. {
  90.     cout<<"Радиус основания конуса равен "<<obj->R<<endl;
  91.     cout<<"Высота конуса равна "<<obj->H<<endl;
  92.     return stream;
  93. }
  94.  
  95. istream &operator>>(istream &stream, TCone** obj)
  96. {
  97.     float newR, newH;
  98.     cout<<"Введите радиус основания конуса"<<endl;
  99.     cout<<"R=";
  100.     cin>>newR;
  101.     cout<<"Введите высоту конуса"<<endl;
  102.     cout<<"H=";
  103.     cin>>newH;
  104.     *obj = new TCone(newR, newH);
  105.     return stream;
  106. }
  107.  
  108. int main()
  109. {
  110.     setlocale(LC_ALL,"RU");
  111.     TBall* ball = new TBall(10);
  112.     TCone* cone = new TCone(5,6);
  113.  
  114.     cout<<"Площадь поверщности сферы "<<ball->area()<<endl;
  115.     cout<<"Объем шара "<<ball->volume()<<endl;
  116.  
  117.     cout<<"Площадь поверхности конуса "<<cone->area()<<endl;
  118.     cout<<"Объем конуса "<<cone->volume()<<endl;
  119.     system("pause");
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement