Advertisement
daniv1

Untitled

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