What is Operator Overloading in C++ ?
The ability to overload operators is one of the most powerful features of C++. The operators are overloaded so that they can perform special operations on the user defined classes. Say for example you create a class representing complex number. We can overload the + operator to add two complex numbers. When an operator is […]
Read the rest of this entry »What are Friend Functions and Friend Classes in C++ ?
A friend can be a function, another class or individual member function of a class. With the help of a friend function it is possible to grant a non member function access to the private members of a class using a friend.
A friend function has all access to all the private and protected members of […]
What are Constructors and Destructors in C++ ?
A constructor constructs an object i.e. it initializes the objects internal data members as well as it may allocate resource (memory, files, sockets, etc). The constructor has the same name as the class name and cannot have a return type not even void.
A constructor can take multiple arguments as its parameters. A class can have […]
What is Inheritance ? How is a Class Inherited in C++ ?
Inheritance is one of the most important principles of object oriented language. Using inheritance one can create a class that can have traits common to a set of related items.
This class is then inherited by other classes each adding things are specific to them. The class that is inherited is called the base class and […]
What are Virtual Functions ? Why do we need Virtual Functions? Explain with an example.
A function that is defined in a base class but overridden by the derived class is called a virtual function.
To create virtual functions precede the function declaration in the base class with the keyword virtual. When a class containing a virtual function is inherited the derived class overrides the virtual function with its own definition. […]
What are Templates in C++ ? How are Templates declared and used ?
Many data structures and algorithms can be defined independently of the type of data they manipulate.
A template allows the separation of the type dependent part from the type independent part. This results in a significant amount of code reusability. Templates can be class templates or function templates. A class template is used for creating a […]
What is Polymorphism ? How is Polymorphism achieved in C++ ?
Polymorphism is one of the most important principle of object oriented language. Polymorphism means many forms i.e. one interface multiple methods. The specific method to call depends on the nature of the situation. No matter what type of car it is the driving mechanism is the same.
For example, you might have a program that defines […]
What are the Standard Exceptions in C++ ?
C++ defined a hierarchy of standard exceptions that are thrown at runtime whenever an abnormal condition takes place.
These exceptions are derived from std::exception class defined in the header. Since there is a common base class of all these exceptions it enables the application to catch these exceptions in a single catch statement. E.g.
catch (std::exception &e)
{
//code […]
What is Multiple Inheritance in C++ ? Explain with an example
It is possible for a derived class to inherit more than a single base class. This is referred as multiple inheritance. The class inherits from multiple base classes where these classes are comma separated. Each listed base class must specify its own access level.
E.g.
class base1
{
};
class base2
{
};
class derived: public base1, public base2
{
};
The base class constructors are […]
What is a Copy Constructor in C++ ? Give an example
Copy constructor is a type of constructor which constructs an object by copying the state from another object of the same class. Whenever an object is copied, another object is created and in this process the copy constructor gets called. If the class of the object being copied is x, the copy constructor’s signature is […]
Read the rest of this entry »
