Advertisement
Guest User

DingDong.h

a guest
Nov 27th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #ifndef SHAPE_H
  2. #define SHAPE_H
  3.  
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. class Shape
  8. {
  9. public:
  10. Shape(){};
  11. //Shape constructor that takes in radius.
  12. Shape(double _radius) : radius(_radius){
  13. };
  14.  
  15. //virtual function to calculate area of the shape.
  16. virtual double CalculateArea(){
  17. return 1;
  18. };
  19.  
  20. //virtual function to calculate volume of the shape.
  21. virtual double CalculateVolume()
  22. {
  23. return 1;
  24. };
  25.  
  26. //virtual function to print out the detail of the shape.
  27. virtual void print()
  28. {
  29. cout << "default shape" << endl;
  30. };
  31.  
  32. double radius;
  33.  
  34. static double PI;
  35.  
  36. };
  37.  
  38. double Shape::PI = 3.14;
  39. #endif
  40.  
  41.  
  42. #include "Shape.h"
  43. #include <cmath>
  44. #include <iostream>
  45. #include <iomanip>
  46.  
  47. using namespace std;
  48.  
  49. //Cone class that inherits Shape
  50. class Cone :public Shape
  51. {
  52. public:
  53.  
  54. double height;
  55. Cone(){};
  56. Cone(double _radius, double _height) : Shape(_radius){
  57. height = _height;
  58. };
  59.  
  60. ~Cone();
  61.  
  62. virtual double CalculateArea();
  63. virtual double CalculateVolume();
  64. virtual void print();
  65. private:
  66. };
  67.  
  68. //calculate area of a cone.
  69. double Cone::CalculateArea()
  70. {
  71. return (PI * radius * (radius + sqrt(radius*radius + height*height)));
  72. }
  73.  
  74. //calculate volume of a cone.
  75. double Cone::CalculateVolume()
  76. {
  77. return (PI * height * (radius * radius) / 3);
  78. }
  79.  
  80. //print details of a cone.
  81. void Cone::print()
  82. {
  83. cout << "-------" << endl;
  84. cout << "Cone" << endl;
  85.  
  86. cout << "Radius: " << radius << endl;
  87. cout << "Height: " << height << endl;
  88. cout << "Area: " << fixed << setprecision(3) << CalculateArea() << endl;
  89. cout << "Volume: " << fixed << setprecision(3) << CalculateVolume() << endl;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement