1 CSC241: Object Oriented Programming Lecture No 11.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
4/14/2015Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23m2 OOP Friend Functions Ref: Sebesta, Chapter 12; Lafore, Chapter 11.
1 CSC241: Object Oriented Programming Lecture No 21.
1 CSC241: Object Oriented Programming Lecture No 28.
Class and Objects.
Operator Overloading. Basics Define operations on user-defined data types –Perform same operation as it is supposed to but, with operands of the user-defined.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Class Array template The array class defined in last week manipulate array of integer If we need to define class of array for float, double data type,
CMSC 2021 Stream I/O Operators and Friend Functions.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Operator overloading Object Oriented Programming.
Operator Overloading in C++
1 CSC241: Object Oriented Programming Lecture No 07.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
Operator Overloading and Type Conversions
OPERATOR OVERLODING.
CSIS 123A Lecture 5 Friend Functions Glenn Stevenson CSIS 113A MSJC.
1 CSC241: Object Oriented Programming Lecture No 13.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Operator Overloads Part2. Issue Provide member +(int) operator Rational + int OK int + Rational Error.
1 CSC241: Object Oriented Programming Lecture No 12.
1 Special Programming Workshop CSIT-120 Fall 2000 Workshop Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming.
1 CSC241: Object Oriented Programming Lecture No 22.
Chapter 14 More About Classes. Chapter 13 slide 2 Topics 13.1 Instance and Static Members 13.2 Friends of Classes 13.3 Memberwise Assignment 13.4 Copy.
1 CSC241: Object Oriented Programming Lecture No 16.
FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message **** ** Welcome.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
1 CSC241: Object Oriented Programming Lecture No 02.
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
LECTURE LECTURE 13 Operator Overloading Textbook p.203—216 Today: const member functions Overloading Operators Postfix/infix increment.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 5 1.
CS Class 19 Today  Practice with classes Announcements  Turn in algorithm for Project 5 in class today  Project 5 due 11/11 by midnight – .
1 CSC241: Object Oriented Programming Lecture No 05.
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.
1 CSC241: Object Oriented Programming Lecture No 03.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
Programming Techniques Classes II Important Class Features Spring 2009.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
1 CSC241: Object Oriented Programming Lecture No 08.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Ref: Sebesta, Chapter 12; Lafore, Chapter 11
Overloading C++ supports the concept of overloading Two main types
Visit for more Learning Resources
CSC241: Object Oriented Programming
Overloaded Constructors and Multiple Classes
Introduction to Programming
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
CSC241: Object Oriented Programming
Visit for more Learning Resources
CSC241: Object Oriented Programming
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Operator Overloading
Operator Overloading; String and Array Objects
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Object Oriented Programming Using C++
Introduction to Programming
CS150 Introduction to Computer Science 1
COP 3330 Object-oriented Programming in C++
Objects as Function Arguments
Presentation transcript:

1 CSC241: Object Oriented Programming Lecture No 11

2 Previous Lecture Overloading binary operator – +, – and = operator for ThreeD class – < operator for distance class – += operator for distance class – Subscript operator for Safe array class Two functions to get and set value One function, return by reference Overloaded [ ] operator, return by reference Data conversion

3 Today’s Lecture Data conversion – one-argument constructor – conversion function Overloading stream operators – Stream insertion – Stream extraction

4 Data Conversion The = operator will assign a value from one variable to another, instatements like intvar1 = intvar2; where intvar1 and intvar2 are integer variables Also, = assigns the value of one user-defined object to another, provided they are of the same type, in statements like dist3 = dist1 + dist2; where the result of the addition, is assigned to another object of type Distance, dist3. dist4 = dist3;

5 Cont. Thus, assignments between types, whether they are basic types or user-defined types, are handled by the compiler with no effort on our part, provided that the same data type is used on both sides of the equal sign what happens when the variables on different sides of the = are of different types? For example: – dist2 = 7.55;

6 Distance class class Distance { private: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance(int ft, float in) : feet(ft), inches(in) { } void getdist() { cout > feet; cout > inches; } void showdist() const { cout << feet << “ : ” << inches ; } };

7 Conversions Between Objects and Basic Types Distance(float meters) { float fltfeet = * meters; feet = int(fltfeet); inches = 12*(fltfeet-feet); } operator float() const { float fracfeet = inches/12; fracfeet += feet; return fracfeet/ ; } main() { float mtrs; Distance dist1 = 2.35; cout << “dist1=“; dist1.showdist(); mtrs = dist1; cout << “\ndist1 = “ << mtrs << “ meters\n”; Distance dist2(5, 10.25); mtrs = dist2; cout << “\ndist2 = “ << mtrs << “ meters\n”; } Go to program Distance dist1 = 2.35; mtrs = dist2; Distance dist1(2.35); mtrs(dist2);operator float()

8 Conversions Between Objects of Different Classes Can be done using – one-argument constructor – conversion function The choice depends upon whether the conversion routine has to be declared in the source class or in the destination class To illustrate, consider a program that contains two classes: A and B. Also consider the statement: object_A = object_B;

9 Cont. In the above statement, object_B of type B is converted to type A and assigned toobject_A. object_B is the source and object_A is the destination. If class B handles the conversion, it will hold a conversion function. On the other hand, if class A carries out the conversion, it will do that through a constructor that takes an argument of type class B.

10 Example program 01: km to miles class Kilometers{ private: double kms; public: Kilometers(double k) : kms(k) { } void display() { cout<<kms; } double getValue() { return kms; } }; class Miles { private: double miles; public: Miles(double m) : miles(m) { } Miles(Kilometers KM) { miles = KM.getValue()/1.61; } void display() { cout <<“ miles = “<< miles; } operator Kilometers() { return Kilometers(miles*1.61); } };

11 Cont. main( ) { Miles m1 = 100; Kilometers k1 = m1; m1.display(); cout << " = "; k1.display(); cout << endl; Kilometers k2 = 100; Miles m2 = k2; k2.display(); cout << " = "; m2.display(); cout << endl; } Output 100 miles = kilometeres 100 kilometers = miles Go to program Miles m1 = 100; –Constructor in Miles class Kilometers k1 = m1; –Constructor in Kilometer class Kilometer(Miles m) or –Conversion function in miles class operator operator Kilometers() Kilometers k2 = 100; –Constructor in Kilometer class Miles m2 = k2; –Constructor in miles class Miles(Kilometer k) or –Conversion function in miles class operator operator Kilometers()

12 Overloading Stream Insertion and Stream Extraction istream& operator >> (istream& s, Distance& d) { cout > d.feet; cout > d.inches; return s; } ostream& operator << (ostream& s, Distance& d) { s << d.feet << “\’-” << d.inches << ‘\”’; return s; } cin >> dist1 cout << dist1; friend istream& operator >> (istream& s, Distance& d); friend ostream& operator << (ostream& s, Distance& d); Go to program

13