SlideShare a Scribd company logo
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

More Related Content

What's hot

Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vishal Patil
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
Control statements
Control statementsControl statements
Control statements
Kanwalpreet Kaur
 
Polymorphism In c++
Polymorphism In c++Polymorphism In c++
Polymorphism In c++
Vishesh Jha
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
Vraj Patel
 
Functions in C
Functions in CFunctions in C
Functions in C
Kamal Acharya
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
Mahender Boda
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 
C functions
C functionsC functions
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
Sanjit Shaw
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
Rabin BK
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
Prof. Dr. K. Adisesha
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
Venkata.Manish Reddy
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 

What's hot (20)

Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Control statements
Control statementsControl statements
Control statements
 
Polymorphism In c++
Polymorphism In c++Polymorphism In c++
Polymorphism In c++
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
C functions
C functionsC functions
C functions
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 

Viewers also liked

Function overloading
Function overloadingFunction overloading
Function overloading
Selvin Josy Bai Somu
 
Function overloading
Function overloadingFunction overloading
Function overloading
Jnyanaranjan Dash
 
Function Overlaoding
Function OverlaodingFunction Overlaoding
Function Overlaoding
Chandrakiran Satdeve
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
Learn By Watch
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKumar
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading Charndeep Sekhon
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding
Michael Heron
 
2CPP12 - Method Overriding
2CPP12 - Method Overriding2CPP12 - Method Overriding
2CPP12 - Method Overriding
Michael Heron
 
03 function overloading
03 function overloading03 function overloading
03 function overloading
Jasleen Kaur (Chandigarh University)
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
Ashita Agrawal
 
operator overloading in c++
operator overloading in c++operator overloading in c++
operator overloading in c++
harman kaur
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
 
Overloading
OverloadingOverloading
Overloading
Mukhtar_Hunzai
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
Nilesh Dalvi
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
sanya6900
 
3 Function Overloading
3 Function Overloading3 Function Overloading
3 Function Overloading
Praveen M Jigajinni
 
Inline functions & macros
Inline functions & macrosInline functions & macros
Inline functions & macros
Anand Kumar
 
Data types
Data typesData types
Data types
Syed Umair
 

Viewers also liked (20)

Function overloading
Function overloadingFunction overloading
Function overloading
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Function Overlaoding
Function OverlaodingFunction Overlaoding
Function Overlaoding
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding
 
2CPP12 - Method Overriding
2CPP12 - Method Overriding2CPP12 - Method Overriding
2CPP12 - Method Overriding
 
03 function overloading
03 function overloading03 function overloading
03 function overloading
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 
operator overloading in c++
operator overloading in c++operator overloading in c++
operator overloading in c++
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Overloading
OverloadingOverloading
Overloading
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
3 Function Overloading
3 Function Overloading3 Function Overloading
3 Function Overloading
 
Inline functions & macros
Inline functions & macrosInline functions & macros
Inline functions & macros
 
Data types
Data typesData types
Data types
 

Similar to Function overloading and overriding

Function overloading
Function overloadingFunction overloading
Function overloading
zindadili
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
ilias ahmed
 
Inheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptxInheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptx
MalligaarjunanN
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
Janu Jahnavi
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Pranali Chaudhari
 
Overloading
OverloadingOverloading
Overloading
Jaya Kumari
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
BalajiGovindan5
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argument
Suresh Mohta
 
Inheritance concepts Presentation (8).pptx
Inheritance concepts Presentation (8).pptxInheritance concepts Presentation (8).pptx
Inheritance concepts Presentation (8).pptx
ABHINAVARYANCSEA301
 
Polymorphism & Templates
Polymorphism & TemplatesPolymorphism & Templates
Polymorphism & Templates
Meghaj Mallick
 
function overloading
function overloadingfunction overloading
function overloading
AvaniNakum
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
sureshraj43
 
Advance oops concepts
Advance oops conceptsAdvance oops concepts
Advance oops concepts
Sangharsh agarwal
 
Object oriented programming C++
Object oriented programming C++Object oriented programming C++
Object oriented programming C++
AkshtaSuryawanshi
 
Functions
FunctionsFunctions
Functions
Pragnavi Erva
 
Chapter 13.1.6
Chapter 13.1.6Chapter 13.1.6
Chapter 13.1.6patcha535
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
Akash Gawali
 
Function overloading
Function overloadingFunction overloading
Function overloading
Sudeshna Biswas
 

Similar to Function overloading and overriding (20)

Function overloading
Function overloadingFunction overloading
Function overloading
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
C plusplus
C plusplusC plusplus
C plusplus
 
Inheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptxInheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptx
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Overloading
OverloadingOverloading
Overloading
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argument
 
My c++
My c++My c++
My c++
 
Inheritance concepts Presentation (8).pptx
Inheritance concepts Presentation (8).pptxInheritance concepts Presentation (8).pptx
Inheritance concepts Presentation (8).pptx
 
Polymorphism & Templates
Polymorphism & TemplatesPolymorphism & Templates
Polymorphism & Templates
 
function overloading
function overloadingfunction overloading
function overloading
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Advance oops concepts
Advance oops conceptsAdvance oops concepts
Advance oops concepts
 
Object oriented programming C++
Object oriented programming C++Object oriented programming C++
Object oriented programming C++
 
Functions
FunctionsFunctions
Functions
 
Chapter 13.1.6
Chapter 13.1.6Chapter 13.1.6
Chapter 13.1.6
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
 
Function overloading
Function overloadingFunction overloading
Function overloading
 

Recently uploaded

WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 

Recently uploaded (20)

WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 

Function overloading and overriding

  • 2. 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
  • 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  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
  • 5. 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
  • 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 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
  • 8. 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
  • 10. 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
  • 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
  • 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: 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