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.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
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.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
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
Chapter 7: User-Defined Functions II
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
C++ Operator Overloading Gordon College CPS212 Gordon College CPS212.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator Overloading in C++
Review of C++ Programming Part II Sheng-Fang Huang.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Pointer Data Type and Pointer Variables
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Case Study - Fractions Timothy Budd Oregon State University.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Learners Support Publications Classes and Objects.
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+
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
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.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
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.
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
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Functions, Scope, and The Free Store Functions Functions must be declared by a function prototype before they are invoked, return_type Function_name(type,
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.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
Structs and Classes Structs A struct can be used to define a data structure type as follows: struct Complex { double real, imag;} // specifying a Complex.
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
By Muhammad Waris Zargar
Overloading Operator MySting Example
CISC181 Introduction to Computer Science Dr
Class: Special Topics Copy Constructors Static members Friends this
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Pass by Reference, const, readonly, struct
Overloading the << operator
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
9-10 Classes: A Deeper Look.
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Operator Overloading; String and Array Objects
Review of Function Overloading
Overloading the << operator
9-10 Classes: A Deeper Look.
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); 

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; }

“this” Pointers A this pointer is a special pointer that is automatically passed to a member function when it is called It is a pointer to the object that calls the member function It is useful for indirect (backdoor) access of member variables and functions, e.g. dVector::dVector(int n1, int n2) { this->n1 = n1;this->n2 = n2; It is also useful for operator overloading and returning objects from member functions, e.g. return *this;

Static Variables Local static variables are not destroyed when the function returns Similar to global variables but has the advantage of not being accessible to other functions This is useful for initializing parameters in a function or giving a function memory

Static Variables Global static variables are not accessible to functions in other files Useful for restricting the access of global variables to only one file Static member variables result in only one copy of the variable that is shared by all the objects of that type Useful for communication between objects or counting the number of objects currently being used

Extern Statement The extern statement is needed to access global variables in other files, e.g. File #1: double global1 = 7.7; // original definition has no extern File #2: extern double global1; // use extern in other files cout << global1;  7.7; Try to avoid the use of global variables when using C++

Const Variables const variables cannot be changed in the program, e.g. const double pi=3.14; pi = 1.0; // not allowed void f1(const double x) { x = 1.0; } // not allowed void f2(const double *x) { *x = 2.0; } // not allowed void f3(const double &x) { x = 2.0; } // not allowed Not that useful for protecting objects with dynamic memory passed by reference such as dVector