Review of Function Overloading

Slides:



Advertisements
Similar presentations
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Advertisements

Operator overloading redefine the operations of operators
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
I NTRODUCING O PERATOR O VERLOADING Chapter 6 Department of CSE, BUET 1.
Introduction to Programming Lecture 31. Operator Overloading.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
EC-241 Object-Oriented Programming
Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
Operator Overloading Fundamentals
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator overloading Object Oriented Programming.
Review of C++ Programming Part II Sheng-Fang Huang.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Case Study - Fractions Timothy Budd Oregon State University.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Operator Overloading Like most languages, C++ supports a set of operators for its built-in types. Example: int x=2+3; // x=5 However, most concepts for.
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+
CS 11 C++ track: lecture 5 Today: Member initialization lists Linked lists friend functions.
J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Templates Class Templates Used to specify generic class types where class members data types can be specified as parameters, e.g. here is a generic List.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
CONSTRUCTOR AND DESTRUCTORS
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
 Binary operators  Unary operators  Conversion Operators  Proxy Classes (simulating a reference) ▪ bitset example  Special operators  Indexing 
CS410 – Software Engineering Lecture #12: C++ Basics V
Yan Shi CS/SE 2630 Lecture Notes
Overloading C++ supports the concept of overloading Two main types
Procedural and Object-Oriented Programming
Pointers and Dynamic Arrays
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Overloading Operator MySting Example
Object-Oriented Design (OOD) and C++
CISC181 Introduction to Computer Science Dr
Jim Fawcett Copyright ©
group work #hifiTeam
Operator Overloading
Overloading the << operator
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.
Variables and Their scope
Overview of C++ Overloading
Operator Overloading.
Introduction to Programming
File I/O.
Java Programming Language
Recitation Course 0603 Speaker: Liu Yu-Jiun.
IT533 Adapted from Textbook (SAVITCH) and Lecture Notes (PUSATLI), 2014.
const A& B::blah (const C& c) const {...} Passed in a reference to a constant object ‘c’  ‘c’ cannot be modified in the function const A& B::blah.
Lists CMSC 202, Version 4/02.
Overloading the << operator
Jim Fawcett Copyright ©
Presentation transcript:

Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int x) { return x*x; } double sqr(double x) { return x*x; } double sqr(double x, double y) { return x*y; } int i = 2; double x = 1.2, y = 2.0; cout << sqr(i) << “ ” << sqr(x) << “ “ << sqr(x,y); 4 1.44 2.4

Operator Overloading This is a powerful C++ feature that allows the use of operators for user defined classes, e.g. dVector A, B, C, D; // a user defined class C = A + B; // = and + is overloaded D = A*B; // * is overloaded (dot product) cout << D; // << is overloaded Similar to function overloading except it uses operators

Member Operator Functions General form: return-type class-name::operator#(argument-list) { // operation to be performed } e.g. void dVector::operator=(dVector &v2) { // v1 = v2 int i; if ( n1 != v2.n1 || n2 != v2.n2 ) return; // error ! for ( i = n1; i <= n2; i++ ) e(i) = v2.e(i); // copy elements }

Improved = Operator dVector dVector::operator=(dVector &v2) { // v1 = v2 int i; dVector *ans; // declare a return variable ans = new dVector(n1,n2); // allocate a dVector if ( n1 != v2.n1 || n2 != v2.n2 ) return *ans; // error ! for ( i = n1; i <= n2; i++ ) ans->e(i) = e(i) = v2.e(i); // copy elements return *ans; }

Overloading the + Operator dVector dVector::operator+(dVector &v2) { // v1 + v2 int i; dVector *ans; // declare a return variable ans = new dVector(n1,n2); // dynamically allocate if ( n1 != v2.n1 || n2 != v2.n2 ) return *ans; // error ! for ( i = n1; i <= n2; i++ ) ans->e(i) = e(i) + v2.e(i); // copy elements return *ans; }

Overloading Unary Operators Example: Norm operator double dVector::operator!() { int i; double ans=0.0; // declare a return variable for ( i = n1; i <= n2; i++ ) ans += e(i)*e(i); return sqrt(ans); }

Friend Functions A Friend function is not a member function of a class, but it has access to all its private members, e.g. class number { double x; // private member public: number() { x = 7.11; } // constructor friend void show_x(number n); }; void show_x(number n) { cout << n.x; } number n1; show_x(n1);  7.11;

<< Operator Overloading class dVector { friend ostream &operator<<(ostream &stream, dVector &v); … }; ostream &operator<<(ostream &stream, dVector &v) { // example usage: cout << v1 << v2 << v3; int i; for ( i = v.n1; i <= v.n2; i++ ) stream << "\n" << v.e(i); return stream; }