What Is Function Overriding?: Directly

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

What is Function Overriding?

• The process of defining / declaring member function of the parent class in


the child class with Same Name and Same Signature as in the parent class.
• When the member function of parent class is overridden in the child class,
then the object of child class cannot Directly access the function of parent
class.
• Scope resolution operator :: will be used to access this function of the
parent class.
• In function overriding, the function in parent class is called the overridden
function and function in child class is called overriding function.
• Function overriding is a feature that allows us to have a same function in
child class which is already present in the parent class. A child class inherits
the data members and member functions of parent class, but when you
want to override a functionality in the child class then you can use function
overriding. It is like creating a new version of an old function, in the child
class.
#include <iostream> void mul()
using namespace std; { if(a<=0 || b<=0)
#include <conio.h>
class Simple
cout<<"Invalid Values"<<endl;
{ else
protected: Simple::mul();
int a,b; }
public: void div()
Simple() { a=b=0; cout<<"construtor of parent"<<endl; } { if(a<=0 || b<=0)
void Set() { cout<<"Enter value of a ="; cin>>a;
cout<<"Enter value of b ="; cin>>b; } cout<<"Invalid Values"<<endl;
void add() { cout<<"Add value of a and b="<< a+b<<endl;} else
Simple::div();
void sub() { cout<<"Subtract value of b from a="<< a-b<<endl;} }

void mul() { cout<<"Multiply value of a and b="<< a*b<<endl;}


}; // end of child class Complex

void div() { cout<<"Devide value of a by b="<< a/b<<endl;}


int main()
{
}; Complex Obj;
class Complex : public Simple // first constructor of parent class will be caleed then constructor of child class will be called
{
// what about call of destructors? In reverse order?
public:
Complex() { cout<<"construtor of child";} Obj.add();
void add() { if(a<=0 || b<=0) Obj.Set();
cout<<"Invalid Values"<<endl;
else Obj.add();
Simple::add(); Obj.sub();
}
Obj.mul();
void sub() { if(a<=0 || b<=0)
cout<<"Invalid Values"<<endl; Obj.div();
else
Simple::sub(); return 0;
} }
Difference between Function Overloading and Function overriding in C++
• Function overloading and Function overriding both are examples
of polymorphism but they are completely different.
• Function overloading is a feature that allows us to have same function more than
once in a program. Overloaded functions have same name but their signature must
be different. Where as Function overriding is a feature of OOPs Programming that
allows us to override a function of parent class in child class.
1) Function Overloading happens in the same class when we declare same functions with
different arguments in the same class. Function Overriding is happens in the child class when child
class overrides parent class function.
2) In function overloading function signature should be different for all the overloaded functions.
In function overriding the signature of both the functions (overriding function and overridden function)
should be same.
3) Overloading happens at the compile time thats why it is also known as compile time
polymorphism while overriding happens at run time which is why it is known as run time
polymorphism.
4) In function overloading we can have any number of overloaded functions. In function
overriding we can have only one overriding function in the child class.
// C++ Access Overridden Function to the Base Class
// C++ program to access overridden function // in main() using the scope resolution operator ::

#include <iostream>
using namespace std;
class Base
{ public:
void print() { cout << "Base Function" << endl; }
};
// OUTPUT
class Derived : public Base
Derived Function
{ public: Base Function
void print() { cout << "Derived Function" << endl; }
};
int main()
{ Derived derived1, derived2;
derived1.print();

// access print() function of the Base class with the object of derived class with the help of scope resolution operator ::
derived2.Base::print();
return 0;
}
// C++ program to access overridden function using pointer int main()
// of Base type that points to an object of Derived class {
#include <iostream> Derived derived1;
using namespace std;
class Base // pointer of Base type that points to derived1
{ public: Base* ptr = &derived1;
void print() { cout << "Base Function" << endl; }
}; // call function of Base class using ptr
class Derived : public Base
{ ptr->print();
public: return 0;
void print() { cout << "Derived Function" << endl; } }
};

Output
Base Function

// This is because even though ptr points to a Derived object,


it is actually of Base type. So, it calls the member function
of Base.
In order to override the Base function instead of accessing it,
we need to use virtual functions in the Base class.

You might also like