TOPIC: FUNCTION OVERLOADING

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Chapter 4 Summation.
General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009.
File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object.
Templates Overload function: define more than one function With same function name Different parameter type Different type Different number of parameter.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
GE 211 Function in C++ Dr. Ahmed Telba Math Library Functions Defining a Function: The general form of a C++ function definition is as follows:
First steps Jordi Cortadella Department of Computer Science.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Week 2. Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
1 Remember: Examination is a chance not ability. By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL.
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
Lecture 19 CIS 208 Wednesday, April 06, Welcome to C++ Basic program style and I/O Class Creation Templates.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
LESSON 2 Basic of C++.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Overloading C++ supports the concept of overloading Two main types
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Topic Pre-processor cout To output a message.
LESSON 2 Basic of C++.
Function Topic 4.
Command Line Arguments
Chapter 5 Function Basics
Functions and an Introduction to Recursion
C# - Inheritance and Polymorphism
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming fundamentals 2 Chapter 2:Function
User-defined Functions
CSC1201: Programming Language 2
Chapter 5 Function Basics
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Name: Rubaisha Rajpoot
User-defined Functions
COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Counting Loops.
Starting Out with C++: From Control Structures through Objects
More ‘concepts’ on Function
Pass by Reference.
CS150 Introduction to Computer Science 1
Functions and an Introduction to Recursion
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
CHAPTER 2 Arrays and Vectors.
CSC1201: Programming Language 2
CHAPTER 2 Arrays and Vectors.
Programming fundamentals 2 Chapter 1: Functions (Sub-Algorithms)
Functions Imran Rashid CTO at ManiWeber Technologies.
CS1201: Programming Language 2
Class: Special Topics Overloading (methods) Copy Constructors
More ‘concepts’ on Function
Presentation transcript:

TOPIC: FUNCTION OVERLOADING

FUNCTION OVERLOADING: C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively.

STATEMENT 1: You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You can not overload function declarations that differ only by return type.

STATEMENT 2: An overloaded declaration is a declaration that had been declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition (implementation).

Ways to overload a function By changing number of Arguments. By having different types of argument.

Number of Arguments different int sum (int x, int y) { cout << x+y; } int sum(int x, int y, int z) cout << x+y+z; int main() { sum (10,20); // sum() with 2 parameter will be called sum(10,20,30); //sum() with 3 parameter will be called }

Different Datatype of Arguments int sum(int x,int y) { cout<< x+y; } double sum(double x,double y) cout << x+y; int main() { sum (10,20); sum(10.5,20.5); }

EXAMPLE 1:(without class and object) Following is the example where same function calc() is being used

ANS CODE: #include<iostream.h> #include<conio.h> int calc (int); int calc (int,int); int main() { int s,a,b; Cout<<“Enter a number:” Cin>>s; Cout<<“square is:”<<calc(s); Cout<<“Enter two numbers:” cin>>a>>b; Cout<<“addition is:”<<cal(a,b); return 0]; getch(); int calc (int x) { return(x*x); } int calc (int x,int y) return(x+y)

EXAMPLE 2:(using class and object) Following is the example where same function print() is being used to print different data types:

ANS CODE: include <iostream> using namespace std; class printData { public: void print(int i) cout << "Printing int: " << i << endl; } void print(double f) cout << "Printing float: " << f << endl; void print(char* c) { cout << "Printing character: " << c << endl; }; int main(void) { printData pd; // Call print to print integer pd.print(5); // Call print to print float pd.print(500.263); // Call print to print character pd.print("Hello C++"); return 0; }

OUTPUT: When the above code is compiled and executed, it produces the following result: Printing int: 5 Printing float: 500.263 Printing character: Hello C++

END