Advertisement
TheWhiteFang

Tutorial 10 Q1

Jan 5th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. //pasterbin.com/u/thewhitefang
  2. // Tutorial 10 Q1
  3. #include<iostream>
  4.  
  5. using namespace std;
  6.  
  7. class Data{
  8.  
  9. public:
  10.     double m_Opposite;
  11.     double m_Adjacent;
  12.     double m_Hypotenuse;
  13.  
  14. };
  15.  
  16. //interface class - pure virtual class (contains pure virtual functions)
  17. //arrow means association
  18. class ITrigo{
  19.  
  20. public:
  21.     virtual void CompAngle(Data input, double& angle1, double& angle2) = 0; //add 'virtual' and '=0'
  22. };
  23.  
  24. //arrow means realization of prv class
  25.  
  26. class SinCsc:public ITrigo{ //worker class realizing Itrigo
  27. public:
  28.     void CompAngle(Data input, double& angle1, double& angle2)
  29.     {
  30.         angle1 = input.m_Opposite / input.m_Hypotenuse; //sin
  31.         angle2 = input.m_Hypotenuse / input.m_Opposite;
  32.     }
  33.  
  34. };
  35.  
  36. class TanCot : public ITrigo{
  37.  
  38.     void CompAngle(Data input, double& angle1, double& angle2)
  39.     {
  40.         angle1 = input.m_Opposite / input.m_Adjacent; //sin
  41.         angle2 = input.m_Opposite / input.m_Hypotenuse;
  42.     }
  43.  
  44.  
  45. };
  46.  
  47. //black diamond represents composition - strong association
  48. //white diamond means aggregation - lightly associated
  49. //no dotted line so no inheritance
  50.  
  51. class Trigo
  52. {
  53. private:
  54.     Data m_input;
  55.     ITrigo* m_pObject;
  56.  
  57. public:
  58.     void SetAttributes(Data input)
  59.     {
  60.         m_input = input;
  61.     }
  62.     void SetTrigoObject(ITrigo* pObject)
  63.     {
  64.         m_pObject = pObject;
  65.     }
  66.     void Compute(double& angle1, double& angle2)
  67.     {
  68.         m_pObject->CompAngle(m_input, angle1, angle2);
  69.     }
  70.  
  71. };
  72.  
  73. int main(){
  74.  
  75.     Data d;
  76.     Trigo t;
  77.  
  78.     d.m_Opposite = 4.0; d.m_Adjacent = 3.0; d.m_Hypotenuse = 5.0;
  79.     t.SetAttributes(d);
  80.     //iv
  81.  
  82.     ITrigo *pObj = new SinCsc(); //Itrigo takes the form of SInCsc
  83.     t.SetTrigoObject(pObj);
  84.  
  85.     double sine, cosecant;
  86.     t.Compute(sine, cosecant);
  87.  
  88.     cout << "Sine: " << sine << endl;
  89.     cout << "Cosecant: " << cosecant << endl;
  90.  
  91.  
  92.     delete pObj;
  93.     //new code
  94.     pObj = new TanCot();
  95.     t.SetTrigoObject(pObj); // to set new values
  96.  
  97.     double tangent, cotangent;
  98.     t.Compute(tangent, cotangent);
  99.  
  100.     cout << "Tangent: " << tangent << endl;
  101.     cout << "Cotangent: " << cotangent << endl;
  102.  
  103.     delete pObj;
  104.  
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement