Advertisement
daniv1

Untitled

May 21st, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.97 KB | None | 0 0
  1. // ConsoleApplication14.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <string>
  7. #define PI  3.1415
  8.  
  9. enum Color { RED, GREEN, BLUE, YELLOW, WHITE, BLACK };
  10. enum Gas { N2O2, H2, He, Ne };
  11.  
  12.  
  13. class Sphere // Базовий клас Sphere
  14.  
  15. {
  16. public:
  17.     Sphere();
  18.     Sphere(double Radius);
  19.     void setRadius(double Radius);
  20.     double Radius();
  21.     double Diameter();
  22.     double Circumference();
  23.     double Area();
  24.     double Volume();
  25.     virtual void displayStatistics();
  26. private:
  27.     double mRadius;
  28. };
  29.  
  30. class Ball :public Sphere// Похідний клас Ball від Sphere
  31. {
  32. public:
  33.     Ball();
  34.     Ball(double Radius, std::string Name, Color color);
  35.     void setName(std::string Name);
  36.     std::string Name();
  37.     void setColor(Color color);
  38.     Color color();
  39.     std::string ColorName();
  40.     void resetBall(double Radius, std::string Name, Color color);
  41.     virtual void displayStatistics() override;
  42. private:
  43.     std::string mName;
  44.     Color mC;
  45. };
  46.  
  47. class Ballon : public Ball // Похідний клас Ballon від Ball
  48. {
  49. public:
  50.     Ballon();
  51.     Ballon(double Radius, std::string Name, Color color, Gas gas);
  52.     void setGas(Gas gas);
  53.     Gas gas();
  54.     std::string GasName();
  55.     void resetBallon(double Radius, std::string Name, Color color, Gas gas);
  56.     void displayStatistics();
  57. private:
  58.     Gas mG;
  59. };
  60.  
  61. class WeightBall : public Ball // Похідний клас WeightBall від Ball
  62. {
  63. public:
  64.     WeightBall();
  65.     WeightBall(double Radius, std::string Name, Color color, double Density);
  66.     void setDensity(double Density);
  67.     double Density();
  68.     virtual double Weight();
  69.     void resetWeightBall(double Radius, std::string Name, Color color, double Density);
  70.     virtual void displayStatistics();
  71. private:
  72.     double mDensity;
  73. };
  74.  
  75. class WeightBallon : public WeightBall, public Ballon // Похідний клас WeightBallon від Ballon
  76. {
  77. public:
  78.     WeightBallon();
  79.     WeightBallon(double Radius, std::string Name, Color color, Gas gas);
  80.     virtual  double Weight();
  81.     void resetWeightBallon(double Radius, std::string Name, Color color, Gas gas);
  82.     virtual  void displayStatistics();
  83. };
  84.  
  85.  
  86.  
  87.  
  88. Sphere::Sphere()  // Реалізація методів класу Sphere
  89. {
  90.     setRadius(1.0);
  91. }
  92. Sphere::Sphere(double Radius)
  93. {
  94.     setRadius(Radius);
  95. }
  96. void Sphere::setRadius(double Radius)
  97. {
  98.     mRadius = Radius;
  99. }
  100. double Sphere::Radius()
  101. {
  102.     return mRadius;
  103. }
  104. double Sphere::Diameter()
  105. {
  106.     return 2.0*Radius();
  107. }
  108. double Sphere::Circumference()
  109. {
  110.     return PI * Diameter();
  111. }
  112. double Sphere::Area()
  113. {
  114.     return PI * Diameter()*Diameter();
  115. }
  116. double Sphere::Volume()
  117. {
  118.     return 4 * PI*pow(Radius(), 3) / 3.0;
  119. }
  120. void Sphere::displayStatistics()
  121. {
  122.     std::cout << "Radius = " << Radius() << std::endl
  123.         << "Diameter = " << Diameter() << std::endl
  124.         << "Circumference = " << Circumference() << std::endl
  125.         << "Area = " << Area() << std::endl
  126.         << "Volume = " << Volume() << std::endl << std::endl;
  127. }
  128.  
  129.  
  130.  
  131. Ball::Ball()
  132. {
  133.     setRadius(100);
  134.     setName("ball");
  135.     setColor(BLACK);
  136. }
  137. Ball::Ball(double Radius, std::string Name, Color сolor) :Sphere(Radius)
  138. {
  139.     setName(Name);
  140.     setColor(сolor);
  141. }
  142. void Ball::setName(std::string Name)
  143. {
  144.     mName = Name;
  145. }
  146. std::string Ball::Name()
  147. {
  148.     return mName;
  149. }
  150. void Ball::setColor(Color color)
  151. {
  152.     mC = color;
  153. }
  154. Color Ball::color()
  155. {
  156.     return mC;
  157. }
  158. std::string Ball::ColorName()
  159. {
  160.     std::string ColorName;
  161.     switch (mC)
  162.     {
  163.     case RED: ColorName = "RED"; break;
  164.     case GREEN: ColorName = "GREEN"; break;
  165.     case BLUE: ColorName = "BLUE"; break;
  166.     case YELLOW: ColorName = "YELLOW"; break;
  167.     case WHITE: ColorName = "WHITE"; break;
  168.     case BLACK: ColorName = "BLACK"; break;
  169.     }
  170.     return ColorName;
  171. }
  172. void Ball::resetBall(double Radius, std::string Name, Color color)
  173. {
  174.     setRadius(Radius);
  175.     setName(Name);
  176.     setColor(color);
  177. }
  178. void Ball::displayStatistics()
  179. {
  180.     std::cout << "Name = " << Name() <<"\n Color Name = "<< ColorName() <<"\n radius = "<< Radius()
  181.         <<"\n  diameter = "<< Diameter() << " \n circumreference = " <<Circumference() <<"\n area = " << Area()<< std::endl;
  182.  
  183. }
  184.  
  185. Ballon::Ballon() :Ball() // Реалізація методів класу Ballon
  186. {
  187.     setGas(N2O2);
  188. }
  189. Ballon::Ballon(double Radius, std::string Name, Color color, Gas gas) : Ball(Radius, Name, color)
  190. {
  191.     setGas(gas);
  192. }
  193. void Ballon::setGas(Gas gas)
  194. {
  195.     mG = gas;
  196. }
  197. Gas Ballon::gas()
  198. {
  199.     return mG;
  200. }
  201. std::string Ballon::GasName()
  202. {
  203.     std::string GasName;
  204.     switch (mG)
  205.     {
  206.     case N2O2: GasName = "Air"; break;
  207.     case H2: GasName = "Hydrogen"; break;
  208.     case He: GasName = "Helium"; break;
  209.     case Ne: GasName = "Neon"; break;
  210.     }
  211.     return GasName;
  212. }
  213. void Ballon::resetBallon(double Radius, std::string Name, Color color, Gas gas)
  214. {
  215.     Ball::resetBall(Radius, Name,  color);
  216.     setGas(gas);
  217. }
  218. void Ballon::displayStatistics()
  219. {
  220.     Ball::displayStatistics();
  221.     std::cout << GasName();
  222. }
  223.  
  224. WeightBall::WeightBall() :Ball()
  225. {
  226.     setDensity(1000.0);
  227. }
  228. WeightBall::WeightBall(double Radius, std::string Name, Color color, double Density) : Ball(Radius, Name, color)
  229. {
  230.     setDensity(Density);
  231. }
  232. void WeightBall::setDensity(double Density)
  233. {
  234.     mDensity = Density;
  235. }
  236. double WeightBall::Density()
  237. {
  238.     return mDensity;
  239. }
  240. double WeightBall::Weight()
  241. {
  242.     return mDensity * Volume();
  243. }
  244. void WeightBall::resetWeightBall(double Radius, std::string Name, Color color, double Density)
  245. {
  246.     resetBall(Radius, Name, color);
  247.     setDensity(Density);
  248. }
  249. void WeightBall::displayStatistics()
  250. {
  251.     Ball::displayStatistics();
  252.     printf(" Density=%lf\n Weight=%lf\n", Density(), Weight());
  253. }
  254.  
  255.  
  256. WeightBallon::WeightBallon() :Ballon()  // Реалізація методів класу WeightBallon
  257. {
  258. }
  259. WeightBallon::WeightBallon(double Radius, std::string Name, Color color, Gas gas) : Ballon(Radius, Name, color, gas)
  260. {
  261. }
  262. double WeightBallon::Weight()
  263. {
  264.     double Density;
  265.     switch (Gas())
  266.     {
  267.     case N2O2: Density = 1.225; break;
  268.     case H2: Density = 0.09; break;
  269.     case He: Density = 0.178; break;
  270.     case Ne: Density = 0.901; break;
  271.     }
  272.     WeightBall::setDensity(Density);
  273.     WeightBall::Density();
  274.     return WeightBall::Weight();
  275. }
  276. void WeightBallon::resetWeightBallon(double Radius, std::string Name, Color color, Gas gas)
  277. {
  278.     resetBallon(Radius, Name, color, gas);
  279. }
  280. void WeightBallon::displayStatistics()
  281. {
  282.     Ballon::displayStatistics();
  283.     std::cout<< "\n Density=" << Density() << "\n  Weight= " << Weight() <<"\n";
  284. }
  285.  
  286. int main()
  287. {
  288.     Sphere s( 10);
  289.     s.displayStatistics();
  290.     std::cout << std::endl;
  291.     Ball b(100, "ball1", BLACK);
  292.     b.displayStatistics();
  293.     std::cout << std::endl;
  294.     Ballon ballon1(100, "Ballon1 ", RED , He);
  295.     ballon1.displayStatistics();
  296.     std::cout << std::endl;
  297.     WeightBall wb1(20, " wb1", RED, 20);
  298.     wb1.displayStatistics();
  299.     std::cout << std::endl;
  300.     WeightBallon wballon1(20, " wballon1", BLACK, H2);
  301.     wballon1.displayStatistics();
  302.     std::cout << std::endl;
  303.     std::cout << std::endl;
  304.     std::cout << std::endl;
  305.     std::cout << std::endl;
  306.     std::cout << wb1.Weight();
  307.     std::cout << std::endl;
  308.  
  309.     std::cout << wballon1.Weight();
  310.     system("pause");
  311.  
  312.  
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement