Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Operator overloading redefine the operations of operators
Lecture 14 Today: Overloading: Revision on this Revision on increment operators the assignment operator the [] operator Book: p , 215,
For(int i = 1; i
Starting Out with C++, 3 rd Edition 1 Chapter 14 – More About Classes.
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
void count_down (int count) { for(i=count; i>1; i--) printf(" %d\t", count); } printf("A%d\n", count); if(count>1) count_down(count-1); printf("B%d\n",
Functions Prototypes, parameter passing, return values, activation frams.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Template Implicit function overload. Function overload Function overload double ssqq(double & a, double & b) { return(a*b);} float ssqq(float & a, float.
1 Data Structures - CSCI 102 CS102 C++ Operator Overloading Prof Tejada.
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.
1 Chapter 11 Introducing the Class Pages ( )
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
EC-241 Object-Oriented Programming
Operator Overloading Fundamentals
C++ Classes & Data Abstraction
Win32 Programming Lesson 4: Classes and Structures.
Reusable Classes.  Motivation: Write less code!
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
CS150 Introduction to Computer Science 1
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Operator overloading Object Oriented Programming.
1 CSC241: Object Oriented Programming Lecture No 07.
Inheritance #1 First questions Similar to Python? What about visibility and encapsulation? – can an object of the child class access private members.
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.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
1 Chapter 8 Operator Overloading. 2 User-defined Meaning for “built-in” Operators zNatural, suggestive usage yComplex number addition, subtraction, etc.
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.
Chapter 8 Operator Overloading.  Operator overloading is considered one of the more useful techniques for improving readability and ease of programming.
OPERATOR OVERLOADING Customised behaviour of operators.
CONSTRUCTOR AND DESTRUCTORS
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
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.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
1 CSC241: Object Oriented Programming Lecture No 08.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
MAITRAYEE MUKERJI Object Oriented Programming in C++
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Overloading Unary Operators
Ref: Sebesta, Chapter 12; Lafore, Chapter 11
Operator Overloading Ritika Sharma.
Chapter 9 Type Conversions
Operator Overloading.
Object-Oriented Programming (OOP) Lecture No. 21
Objectives Define polymorphism Overload functions
Repetition Statements
Unary Operators ++ and --
Introduction to Programming
Starting Out with C++: From Control Structures through Objects
Operator Overloading.
Pointers Lecture 1 Thu, Jan 15, 2004.
Java Programming with Multiple Classes
Introduction of Programming
Object-Oriented Programming (OOP) Lecture No. 22
Lab – 2018/04/12 implement getTotalCount to return how many ‘nMatrix’s are allocated(exist) (modify constructor and destructor, use static variable and.
Review of Function Overloading
Constructors and Deconstructor
Chapter 11 Classes.
Presentation transcript:

Chapter 8

Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such as +,, additional meanings when they are applied to user defined data types a = b + c d3.addobjects(d1, d2) d3 = d1.addobject(d2); d3 = d1 +d2

Overloading Unary Operator class counter { private: int count; // count public: counter(){count = 0;} // constructor int get_count() // return count {return count;} void operator ++(){ ++count;} // increment prefix }; void main() { counter c1, c2 // define and initialization cout<<“c1 = ”<<c1.get_count(); cout<<“c2= ”<<c2.get_count(); ++c1; ++c2; cout<<“c1 = ”<<c1.get_count(); cout<<“c2= ”<<c2.get_count(); } Output C1=0 C2=0 C1 =1 C2=2

Cont’d Operator key word is use to overload the ++ operator void operator ++ () Compiler distinguish overloaded operator by looking at data type of their operand ++ operator increments the count data in the object Operator++() can return a variable of type object for which it is invoked like simple functions c = ++c2;

Example- return class counter { private: int count; // count public: counter(){count = 0;} int get_count() { return count;} counter operator ++(){ ++count; counter temp; temp.count = count; return temp;} }; void main() { // define and initialization counter c1, c2 cout<<“c1 = ”<<c1.get_count(); cout<<“c2= ”<<c2.get_count(); ++c1; // c1=1; c2=++c1; //c1=2, c2=2 cout<<“c1 = ”<<c1.get_count(); cout<<“c2= ”<<c2.get_count(); }

Nameless Temporary Objects Make temporary object and gives it same value and return it counter operator ++(){ ++count; counter temp; temp.count = count; return temp;} OR counter operator ++() { ++count; return Counter(count); // must have constructor with single argument }

Postfix notation class counter { private: unsigned int count; Public: counter(){count = 0;} counter(int c){count = c;} unsigned int get_count() {return count;} counter operator ++() {return counter(++count);} counter operator ++(int) {return counter(count++); } }; void main() { counter c1, c2 ; cout<<“c1 = ”<<c1.get_count(); cout<<“c2= ”<<c2.get_count(); ++c1; // c1=1; c2=++c1; //c1=2, c2=2 cout<<“c1 = ”<<c1.get_count(); cout<<“c2= ”<<c2.get_count(); c2=c1++; //c1=3, c2=2 cout<<“c1 = ”<<c1.get_count(); cout<<“c2= ”<<c2.get_count(); }

Overloading Binary Operators- Arithmetic Operator (+) class Distance { private: int feet; float inches; public: Distance(){feet=0;inches=0;} Distance(int ft, float in){ feet=ft; inches =in;} void getdist() {cout >feet; cout >inches; } void showdist() { cout<<feet<<inches;} Distance operator + (Distance); }; Distance Distance:: operator + (Distance d2) { int f = feet + d2.feet; float i = inches +d2.inches; if(i>=12){i-=12.0;f++;} return Distance(f,i); }

Cont’d int main() { Distance dist1, dist3; dist1.getdist(); Distance dist2(11,6.25); dist3 = dist1 + dist2; cout<<dist1.showdist(); cout<<dist2.showdist(); cout<<dist3.showdist(); }

Cont’d The argument on the left side of operator is the object (dist1) of which operator is member The object on the right side of the operator(dist2) is argument to the operator

Comparison operator class Distance { private: int feet; float inches; public: Distance(){feet=0;inches=0;} Distance(int ft, float in){ feet=ft; inches =in;} void getdist() {cout >feet; Cout >inches; } void showdist() { cout<<feet<<inches;} bool operator < (Distance); }; bool Distance:: operator < (Distance d2) { float bf1= feet +inches/12; Float bf2= d2.feet + d2.inches/12} return (bf1<bf2)?true:false; }

Cont’d int main() { Distance dist1; dist1.getdist(); Distance dist2(6,2.5); cout<<dist1.showdist(); cout<<dist2.showdist(); If(dist1 <dist2) cout<<“dist1 Less than dist2”; else cout<<“dist1 greater than dist2”; return 0; }

Data conversion Conversion between basic types (Implicit conversion) e.g. intvar = floatvar; Compiler do automatically by calling built in routines Explicit conversion called casting and is also done by calling same built in routines intvar = static_cast (floatvar); From basic to user-defined Distance(float meters) { float fltfeet= MTF *meters; feet = int(fltfeet); inches = 2 * (fltfeet –feet) } Distance dist1 = 2.35 // Convert meters to Distance

Cont’d From user-Defined to Basic using conversion operator Operator float() { flat fracfeet = inches/12; fracfeet +=float(feet); return fracfeet/MFT; } mtrs = static_cast (dist1); OR mtrs = dist2;

Explicit -keyword Explicit is used in the declaration of constructors to indicate that conversion of an initializer should not take place. e.g. Distance dist1 = 2.35 class A { public: A(int);...}; A declaration like: A a1 = 37; or a(37) // says to call the A(int) constructor to create an A object from the integer value To disable such converting constructor use explicit keyword class A { public: explicit A(int);… };

Mutable - keyword C++ keyword declaring a member non-constant even if it is a member of a const object. class scrollbar{ private: int size; mutable string owner; … Public: scrollbar(int sz, string own){size =sz; owner=own;} void setSize(int sz){size = sz;} void setOwner (string own){owner= own;}... }; int main(){ const scrollbar sbar(60, “window1”); sbar.Setsize(100); // illegal sbar.setOwner(“Window2”); // this is OK