Virtual Functions in C++ Explained With Simple Example

A virtual function is a member function which is defined in Base class and redefined by the derived class. “virtual” keyword must be added before the function declaration to define a function virtual. These functions gets inherited by Derived class from Base class and Derived class can choose to provide it’s own definition.
A pure virtual function is a virtual function which has no definition in Base class. Pure virtual function definition is used to ensure all derived class must override the base class function definition.

Normal syntax to define virtual function is as follows:

virtual int samplefun (int x, char c);
(more…)
Virtual Functions in C++ Explained With Simple Example Read More

C++ Casting Operators Explained With Simple Example

Type casting is the method to convert variable (or expression) of a type to another type. Casting operators are special operators which are used to convert variable of one data type to another data type. Type casting can be done in two ways:

Implicit casting is being done automatically where values are copied to another compatible type. Eg: int to long int or float etc.

Explicit casting is done specifically by the programmer. In C++ there are four types of casting operator available.

(more…)
C++ Casting Operators Explained With Simple Example Read More

reinterpret_cast Casting Operator Explained With Simple Example

This is one of the most complex and dangerous casting operators available in C++. The “reinterpret_cast” operator can convert any type of variable to fundamentally different type. This cast operator can convert an integer to a pointer and so on. This cast operator can also convert variable into totally incompatible type too.

Normal syntax to do reinterpret_cast is as follows:

reinterpret_cast <target-type> (expr)

target-type is the target of the cast whereas expr is being cast into the new target-type.

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

static_cast Casting Operator Explained With Simple Example

This is the simplest type casting operator available in C++. The “static_cast” operator performs a normal cast. This casting operator is basically a substitute for normal casting operator. “static_cast” operator doesn’t do any runtime checks. Hence programmer should consider whether casting is applicable or not.

Normal syntax to do static_cast is as follows:

static_cast <target-type> (expr)

target-type is the target of the cast whereas expr is being cast into the new target-type.

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

const_cast Casting Operator Explained With Simple Example

This is one of the most dangerous casting operators. The “const_cast” operator is used to remove the const or volatile property of a variable. The target-type and source-type must be of the same type here.

Normal syntax to do const_cast is as follows:

const_cast <target-type> (expr)

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

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

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

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

Exception Handling In C++ Explained With Simple Example

Exception Handling in C++ allows a programmer to handle run time errors in an orderly fashion. Using this routine, an error handling function can be invoked which can take some corrective action to avoid system crash or to recover the system from errors.
Exception Handling in C++ is built using three keywords – try, catch and throw.
In general, all the code which might throw some error put into try block and expected errors are caught using catch block.
If an error occurred in try block then that error is thrown (error can be thrown explicitly by calling throw also) which will be caught by relevant catch block.

(more…)
Exception Handling In C++ Explained With Simple Example Read More