Advertisement
TheWhiteFang

Tutorial 7 Q3 [Top-level method]

Dec 7th, 2014
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. // http://pastebin.com/u/TheWhiteFang
  2. // Tutorial 7 Q3 [Top-level method]
  3. #include <iostream>
  4. #include <math.h>
  5. using namespace std;
  6.  
  7. class CCube{
  8.     float m_EdgeLength;
  9.     float m_SurfaceArea;
  10.  
  11. public:
  12.     CCube(float inEdge = 0.0){
  13.  
  14.         m_EdgeLength = (float)inEdge;
  15.         m_SurfaceArea = 6 * (inEdge*inEdge);
  16.         // To be completed
  17.     }
  18.  
  19.     friend CCube operator+ (const CCube &inVal1, const CCube &inVal2);
  20.     friend ostream &operator<<(ostream &output, const CCube &source);
  21.  
  22. };
  23.  
  24. CCube operator+ (const CCube &inVal1, const CCube &inVal2)
  25. {
  26.     CCube temp;
  27.  
  28.     temp.m_SurfaceArea = (5 * pow(inVal1.m_EdgeLength, 2)) + (5 * pow(inVal2.m_EdgeLength, 2));
  29.     temp.m_SurfaceArea += inVal2.m_EdgeLength - inVal1.m_EdgeLength;
  30.  
  31.     //if (inVal2.m_EdgeLength > inVal1.m_EdgeLength){
  32.     //  temp.m_SurfaceArea += inVal2.m_EdgeLength - inVal1.m_EdgeLength;
  33.     //}
  34.  
  35.     //else if (inVal1.m_EdgeLength > inVal2.m_EdgeLength){
  36.     //  temp.m_SurfaceArea += inVal1.m_EdgeLength - inVal2.m_EdgeLength;
  37.     //}
  38.  
  39.     return temp;
  40.  
  41. }
  42.  
  43.  
  44.  
  45. ostream &operator<<(ostream &output, const CCube &source){ //return ostream by reference //only thing that changes is classname
  46.     output << "Surface Area = "<< source.m_SurfaceArea << " cm2 )" << endl;
  47.     return output;
  48. }
  49.  
  50. int main()
  51. {
  52.     CCube cubeObj1((float)2.5);
  53.     CCube cubeObj2((float)3.5);
  54.     cout << "Cube 1 (" << cubeObj1 << /*")" <<*/ endl;
  55.     cout << "Cube 2 (" << cubeObj2 << /*")" <<*/ endl;
  56.  
  57.     CCube cubeObj3 = cubeObj1 + cubeObj2;
  58.     cout << "Cube 1 + Cube 2 = ";
  59.     cout << "(" << cubeObj3 << /*")" <<*/endl;
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement