Advertisement
TheWhiteFang

Tutorial7 Q4 [Top level method]

Dec 10th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. // http://pastebin.com/u/TheWhiteFang
  2. // Tutorial7 Q4 [Top level method]
  3. #include <iostream>
  4. #include <math.h>
  5. using namespace std;
  6. #define PI 3.141
  7.  
  8. class CCylinder{
  9.     float m_Radius;
  10.     float m_Height;
  11.     float m_SurfaceArea;
  12.  
  13. public:
  14.     CCylinder (float inRadius = 0.0 , float inHeight = 0.0){
  15.    
  16.         m_Radius = inRadius;
  17.         m_Height = inHeight;
  18.         m_SurfaceArea = ( 2 * PI * inRadius*inRadius) + (2 * PI * inRadius * inHeight);
  19.  
  20.     }
  21. friend CCylinder operator+ (const CCylinder &inVal1, const CCylinder &inVal2);
  22. friend ostream &operator<<(ostream &output, const CCylinder &source);
  23. };
  24.  
  25. //top level function
  26.  
  27. CCylinder operator+ (const CCylinder &inVal1, const CCylinder &inVal2)
  28. {
  29.     CCylinder temp;
  30.  
  31.     temp.m_SurfaceArea = (PI*inVal2.m_Radius*inVal2.m_Radius)-(PI*inVal1.m_Radius*inVal1.m_Radius);
  32.     temp.m_SurfaceArea += (PI*inVal2.m_Radius*inVal2.m_Radius) + (PI*inVal1.m_Radius*inVal1.m_Radius);
  33.     temp.m_SurfaceArea += (2*PI*inVal1.m_Radius*inVal1.m_Height)+(2*PI*inVal2.m_Radius*inVal2.m_Height);
  34.  
  35.  
  36.     return temp;
  37.  
  38. }
  39.  
  40. //ostream operator
  41. ostream &operator<<(ostream &output, const CCylinder &source){
  42.     output << "Surface Area = "<< source.m_SurfaceArea << " cm2 )" << endl;
  43.     return output;
  44. }
  45.  
  46.  
  47. int main()
  48. {
  49.     CCylinder cyObj1((float)2.5, (float)3.5);
  50.     CCylinder cyObj2((float)3.5, (float)4.5);
  51.     cout<< "Cylinder 1 (" << cyObj1 << /*")" <<*/ endl;
  52.     cout<< "Cylinder 2 (" << cyObj2 <</* ")" <<*/ endl;
  53.  
  54.     CCylinder cyObj3 = cyObj1 + cyObj2;
  55.     cout << "Cylinder 1 + Cylinder 2 = ";
  56.     cout << "(" << cyObj3 <</* ")" <<*/ endl;
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement