donmike25

c++ tuto 10 ques 1

Jan 4th, 2015
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Data
  5. {
  6. public:
  7.     double m_Opposite;
  8.     double m_Adjacent;
  9.     double m_Hypotenuse;
  10.  
  11. };
  12.  
  13. class ITrigo // pure virtual class
  14. {
  15. public:
  16.     virtual void CompAngle(Data input, double& angle1, double& angle2) = 0;
  17. };
  18.  
  19. class SinCsc : public ITrigo // worker class, realizing ITrigo
  20. {
  21. public:
  22.     void CompAngle(Data input, double& angle1, double& angle2)
  23.     {
  24.         angle1 = input.m_Opposite / input.m_Hypotenuse;
  25.         angle2 = input.m_Hypotenuse / input.m_Opposite;
  26.     }
  27. };
  28.  
  29. class Trigo
  30. {
  31. private:
  32.     Data m_Input;
  33.     ITrigo* m_pObject;
  34.  
  35. public:
  36.     void SetAttributes (Data input)
  37.     {
  38.         m_Input = input;
  39.     }
  40.     void SetTrigoObject (ITrigo* pObject)
  41.     {
  42.         m_pObject = pObject;
  43.     }
  44.     void Compute (double& angle1, double& angle2)
  45.     {
  46.         m_pObject->CompAngle(m_Input, angle1, angle2);
  47.     }
  48.  
  49. };
  50.  
  51. int main(){
  52.  
  53.     Data d;
  54.     Trigo t;
  55.  
  56.     d.m_Opposite = 4.0; d.m_Adjacent = 3.0, d.m_Hypotenuse = 5.0;
  57.  
  58.     t.SetAttributes(d);
  59.  
  60.     ITrigo *pObj = new SinCsc();  
  61.     t.SetTrigoObject(pObj);
  62.  
  63.     double sine, cosecant;
  64.     t.Compute(sine, cosecant);
  65.  
  66.     cout << "Sine: " << sine << endl;
  67.     cout << "Cosecant: " << cosecant << endl;
  68.  
  69.     delete pObj;
  70.  
  71.  
  72.  
  73.  
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment