Headlines News :
Home » , , » Learn C++ in 12 days - Day 12

Learn C++ in 12 days - Day 12

Written By Ente Malayalam on Friday, May 24, 2013 | 5/24/2013


            By yesterday, we have finished the first two major steps of object oriented programming
-          Abstraction
-          Encapsulation
If we take our first example, Car dealer ship system, after the above two examples, all the real world entities are practically implemented in terms of classes and objects. As we said before, the system will have a meaning only when all these entities work together in a co-operative manner. Or I should say, system will have a meaning only when proper relationships are established between the different entities. That exactly is our next step.
Establishing Relationships :
            We will take the same relationship diagrams which we established in our first class. Let me put it diagrammatically wilth UML notations here.  







     








We have currently represented it diagrammatically. Our next job is to practically implement it. So lets start with association.

Association:
            As I have already told you, the relationship association comes into picture when two entities are related for some specific functionality. For eg: if you consider customer and car, they are related for functionalities like either buy a car or configure a car. Mean while change in one entity is not going to effect the associated entity.
            Talking about the implementation,  if I take the same example, the entity called car comes into picture only for the specific behaviour or customer ie) buy or configure a car. Ie) an object of car should come into existence when the behaviour buy or configure car is invoked and as expected it will go out of existence once the functionality is over. Keeping these things in mind and implementing it (we will implement it for configure a car)
//car.h
#include <iostream>
using namespace std;
class Aclass_Car        {
        private:
                char Model_Name[20];
                int Speed;
                int Mileage;
        public:
                Aclass_Car()    {}
                Aclass_Car(char *n, int s, int m)       {
                        strcpy(Model_Name, n);
                        Speed = s;
                        Mileage = m;
                }
                void Set_Speed(int);
                void Set_Mileage(int);
                void Move();
};

//car_library.cpp
#include "car.h"

void Aclass_Car :: Set_Speed(int s)     {
        Speed = s;
}

void Aclass_Car :: Set_Mileage(int m)   {
        Mileage = m;
}

void Aclass_Car :: Move()       {
        cout << "Car is moving with a speed of " << Speed << "km/hour and";
        cout << "a mileage of " << Mileage << endl;
}

//customer.h
#include <iostream>
using namespace std;

class Customer  {
        private:
                char Customer_Name[20];
                int Reference_Id;
        public:
                Customer()      {}
                Customer(char *n, int r)        {
                        strcpy(Customer_Name, n);
                        Reference_Id = r;
                }
                void Configure_Car();
};









//customer_library.cpp
#include "car.h"
#include "customer.h"

void Customer :: Configure_Car()        {
        Aclass_Car *Bmw = new Aclass_Car("BMW", 1, 1);

        Bmw -> Set_Speed(30);
        Bmw -> Set_Mileage(8);

        Bmw -> Move();
        delete Bmw;
}

//car_customer_application.cpp
#include "car.h"
#include "customer.h"

int main()      {
        Customer John("John Abraham", 12);
        John.Configure_Car();
}

As you have noticed, the life span of the related entity car exists only for the specific functionality called Configure_Car();

Shared Aggregation:

            Lets try to implement the shared aggregation relationship between car and ac. Here car and ac is having a whole part relationship but they are loosely coupled. In the sense, the life time of both the entities may not be same.
            Talking about the implementation, If you consider ac and car, we can see that ac can be inserted or removed any time you want. But ac will surely be destroyed when the car gets destroyed. Keeping these things in mind, lets try to implement the shared aggregation here.

//ac.h
#include <iostream>
using namespace std;

class Ac        {
        private:
                float Temperature;
        public:
                Ac()    : Temperature(21.5)     {}
                void Set_Temperature(float);
};

//ac_library.cpp
#include "ac.h"

void Ac :: Set_Temperature(float t)     {
        Temperature = t;
}
//car.h
#include <iostream>
#include "ac.h"
using namespace std;

class Car       {
        private:
                char Model_Name[20];
                Ac *Aptr;
        public:
                Car()   {}
                Car(char *m)    {
                        strcpy(Model_Name, m);
                }
                void Plug_Ac();
                void Switch_On_Ac();
                void Switch_Off_Ac();
                ~Car()  {
                        if(Aptr != NULL)
                                delete Aptr;
                }
};

//car_library.cpp
#include "car.h"

void Car :: Plug_Ac()   {
        Aptr = new Ac;
}

void Car :: Switch_On_Ac()      {
        float Temperature;
        cout << "Enter the Temperature" << endl;
        cin >> Temperature;
        Aptr -> Set_Temperature(Temperature);
        cout << "Ac is on and temperature is set to " << Temperature << " celsius" << endl;
}


void Car :: Switch_Off_Ac()     {
        Aptr -> Set_Temperature(0.0);
        cout << "Ac is switched off" << endl;
}


//car_ac_application.cpp
#include "car.h"
int main()      {
        Car Bmw("BMW");
        Bmw.Plug_Ac();
        Bmw.Switch_On_Ac();
        Bmw.Switch_Off_Ac();
}

Note , even though ac comes into picture in a behaviour Plug_Ac(), we have made sure that it gets destroyed when the car is destructed.

Composite Aggregation:
           
            If you consider the relationship between Car and Engine, you can see that even they have whole part relationship but only difference here is they are strongly / tightly coupled. In the sense, the life time of both the entities will be same or in other words car doesn’t have any existence without any engine.
            So talking about the implementation, engine should get constructed at the same time of construction of car and it should get destroyed at the time of destruction of car. Keeping these things in mind, lets try to establish composite aggregation between car and engine.

//engine.h
#include <iostream>
using namespace std;


class Engine    {
        private:
                float Temperature;
                int Cubic_Capacity;
        public:
                Engine()  : Temperature(30.00), Cubic_Capacity(800)     {}
                void Start_Engine();
                float Check_Temperature();
                void Cool_Engine();
};



//engine_library.cpp
#include "engine.h"

void Engine :: Start_Engine()   {
        cout << "Engine Started" << endl;
        Temperature += 200;
}

float Engine :: Check_Temperature()     {
        return Temperature;
}

void Engine :: Cool_Engine()    {
        cout << "Engine cooled...it s safe" << endl;
        Temperature -= 200;
}

//car.h
#include <iostream>
#include "engine.h"
using namespace std;

class Car       {
        private:
                char Model_Name[20];
                Engine *Eptr;
        public:
                Car(char *m)    {
                        strcpy(Model_Name, m);
                        Eptr = new Engine;
                }
                void Move_Car();
                ~Car()  {
                        delete Eptr;
                }
};










//car_library.cpp
#include "car.h"

void Car :: Move_Car()  {
        Eptr -> Start_Engine();
        if (Eptr -> Check_Temperature() >= 100) {
                cout << "Temperature crossess the limit" << endl;
                cout << "cool it" << endl;
                Eptr -> Cool_Engine();
        }
}

//application.cpp
#include "car.h"

int main()      {
        Car Bmw("Bmw");
        Bmw.Move_Car();
}


Dependency:
           
            Considering the entities racing car and racing  track, as we discussed before the ground clearance (which is directly related to suspension height) of the car depends upon the racing road. We can even call it as using relationship. One thing you can notice here is it is very similar to association. But here the change in features of one entity effects the other also. For eg: change in type of racing track effect the ground clearance of car.
           
            Talking about the implementation, here since setting up the suspension of car checks the type of racing  track which is a private property of the class racing track, dependency will be implemented using the concept of friends. Keeping all these things in mind, lets try to do the implementation.













//racing_track.h
#include <iostream>
#include "racing_car.h"
using namespace std;

class Racing_Track      {
        private:
                char Track_Type[20];
        public:
                Racing_Track()  {
                        strcpy(Track_Type, "Normal");
                }
                Racing_Track(char *t)   {
                        strcpy(Track_Type, t);
                }
                friend void Racing_Car :: Set_Up_Suspension(Racing_Track);
};

//racing_car.h
#include <iostream>
using namespace std;
class Racing_Track;
class Racing_Car        {
        private:
                int Ground_Clearance;
        public:
                Racing_Car() : Ground_Clearance(1300)   {}
                void Set_Up_Suspension(Racing_Track);
};

//racing_car_library.cpp
#include "racing_track.h"

void Racing_Car :: Set_Up_Suspension(Racing_Track Tobj) {
        if (strcmp(Tobj.Track_Type, "Normal"))
                cout << "current ground clearance is enough" << endl;
        else
                Ground_Clearance = 3000;
}







//racing_car_track_application.cpp
#include "racing_track.h"

int main()      {
        Racing_Car Ferrari;
        Racing_Track Track_Normal;
        Racing_Track Track_Rough("Rough");

        Ferrari.Set_Up_Suspension(Track_Normal);
        Ferrari.Set_Up_Suspension(Track_Rough);

}

Now the only relationship we left is inheritance or generalization. Our next 2 to 3 sessions will purely concentrate on that.

Reference : OOAD , Grady Booch
n stw �A' o ��� s� 2.0pt;font-family:"Times New Roman","serif"'>}

main()
{
        stack <int> s1;
        s1.push(10);
        s1.push(20);
        s1.push(30);
        cout << s1.pop() << endl;
        cout << s1.pop() << endl;  
        cout << s1.pop() << endl;
        stack <char> s2;
        s2.push('a');
        s2.push('b');
        s2.push('c');
        cout << s2.pop() << endl;
        cout << s2.pop() << endl;
        cout << s2.pop() << endl;
}       








An eg with two generic data types:

Let s c an eg:

template <class T1, class T2>
class sample    {
        private:
                T1 x;
                T2 y;
        public:
                sample(T1 p, T2 q)      {
                        x = p;
                        y = q;
                }
                void display();
};
template <class T1, class T2> void sample <T1, T2> :: display()
{
        cout << x << endl;
        cout << y << endl;
}
main()    
{
        sample <int, char> obj(10, 'a');
        obj.display();
}

Share this article :

0 comments:

Post a Comment

 
Support : Creating Website | Johny Template | Maskolis | Johny Portal | Johny Magazine | Johny News | Johny Demosite
Copyright © 2011. Education World - All Rights Reserved
Template Modify by Creating Website Inspired Wordpress Hack
Proudly powered by Blogger