Posts

constructor throw exception

When a construct throws an exception then; that object is not fully constructed so destructor can not be called on that object. c++ destroys only fully constructed objects So this may leads to memory leakage. To solve this problem we have to 1. write a cleanup function which clears all allocated memory or resource 2. write the part of constructor block which  may leads to crash in try block & call cleanup function in catch block. #include<iostream> using namespace std; class first {     public:         first()         {             cout<<"in constructor of first"<<endl;         }         ~first()         {             cout<<"in destructor of first"<<endl;       ...

Why copy constructor is passed by reference not by pointer

The syntax of copy constructor says that it should be reference not pointer. 1. Passing by references ensures an actual object is passed to the copy constructor, while a pointer can have NULL value, and make the constructor fail. 2. if we pass a pointer then we have to dereference it. so it will be overhead for the user. Technically we can write a constructor which pass a pointer; but that is overloaded constructor not copy constructor.

State Design Pattern

Image
It is one the behavioural design pattern The state design pattern is a design pattern that allows an object to completely change its behavior depending upon its current internal state. The state design pattern change the object's behavior at run-time without changing the interface used to access the object or losing the current state. The class change is hidden to outside world with the use of wrapper object. Its also known as state machine. Example: In game design; we have many screens/state main-menu, loading screen, game-play, in game menu screen, result screen etc. To maintain different screen state; we use state design pattern to traverse different screen. Example code #include<iostream> using namespace std; class GameState {     public:         virtual void handleKey()=0;         virtual void handleMouse()=0; }; class Game {     GameState *currentScreen; ...

Bridge Design Pattern

Image
This is structural design pattern. The bridge pattern is a design pattern that separates the abstract elements of a class from its technical implementation. This provides a cleaner implementation of real-world objects and allows the implementation details to be changed easily. The bridge pattern is used to separate the abstract elements of a class from the implementation details. For example, the abstract elements may be the business logic of an application. They can be created without any knowledge of the implementation details of their data access or interoperability with the operating system. The pattern provides the means to replace the implementation details without modifying the abstraction. This permits, for example, changing operating systems, databases, etc. with no impact to the business logic. Example: In automate message sending application there are mainly 3type of mode to send message phone call, sms & email. There is no issue if there are less number of sendi...

Strategy Design Pattern

Image
This is one of behavioural design pattern. The strategy pattern is a design pattern that allows a set of similar algorithms to be defined and encapsulated in their own classes. The algorithm to be used for a particular purpose may then be selected at run-time according to your requirements. The strategy pattern is used to create an interchangeable family of algorithms from which the required process is chosen at run-time. This allows the behavior of a program to change dynamically according to configuration details or user preferences. It also increases flexibility by allowing new algorithms to be easily incorporated in the future. Checklist 1. Create a abstract base class which contains behaviors. 2. Implements the behaviors in derived classes. 3. Define a strategy class which aggregates with  behavior class. Example : While traveling, we can choose any of transport service; bus, cab, train, bike. Each travel service have their estimate time & cost of travel. Trav...

Builder Design Pattern

Image
Builder Design pattern is one of the creational design pattern. This pattern allows for the step-by-step creation of complex objects using the correct sequence of actions. The construction is controlled by a director object that only needs to know the type of object it is to create. Example #include<iostream> using namespace std; class Meal {     public:         string Sandwich;         string Drink;         string Sideitem;         int price;         void ServeMeal()         {             cout<<"Your order please"<<endl;             cout<<Sandwich<<endl<<Drink<<endl<<Sideitem<<endl<<endl;          ...

STL Tutorial

Image
https://www.cs.helsinki.fi/u/tpkarkka/alglib/k06/lectures/containers.html Vector:  Vectors are sequence containers that have dynamic size. In other words, vectors are dynamic arrays. Just like arrays, vector elements are placed in contiguous storage location so they can be accessed and traversed using iterators. Vector provide random access. Insertions and deletions in the beginning or in the middle are slow. #include <iostream> #include <vector> using namespace std; int main()  {    vector<int> vec;    int i;    cout << "vector size = " << vec.size() << endl;    for(i = 0; i < 5; i++) {       vec.push_back(i);    }    cout << "extended vector size = " << vec.size() << endl;    for(i = 0; i < 5; i++) {       cout << "value of vec [" << i << "] = " << vec[i] <...