Chapter 4.
Chapter 4.
FUNCTION OVERRIDDING
When derived class inherited the features of base class. Suppose, the same function is defined in both the derived class and the
based class. Now if we call this function using the object of the derived class, the function of the derived class is executed. This
is known as function overriding in C++. The function in derived class overrides the function in base class.
Ex:
class B
{
public:
void show()
{
cout<<"I am in base show"<<endl;
}
};
class D:public B
{
public:
void show()
{
Cout<<"I am in derived show"<<endl;
}
};
Note: Here the base version of function show () will work as it overrides the derived version of show ().
VIRTUAL FUNCTION
• A virtual function is a member function which is declared within a base class and is re-defined (overridden) by a derived class. When you
refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the
derived class’s version of the function.
• Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for function call.
3. Virtual functions should be accessed using pointer or reference of base class type to achieve runtime polymorphism.
4. The prototype of virtual functions should be the same in the base as well as derived class.
5. They are always defined in the base class and overridden in a derived class. It is not mandatory for the derived class to override (or re-define
the virtual function), in that case, the base class version of the function is used.
6. A class may have virtual destructor but it cannot have a virtual constructor.
VIRTUAL FUNCTION:
• Late binding (Runtime) is done in accordance with the content of pointer (i.e. location pointed to by pointer)
and Early binding / static binding/static linking/compile time polymorphism(Compile time) is done according
to the type of pointer, Object is bound to its function call during compile time. Here compiler is able to select
the appropriate function for a particular call at the compile time itself.
Important Tips!
A base pointer can be made to point to any number of derived objects, it cannot access the members defined by a
derived class. It can access only the members which are common to the base class. If a same function is present in
both base and derived class, always base version of the function is called when we access the function using base
pointer (no matters whether it points to base class or derived class). Derived version of the function can be called
by making the function (having same name) as virtual. This is also called function overriding because function
in the base class is overridden by the function in the derived class.
Pure Virtual Function
A pure virtual function is a virtual function that has no definition within the base class. To declare a pure
virtual function, use this general form:
virtual return-type function-name (parameter-list) = 0;
When a virtual function is made pure, any derived class must provide its own definition. If the derived class fails to override the
pure virtual function, a compile-time error will result. Hence definition for pure virtual function must be there in the derived
class.
Abstract Classes
A class that contains at least one pure virtual function is said to be abstract. Because an abstract class contains one
or more functions for which there is no definition (that is, a pure virtual function), no objects of an abstract class may
be created.
Instead, an abstract class constitutes an incomplete type that is used as a foundation for derived classes. Although
we cannot create objects of an abstract class, we can create pointers and references to an abstract class. This allows
abstract classes to support run-time polymorphism, which relies upon base-class pointers and references to select
the proper virtual function.
This pointer
Every object in C++ has access to its own address through an important pointer called this pointer. The this pointer is an implicit
parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object.
Friend functions do not have a this pointer, because friends are not members of a class. Only non static member functions
have a this pointer.
Operator Overloading
• It is most striking feature of C++.
• In operator overloading an operator can be operated on user defined data types. i.e. + operator perform addition of integers or
real numbers. But we can overload this operator to compute sum of two complex number.
• Only existing operators can be overloaded. New operators cannot be overloaded (i.e. $ cannot be overloaded as it is not an
operator.)
• It should obey the basic meaning of an operator i.e. + operator cannot be used to subtract two numbers.
Following operators cannot be overloaded:
1. Class member access operators(.,.*).
2. Scope resolution operator (::).
3. Size operator (sizeof).
4. Conditional operator (?:).