KEMBAR78
Function overloading and overriding | PPTX
Function Overloading and
Overriding
PRESENTED BY:
RAJAB ALI
Overloading Introduction
 One of the more powerful features for code readability and
usability is that of overloading.
 Like most things, it can be used for both good and evil.
 Function overloading is the availability of various functions within
a class that differ from each other in function signature i.e.
various functions share same name with different parameter
types or number of parameters.
rajabalicr7@gmail.com
Functions in C++
 A function in C++ is not uniquely identified by its name
alone.
 Each function has a function signature, consisting of two
elements.
 The name of the method
 The order and type of its parameters.
 Together, these act as the unique identifier for a C++
method.
 The following thus are two different functions:
 addTwo (int x, int y);
 addTwo (float x, float y);
rajabalicr7@gmail.com
Function Overloading
 The process of providing more than one functions with
the same name is called method overloading.
 We say that these functions have been overloaded.
 Overloading makes sure that we can provide a consistent
and clear interface to our methods regardless of the
parameters type.
 We don’t need addTwoInts and addTwoFloats, for example.
rajabalicr7@gmail.com
Function Overloading
 The compiler works out which of the methods to call
based on the parameters it is passed.
 It will check for the method that has a matching signature.
 It will execute that method only.
 If no matching signatures are found, a compile-time error
will be displayed.
rajabalicr7@gmail.com
Function Overloading
int add_nums (int one, int two) {
return one + two;
}
int add_nums (float one, float two) {
return (ceil (one + two));
}
int main() {
int answer_one, answer_two, answer_three;
answer_one = add_nums (1, 2); // Fine
answer_two = add_nums (1.0f, 2.0f); // Fine
answer_three = add_nums (1.0f, 2) // Error
}
rajabalicr7@gmail.com
Function Overriding
A function in child class overrides a function in parent
class if they have the same name and type signature.
 Classes in which functions are defined must be in a
parent-child relationship.
 Overloading deals with multiple functions in the same
class with the same name but different signatures
 Overriding deals with two functions, one in a parent class
and one in a child class, that have the same signature
rajabalicr7@gmail.com
Function Overriding
class Base
{
protected:
void myFunc() { cout<<"Base Class’ Function"; }
};
class Derived: public Base {
public:
void myFunc() { cout<<"Derived Class’ Function"; }
void myFunc(int a)
{
cout<<"Derived Class’ Function with Parameter
Value“<<a;
}
};
rajabalicr7@gmail.com
Function Overriding
rajabalicr7@gmail.com
Function Overriding
 To access the overridden function of base class from
derived class, scope resolution operator ::.
 Following statement is used in derived class to access
the base class get_data() function:
A::get_data; // Calling get_data() of class A.
rajabalicr7@gmail.com
Code
class A
{
public:
void fun()
{ cout << "n-> BASE <-n"; }
};
class B:public A {
public:
void fun()
{
A::fun();
cout << "-> DERIVED <-nn";
}
};
void main()
{
B b1;
b1.fun();
} rajabalicr7@gmail.com
Output
rajabalicr7@gmail.com
Code
class A
{
public:
void fun()
{ cout << "n-> BASE <-n"; }
};
class B:public A {
public:
void fun()
{
cout << "-> DERIVED <-nn";
}
};
void main()
{
A *a1;
B b1;
a1=&b1;
a1->fun();
}
rajabalicr7@gmail.com
Virtual function
class A
{
public:
virtual void fun()
{ cout << "n-> BASE <-n"; }
};
class B:public A {
public:
void fun()
{
cout << "-> DERIVED <-nn";
}
};
void main()
{
A *a1;
B b1;
a1=&b1;
a1->fun();
} rajabalicr7@gmail.com
Output
rajabalicr7@gmail.com
rajabalicr7@gmail.com

Function overloading and overriding

  • 1.
  • 2.
    Overloading Introduction  Oneof the more powerful features for code readability and usability is that of overloading.  Like most things, it can be used for both good and evil.  Function overloading is the availability of various functions within a class that differ from each other in function signature i.e. various functions share same name with different parameter types or number of parameters. rajabalicr7@gmail.com
  • 3.
    Functions in C++ A function in C++ is not uniquely identified by its name alone.  Each function has a function signature, consisting of two elements.  The name of the method  The order and type of its parameters.  Together, these act as the unique identifier for a C++ method.  The following thus are two different functions:  addTwo (int x, int y);  addTwo (float x, float y); rajabalicr7@gmail.com
  • 4.
    Function Overloading  Theprocess of providing more than one functions with the same name is called method overloading.  We say that these functions have been overloaded.  Overloading makes sure that we can provide a consistent and clear interface to our methods regardless of the parameters type.  We don’t need addTwoInts and addTwoFloats, for example. rajabalicr7@gmail.com
  • 5.
    Function Overloading  Thecompiler works out which of the methods to call based on the parameters it is passed.  It will check for the method that has a matching signature.  It will execute that method only.  If no matching signatures are found, a compile-time error will be displayed. rajabalicr7@gmail.com
  • 6.
    Function Overloading int add_nums(int one, int two) { return one + two; } int add_nums (float one, float two) { return (ceil (one + two)); } int main() { int answer_one, answer_two, answer_three; answer_one = add_nums (1, 2); // Fine answer_two = add_nums (1.0f, 2.0f); // Fine answer_three = add_nums (1.0f, 2) // Error } rajabalicr7@gmail.com
  • 7.
    Function Overriding A functionin child class overrides a function in parent class if they have the same name and type signature.  Classes in which functions are defined must be in a parent-child relationship.  Overloading deals with multiple functions in the same class with the same name but different signatures  Overriding deals with two functions, one in a parent class and one in a child class, that have the same signature rajabalicr7@gmail.com
  • 8.
    Function Overriding class Base { protected: voidmyFunc() { cout<<"Base Class’ Function"; } }; class Derived: public Base { public: void myFunc() { cout<<"Derived Class’ Function"; } void myFunc(int a) { cout<<"Derived Class’ Function with Parameter Value“<<a; } }; rajabalicr7@gmail.com
  • 9.
  • 10.
    Function Overriding  Toaccess the overridden function of base class from derived class, scope resolution operator ::.  Following statement is used in derived class to access the base class get_data() function: A::get_data; // Calling get_data() of class A. rajabalicr7@gmail.com
  • 11.
    Code class A { public: void fun() {cout << "n-> BASE <-n"; } }; class B:public A { public: void fun() { A::fun(); cout << "-> DERIVED <-nn"; } }; void main() { B b1; b1.fun(); } rajabalicr7@gmail.com
  • 12.
  • 13.
    Code class A { public: void fun() {cout << "n-> BASE <-n"; } }; class B:public A { public: void fun() { cout << "-> DERIVED <-nn"; } }; void main() { A *a1; B b1; a1=&b1; a1->fun(); } rajabalicr7@gmail.com
  • 14.
    Virtual function class A { public: virtualvoid fun() { cout << "n-> BASE <-n"; } }; class B:public A { public: void fun() { cout << "-> DERIVED <-nn"; } }; void main() { A *a1; B b1; a1=&b1; a1->fun(); } rajabalicr7@gmail.com
  • 15.
  • 16.