dynamic_cast Casting Operator Explained With Simple Example

This is one of the most important casting operators. The “dynamic_cast” performs a run-time type casting which also checks the type casting validity. If type casting is done to compatible type then it succeeds else it will throw “bad_cast” exception.

Normal syntax to do dynamic_cast is as follows:

dynamic_cast <target-type> (expr)

where target-type and expr must be of type pointer or reference.

(more…)
dynamic_cast Casting Operator Explained With Simple Example Read More

Replace Character of String With Another Character N Place Down the Alphabet

Given a string ‘s’ and a number ‘n’, write a function that returns a string with each character in s replaced with another character that is ‘n’ positions down the alphabet.

If n is 1, A is replaced with B, B is replaced with C, Z is replaced with A and so on.

If n is 2, A is replaced with C, B is replaced with D, Z is replaced with B and so on.

(more…)
Replace Character of String With Another Character N Place Down the Alphabet Read More

Friend Function Explained With Simple Example

A Friend function is a function defined outside the class, but it has access to all private and protected members of the class. To declare a friend function, it’s prototype must be declared inside the class, preceding it with keyword “friend”. For eg:

class Demo
{
    private:
        int m;
        int y;

    public:
        friend int sum (Demo d);
        void print_val ();
        Demo (int m, int y);
};
(more…)
Friend Function Explained With Simple Example Read More

Inline Function Explained With Simple Example

Inline function is an important addition in C++. These inline functions mostly are not called and is expanded in line at the invocation place. Hence, these functions are called inline functions. To define a function as inline function, precede function definition with “inline” keyword. These functions are almost similar to Macros in C.
For eg:

inline int sum (int a, int b);
(more…)
Inline Function Explained With Simple Example Read More

Monostate Design Pattern explained with simple example

Monostate design pattern is a singleton design pattern variation in which a class will act like a singleton but this class will look like a normal class. This design pattern states that all data member of monostate classes are static but any number of instances can be created in the program.

Before going ahead have a look at Design pattern simplified version and Singleton design pattern.

(more…)
Monostate Design Pattern explained with simple example Read More

Dependency Inversion Principle explained with simple example

Dependency Inversion Principle (DIP) is a software design principle which states that “High-level modules should not depend on low-level modules. Both should depend on abstractions And Abstractions should not depend on details. Details should depend on abstractions“. This design principle ensures a lower coupling between different classes.

Before going ahead we should know why do we need software design principle and what is software design principle.

(more…)
Dependency Inversion Principle explained with simple example Read More

Interface Segregation Principle explained with simple example

Interface Segregation Principle (ISP) is a software design principle which states that “many client specific interfaces are better than one generic interface“. This principle enforces to implement only usable methods which will reduce coupling between modules. This principle ensures that any client should be dependent only on those methods which they use.

Before going ahead we should know why do we need software design principle and what is software design principle.

(more…)
Interface Segregation Principle explained with simple example Read More

Liskov Substitution Principle explained with simple example

Liskov Substitution Principle (LSP) is a software design principle which states that “derived class objects should be completely replaceable by their base classes“. This principle ensures that any derived class extending base class will not change their behaviour.

Before going ahead we should know why do we need software design principle and what is software design principle.

(more…)
Liskov Substitution Principle explained with simple example Read More

Open/Closed design principle explained with simple example

Open/Closed design principle is a software design principle which states that every module, class or function of a software should be open for extension but closed for modification. This design principle ensures any new functionality can be added via new classes which means minimum code changes required in existing classes.

Before going ahead we should know why do we need software design principle and what is software design principle.

(more…)
Open/Closed design principle explained with simple example Read More