Advertisement
TheWhiteFang

Tutorial 10 Q1 with added exception handling

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