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

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Operator overloading redefine the operations of operators
Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Complex Numbers and Their Geometry Complex Numbers and Their Geometry
Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012.
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Copyright  Hannu Laine C++-programming Part 8 Hannu Laine.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
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.
Abstract Data Type Fraction Example
Operator Overloading Fundamentals
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
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.
Operator Overloading Enable C++’s operators to work with class object >, +, -, *, / This operators perform differently depending on their context in integer,
ADT Specification Example
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Engineering Problem Solving With C++ An Object Based Approach Chapter 8 Introduction to Classes.
CMSC 2021 Stream I/O Operators and Friend Functions.
Chapter 15: 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.
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 Object Oriented Programming.
Chapter 3 Section 3.4 The Fundamental Theorem of Algebra.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
Chapter 12: Adding Functionality to Your Classes.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
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 Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
18-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
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.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
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.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Chapter Complex Numbers What you should learn 1.Use the imaginary unit i to write complex numbers 2.Add, subtract, and multiply complex numbers 3.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Class Operations Creating New Types. Review Last time, we began building, a class to allow us to model temperatures: Last time, we began building Temperature,
南亚和印度.
Operator Overloading What is operator overloading? Most predefined operators (arithmetic, logic, etc.) in C++ can be overloaded when applied to objects.
Simplify. Complex Numbers Complex Numbers Intro Definition of Pure Imaginary Numbers: For any positive real number, “b” Where i is the imaginary unit.
Andy Wang Object Oriented Programming in C++ COP 3330
Function Overloading Can enables several function Of same name
Chapter 14: More About Classes.
Introduction to Programming
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Chapter 14: More About Classes.
Classes and Data Abstraction
Conjugates Standard form of Complex Numbers
Introduction to Programming
Chapter 14 Object-Oriented Software Development
Chapter 11 Classes.
Presentation transcript:

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

Overloading Operators Allows a programmer defined data type to be used in the same way that a predefined data type is used. Definitions for operator functions are included in a class definition in the same way as member functions –keyword operator is used followed by the name of the function. –the name of the function is one of the predefined C++ operators Only predefined operators may be overloaded All predefined operators except(. ::.* ?: sizeof) may be overloaded.

Complex Number Class 4A complex number is a number that has two components; the real component and the imaginary component. a + bi 4Arithmetic is defined as follows: (a + bi) + (c + di) = (a + c) + (b + d)i (a + bi) - (c + di) = (a - c) + (b - d)i (a + bi) * (c + di) = (ac - bd) + (ad + bc)i (a + bi) / (c + di) = (ac + bd) / (c**2+d**2) + [ (bc -ad) /(c**2+d**2)]i

Class Declaration class complex { public: complex(); complex(double,double); void print(ostream&); void input(istream&); complex operator+(complex&) const; complex operator-(complex&) const; complex operator*(complex&) const; complex operator/(complex&) const; private: double real, imag; };

Implementation - constructors complex::complex() : real(0), imag(0) { //default constructor } complex :: complex(double r, double im) : real(r), imag(im) {//parameterized constructor }

Implementation – Overloaded Operators complex complex::operator+(complex& c) const { complex temp; temp.real = real + c.real; temp.imag = imag + c.imag; return temp; }

Implementation - Continued complex complex::operator/(complex& c) const { complex temp; temp.real = (real*c.real + imag*c.imag)/ ( pow(c.real,2) + pow(imag,2) ); temp.imag = (imag*c.real - real*c.imag)/ ( pow(c.real,2) + pow(imag,2) ); return temp; }

Practice! – Implement the * operator (a + bi) * (c + di) = (ac - bd) + (ad + bc)i complex complex::operator*(complex& c) const { complex temp; temp.real = real*c.real – imag*c.imag; temp.imag = real*c.imag + imag*c.real; return temp; }

Test Program complex c1, c2, c3;//declare three complex variables c1.input(cin); c2.input(cin); //test addition c3 = c1 + c2;// using overloaded operator + cout << endl << "c1 + c2 is "; c3.print(cout); //test division c3 = c1 / c2; // using overloaded operator / cout << endl << "c1 / c2 is "; c3.print(cout); cout << endl;

Sample Output 4Using the following input: The expected output from our test program will be: c1 + c2 is i c1 / c2 is i

Variations Overloading Operators as: –member functions –friend functions –top level (non-member) functions

Member Functions binary operators (ie +,-, * ) –member function requires one argument –the left hand operand is the object invoking the operator unary operator –member function requires no arguments –assumed to be prefix version of operators ++ and – –postfix versions of operators ++ and – are not covered in this text Disadvantages –first argument must be an object of the class

Example – class complex In complex.h –complex operator +(complex c); operator + implemented in complex.cpp In a client program complex a, b, c; c = a + b; //a is calling object, b is argument c = a ; //OK, constructor is called to // convert 54.3 to complex object c = a // is not allowed

Friend Functions binary operators –friend function requires two arguments unary operator –friend function requires one argument Disadvantage –A friend function is NOT a member function –Friend functions violate a strict interpretation of object oriented principals (implementation is hidden) –Recommended for operator overloading only

Example: In class definition: friend complex operator +(complex c1, complex c2); In class implementation: complex operator +(complex c1, complex c2) { complex temp; temp.real = c1.real + c2.real; temp.imag = c1.imag + c2.imag; return temp; } In client program: complex cA, cB, cC; cC = cA+cB; cC = cB; //this is ok, when + is a friend function

Class Declaration class complex { public: complex(); complex(double,double); friend ostream& operator <<(ostream&, complex); friend istream& operator >>(istream&, complex&); complex operator+(complex&) const; complex operator-(complex&) const; complex operator*(complex&) const; complex operator/(complex&) const; private: double real, imag; };

Implementation ostream& operator <<(ostream& os, complex r) {os << r.real << + << r.imag << i; return os; } istream& operator >> (istream& is, complex& r) {is >> r.real >> r.imag; return is; }

Error Checking on input operator 4If your input fails because of incorrect format, your function should mark the state of the istream as bad is.clear(ios::badbit | is.rdstate() ) 4clear resets entire error state to zero 4clear(ios::badbit) clears all and sets badbit 4is.rdstate() returns the previous state of all bits 4Statement sets the bit vector to the bitwise OR of badbit with previous state 4Failure can be tested with is.fail()

Top Level (Non-Member) Functions binary operators –top level function requires two arguments unary operator –top level function requires one argument Disadvantage: –Top level functions do not have access to private data members. Function must use accessor functions.

Example of top level function Function prototype in client program. complex operator +(complex c1, complex c2); Implemention in client program. complex operator +(complex c1, complex c2) { return complex(c1.get_real() + c2.get_real(), c1.get_imag() + c2.get_imag()); } Use in client program. complex cA, cB, cC; cC = cA + cB; cC = cA;//this is ok, when + is a top level //function