0% found this document useful (0 votes)
17 views11 pages

Chapter 4.

Note

Uploaded by

yug
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views11 pages

Chapter 4.

Note

Uploaded by

yug
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

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.

class Base int main() {


{ Derived derived1, derived2;
public: derived1.print();
void print() {
cout << "Base Function" <<endl; // access print() function of the Base class
} derived2.Base::print();
};
return 0;
class Derived : public Base { }
public:
void print() { Output: Derived Function
cout << "Derived Function" << endl; Base Function
//Base::print(); //to access the overridden function of
base class
}
};
Function Overriding using Virtual Function
• When a base class and derived class contain same member function, then the base version of the member function always works
when we invoke that function using base pointer.

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.

• They are mainly used to achieve Runtime polymorphism

• Functions are declared with a virtual keyword in base class.

• The resolving of function call is done at runtime.

Rules for Virtual Functions

1. Virtual functions cannot be static.

2. A virtual function can be a friend function of another class.

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 (?:).

Following are the operator cannot be overloaded by friend function:


= Assignment operator
( ) Function call operator
I l Subscripting operator
·> Class member access operator
Defining operator overloading
• To define an additional task to an operator, we must specify what it means in relation to the class to which the
operator is applied. This is done with the help of a special function, called operator function, Which describes
the task. The general form of an operator function is:
• Return-type classname :: operator op(arg-list)
{
function define; //task define
}
Op- overloading operator
Return-type: type of the value return by specified operation.
Operator function must be either member function(non-static) or friend function.
*Friend function have one argument for unary operator and two for binary operator.
*Member function has no argument for unary operator and one for binary operator.
Invoking of operator overloading:
op x or x op; // for unary operator
x op y ; //for binary operator

You might also like