Posts

Showing posts from July, 2017

Facade Design Pattern

1. It is one of structural design pattern. 2. This design pattern provides the simplified interface to the user and hide the complex details. Checklist:   1. Create a common interface for subsystem.   2. Create one wrapper class which include all the subsystem.   3. Create a object of wrapper class which provide the simplify interface of the larger body of  system like a class library. Implementation: Here we are implementing the Widget library which is providing the different widgets and there properties in single Object. 1. Implementing the common interface class which contain the properties of widget. class widget{      void size(int l, int b);      void color(sring color_name); }; 2. implements the widgets with there property by inheriting the interface class widget. class button:widget {     void size(int l, int b);    {         cout<<"color button";   ...

Composition vs Aggregation

Image
Sometime complex objects are created using smaller & simpler objects. if we take a car; its compose of wheel, body, engine, steering etc. The process of creating a complex object with the help of small objects is known as object composition. 1. Association Association is a relationship where all objects have their own life cycle and there is no owner. There is no ownership between the objects and both have their own life-cycle. Both can create and delete independently. 2.Composition In composition relationship,  The main object is responsible for the life & death of its sub objects. that means when main object is created, sub objects are created. when main object is deleted all its sub parts are deleted Its also know as "is-a" relationship. #include<iostream> using namespace std; class Enginee {         int power;         int type;     public:      ...

Factory Design Pattern

Image
This is one of creational design pattern  provides the best ways to create an object. In factory design pattern sub class decide whose object should be created on the basis of argument. checklist: 1. Create abstract base class. 2. Derive sub classes from that base class & override all the functions of abstract base class. 3. Create a Factory class which will provide a object of Derive sub class as per requirement. Client just needs to make call to library’s guifactory CreateWidget method and pass the type it wants without worrying about the actual implementation of creation of objects Here we implemented the widgets for GUI library. #include<iostream> using namespace std; enum widgettype {     BUTTON,     SLIDER,     CHECKBOX }; class Widget {     public:         virtual void Init()=0;         virtual void Show()=0;   ...

Name Mangling in C++

In  C++ we can write multiple functions of  same name with different parameter type is call function overloading. To achieve function overloading c++ uses a concept know as "name mangling". For differentiating the function name c++ compiler change the function name by adding information about arguments Ex: actual function prototype:   void show(int arg) After name mangling:         void show_i(int arg) actual function prototype:   void show(float arg) After name mangling:         void show_f(float arg) Here the function show(int) & show(float) changes to show_i(int) & show_f(float) Actually in developer think that he/she is calling show function but compiler changer that show to show_i or show_f according to the arguments. C doesn't support "name mangling"; so we can't call c function in c++ project. To call c function in c++ "extern" key word is used. How do we call C function from C++ Ans: ...

Basic Vector Implementation

template <typename T> myvector { T *array; int size;         typedef T* Iterator; public: myvector() { size =0; } myvector(int s) { array = new T[s]; size = s; }   myvector(int s, T val) { array = new T[s]; for(int i=0;i<s;i++) array[i] = val;                 }                                 myvector(const myvector &obj1)                 {                      size = obj1.size;                      array = new T[obj1.size];                                            for(int i=0;i<obj1.size;i++)  ...