Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //pasterbin.com/u/thewhitefang
- // Tutorial 10 Q1 with added exception handling
- #include<iostream>
- #include<string>
- using namespace std;
- class Data{
- public:
- double m_Opposite;
- double m_Adjacent;
- double m_Hypotenuse;
- };
- //interface class - pure virtual class (contains pure virtual functions)
- //arrow means association
- class ITrigo{
- public:
- virtual void CompAngle(Data input, double& angle1, double& angle2) = 0; //add 'virtual' and '=0'
- };
- //arrow means realization of prv class
- class SinCsc:public ITrigo{ //worker class realizing Itrigo
- public:
- void CompAngle(Data input, double& angle1, double& angle2)
- {
- angle1 = input.m_Opposite / input.m_Hypotenuse; //sin
- angle2 = input.m_Hypotenuse / input.m_Opposite;
- }
- };
- class TanCot : public ITrigo{
- void CompAngle(Data input, double& angle1, double& angle2)
- {
- angle1 = input.m_Opposite / input.m_Adjacent; //sin
- angle2 = input.m_Opposite / input.m_Hypotenuse;
- }
- };
- //black diamond represents composition - strong association
- //white diamond means aggregation - lightly associated
- //no dotted line so no inheritance
- class Trigo
- {
- private:
- Data m_input;
- ITrigo* m_pObject;
- public:
- Trigo(){
- m_pObject = NULL;
- }
- void SetAttributes(Data input)
- {
- m_input = input;
- }
- void SetTrigoObject(ITrigo* pObject)
- {
- m_pObject = pObject;
- }
- void Compute(double& angle1, double& angle2)
- {
- //to access member variable/function of other class
- if(m_pObject == NULL)
- {
- throw string("Error: m_pObject is not initialized");
- }
- m_pObject->CompAngle(m_input, angle1, angle2);
- }
- };
- int main(){
- Data d;
- Trigo t;
- d.m_Opposite = 4.0; d.m_Adjacent = 3.0; d.m_Hypotenuse = 5.0;
- t.SetAttributes(d);
- //iv
- ITrigo *pObj = new SinCsc(); //Itrigo takes the form of SInCsc
- //t.SetTrigoObject(pObj); //if not called program will crash
- double sine, cosecant;
- try
- {
- t.Compute(sine, cosecant);
- cout << "Sine: " << sine << endl;
- cout << "Cosecant: " << cosecant << endl;
- }
- catch(string obj)
- {
- cout << obj << endl;
- }
- delete pObj;
- //new code
- pObj = new TanCot();
- t.SetTrigoObject(pObj); // to set new values
- double tangent, cotangent;
- t.Compute(tangent, cotangent);
- cout << "Tangent: " << tangent << endl;
- cout << "Cotangent: " << cotangent << endl;
- delete pObj;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement