Monday, July 3, 2023

C++ : Pointers and Reference

 Demo to illustrate use of pointers and reference when passed as parameter to a function

Example : https://github.com/puthukkudi/PointersAndReference.git

C++ : Adapter Pattern

The adapter pattern convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.


Examplehttps://github.com/puthukkudi/AdapterPattern.git

Sunday, July 2, 2023

C++: Abstract Factory Pattern

Abstract Factory design pattern is one of the Creational pattern. Abstract Factory pattern is almost similar to Factory Pattern and is considered as another layer of abstraction over factory pattern. Abstract Factory patterns work around a super-factory which creates other factories.

Abstract factory pattern implementation provides us with a framework that allows us to create objects that follow a general pattern. So at runtime, the abstract factory is coupled with any desired concrete factory which can create objects of the desired type.


Example :


In Factory pattern example we had vehicle factory to create car and bike object.Now in Abstract factory pattern we introduce one more level where client ask vehicle factory to create either TATA car or Toyota Car. Similarly it cas ask to create TATA bike or Toyota bike.


https://github.com/puthukkudi/AbstractFactoryPattern.git




Saturday, July 1, 2023

C++ : Factory Pattern

  •  Factory Pattern comes under creational pattern.
  • It provides a way of creating the object without exposing the creation logic to the client.

Example : Vehicle library for the client

Friday, June 30, 2023

C++ : Singleton pattern

 Singleton Pattern


Singleton is creational design pattern.
Only one instance of the class is created in Singleton Pattern.

Example :

- Only one instance of confriguation manager
- Only one instance of Logger
- Only one instance of db connection





Thursday, June 29, 2023

C++ : Constructors And Destructors

Constructors are used to construct an instance of a class and initiallize the data members of a class.

In C++ initiallization of data members are mandatory.

Destructors are used to deallocate the memory occupied by the instance of a Class.

Please check the example below :

https://github.com/puthukkudi/ConstructorsAndDestructors.git

C++ : Virtual Functions

Virtual functions helps to override methods from base class.
If the Base class and the Child class  both defines the function method() then the pointer reference of Base class pointing to the object of Child class when used to call function method(); it will call the Base class function method()

i.e;

Base * base = new Child();
base->method(); // this will call the Base class method() ; unless untill you declare method() as virtual in base class.



Internal Working :

If compiler finds any virtual function it creates a static array for each class called VTable or Virtual Table. Elements of this VTable are the function pointer to each member function.Since Vtable is a static array it means its object independent.

During object creation compiler creates a secrete data member for the base class. This data memeber is called Virtual Pointer. This Virtual Pointer is inherited by the child class. Respective class instances's virtual pointer points to respective class's VTable. Hence provisioning for overriding. 

Virtual Functions can't be static as static functions are tied to a class and not to instance of a class.






Please check the example below : 

https://github.com/puthukkudi/VirtualFunction.git

Wednesday, June 28, 2023

C++ : POINTERS

A pointer is an integer that stores the memory address.



Consider  a single street, with houses on one side from start to end.
Memory in a computer is just like this street, its an one dimmensional array/line, with a sequence of byte arranged in that 1D array. So one byte is equivallent to one house.

Now each house will have an address. If someone want to post a gift to a particular house you need an address to that house. Similarly for writting / reading from memory you need the address to that particular byte. This address is held by the pointer.

Please check documentation folder in project below :