of functions, methods & operators

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Operator Overloading Fundamentals
Class and Objects.
CS-240 Operator Overloading Dick Steflik. Operator Overloading What is it? –assigning a new meaning to a specific operator when used in the context of.
Chapter 14: Overloading and Templates
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
Constructors & Destructors Review CS 308 – Data Structures.
CMSC 2021 Stream I/O Operators and Friend Functions.
1 Overloading functions C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define.
Operator overloading Object Oriented Programming.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 14. User Defined Classes.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R1. ADTs as Classes.
Introduction to Classes in C++
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Programming in C++ Michal Brabec Petr Malý. Class / Struct Class / Struct consists of: data members function members constructors destructor copy constructor.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
Const Member Functions Which are read-only? //fraction.h... class Fraction { public: void readin(); void print();
Friend Function. 2 Any data which is declared private inside a class is not accessible from outside the class. A non-member function cannot have an access.
Constructors Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction.
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
Methods.
C is a high level language (HLL)
1 Classes struct Public and Private Parts of a struct Class Scope of a Class Overloading Member Functions Class in a Class Static Members of Classes this.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Functions CMSC 202. Topics Covered Function review More on variable scope Call-by-reference parameters Call-by-value parameters Function overloading Default.
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 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
CMSC 202 Lesson 9 Classes III. Warmup Using the following part of a class, implement the Sharpen() method, it removes 1 from the length: class Pencil.
Operator Overloading.
Regarding assignment 1 Style standards Program organization
Pointer to an Object Can define a pointer to an object:
Operator Overloading Ritika Sharma.
Procedural and Object-Oriented Programming
CSCE 210 Data Structures and Algorithms
Access Functions and Friend Functions
Review What is an object? What is a class?
A First C++ Class – a Circle
Concepts of Constructors and Its Types
14.4 Copy Constructors.
Chapter 15: Overloading and Templates
Operator Overloading BCA Sem III K.I.R.A.S.
Global & Local Identifiers
The dirty secrets of objects
Function Overloading C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define similar.
Functions.
CMSC 202 Lesson 22 Templates I.
Operator Overloading.
COP 3330 Object-oriented Programming in C++
Templates I CMSC 202.
CMSC 202 Lesson 6 Functions II.
Object Oriented programming
Objects as Function Arguments
Chapter 11 Classes.
Presentation transcript:

of functions, methods & operators Overloading of functions, methods & operators

Overloading Definition: Declaring two or more operations for a single function, method or operator, depending on the number and data types of the arguments or operands.

Overloading For functions and methods: Two or more functions/methods with the same name/class but that differ by number, order and data type of arguments.

Overloading Purpose: - to give options to programmers using the code, often defaults. - to define the action of an operator on objects.

Overloaded Functions Examples: functions // prototypes void print(float f); void print(float f, int ndpdp); void print(float f, int ndpdp, int width);

Overloaded Functions Examples: functions // print a float number, no formatting void print(float f) { cout << f; }

Overloaded Functions Examples: functions // print a float number with precision void print(float f, int ndpdp) { cout << fixed << setprecision(ndpdp); cout << f; }

Overloaded Functions Examples: functions // print with precision and in field width void print(float f, int ndpdp, int width) { cout << fixed << setprecision(ndpdp); cout << setw(width) << f; }

Overloaded Functions Examples: functions const float pi = 3.1415926; void main() { print(pi); // no formatting print(pi, 3); // 3 past dec point print(pi, 3, 10); // 3 pdp and field of } // width 10

Overloaded Functions Examples: functions - using defaults const int DEF_NDPDP = 2; const int DEF_WIDTH = 10;

Overloaded Functions Examples: functions - using defaults // use default precision and width void print(float f) { cout << fixed << setprecision(DEF_NDPDP); cout << setw(DEF_WIDTH) << f; }

Overloaded Functions Examples: functions - using defaults // use default width void print(float f, int ndpdp) { cout << fixed << setprecision(ndpdp); cout << setw(DEF_WIDTH) << f; }

Overloaded Functions Examples: functions - using (no) defaults // using no defaults void print(float f, int ndpdp, int width) { cout << fixed << setprecision(ndpdp); cout << setw(width) << f; }

Overloaded Functions Examples: functions - using defaults, even better // invoke "no defaults" function with defaults void print(float f) { print(f, DEF_NDPDP, DEF_WIDTH); } void print(float f, int ndpdp) { print(f, ndpdp, DEF_WIDTH);

Overloaded Methods The exact same idea as with functions, but in a class class fraction { private: int numer, int denom; public: void set(int n); // def denom=1 void set(int n, int d); };

Overloaded Methods void fraction::set(int n, int d) { numer = n; denom = d; } void fraction::set(int n) { set(n,1); // invoke no-def overloaded } // method with default value

Overloaded Methods void main() { fraction x,y; // both constr. to 0/1 x.set(1,2); // numer=1, denom=2 y.set(3); // numer=3, denom=1 }

Overloaded Operators Some operators we've used are already Overloaded: int a = 2 + 3; // + adds two ints float b = 2.5 + 3.5; // + adds two floats float e = 2.5 + 3; // + adds int to float string c = "Hello"; // + concatenates string d = c + " World"; // two strings

Overloaded Operators How can we overload + to add two fractions, when WE created class fraction? fraction a, b, c; a.set(1,2); b.set(1,3); c = a + b; // compiler error!!! cout << c.getNumer() << "/" // want it to << c.getDenom() << endl; // print 5/6

Overloaded Operators Syntax: retType operator op (type a1, type arg2) { ... return value/variable/object; }

Overloaded Operators // how to add two fractions, resulting in a fraction fraction operator + (fraction a, fraction b) { fraction c; int denom = a.getDenom() * b.getDenom(); int numer = (a.getNumer() * b.getDenom()) + (b.getNumer() * a.getDenom()); c.set(numer, denom); return c; }

Overloaded Operators Overloaded operators are NOT part of a Class and so do not have access to private members/methods However, they are commonly coded in the class Implementation, and prototyped in the class Interface after the class interface declaration.

Overloaded Operators class fraction { private: int numer, int denom; public: void set(int n); void set(int n, int d); int getNumer(); int getDenom(); }; fraction operator + (fraction a, fraction b);

Overloaded Operators void fraction::set(int n, int d) {numer=n; denom=d;} void fraction::set(int n) { set(n,1); } int fraction:: getNumer() { return numer; } int fraction:: getDenom() { return denom; } fraction operator + (fraction a, fraction b) { fraction c; ... return c; }

Overloaded Operators Overloaded operators are NOT part of a Class and so do not have access to private members/methods UNLESS they are FRIENDS

Friend of a class Definition: A function, other class, or overloaded operator that has access to the private members of a class.

Friend of a class Syntax: class classname { friend function prototype; friend class otherclassname; friend overloaded operator prototype; public: private: };

Friend of a class Example: class fraction { friend void main(); friend class fractionList; friend fraction operator + (fraction a, fraction b) ; public: private: };

Friend of a class // rewritten as a friend of class fraction fraction operator + (fraction a, fraction b) { fraction c; c.denom = a.denom * b.denom; c.numer = (a.numer * b.denom) + (b.numer * a.denom); return c; }

Vocabulary Overloading Term Definition Overloading Declaring two or more operations for a single function, method or operator, depending on the number and data types of the arguments or operands. Overloaded Function/Method Two or more functions/methods with the same name but with arguments that differ by number, type and order. Overloaded Operator An operator that can operate on two or more types of operands. Friend A function, other class, or overloaded operator that has access to the private members of a class.