CSS 342 Data Structures, Algorithms, and Discrete Mathematics I

Slides:



Advertisements
Similar presentations
Operator Overloading Fundamentals
Advertisements

More on Classes. COMP104 ADT / Slide 2 Abstract Data Type * An Abstract Data Type is a class with some special restrictions. * These restrictions can.
Operator Overloading Back to Fractions.... Implementing an Object We’ve talked at length about object- orientation. – We’ve looked heavily at encapsulation.
Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros
Abstract Data Types Development and Implementation JPC and JWD © 2002 McGraw-Hill, Inc.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Abstract Data Types Development and Implementation.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
C++ Workshop Designing and Implementing Classes. References ● C++ Programming Language, Bjarne Stroustrup, Addison-Wesley ● C++ and Object-Oriented Numeric.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Templates and Polymorphism Generic functions and classes.
Abstract Data Type. COMP104 Slide 2 Summary l A class can be used not only to combine data but also to combine data and functions into a single (compound)
Lecture 4 OOP Course. 4. Operators Using constructors: String int main(){ String s1(“My String”); String s2(s1); String s3; s3=s1; } int main(){ String.
CMSC 2021 Stream I/O Operators and Friend Functions.
Object Oriented Programming in C++ Chapter5 Operator Overloading.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 14. User Defined Classes.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R1. ADTs as Classes.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Operator Overloading ~ and User Defined Conversions.
Case Study - Fractions Timothy Budd Oregon State University.
CHAPTER 7 Implementing abstract data types 抽象数据类型实现 Introduction A data abstraction is a representation of information and the operations to be performed.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CH1, C++ INTERLUDE 1.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
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.
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE C++ INTERLUDE 1.3. C++ BOOK.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CH 2, APPENDIX E.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
OPERATOR OVERLOADING WEEK 4-5 CHAPTER 19. class Money {private:int lira; int kurus; public: Money() {}; Money(int l, int k) { lira=l+ k/100; kurus=k%100;
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 5 1.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO , APP D, C++ BOOK.
Programming Abstract Data Types. COMP104 Lecture 30 / Slide 2 Review: class l Name one reason why we need the class construct. l Why do we need "inspector.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO C++ INTERLUDE 2, CHAPT 4, 6.
CISC220 Spring 2010 James Atlas Lecture 04: Pointers, Functions, Memory Management, ADTs, Classes, Hardware, Software, Graphics, Networking, AI, Databases,
Object-Oriented Programming (OOP) Lecture No. 16
Andy Wang Object Oriented Programming in C++ COP 3330
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading CS 3370 – C++ Chapter 14.
Operator Overloading Introduction
Yan Shi CS/SE 2630 Lecture Notes
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value A copy of the argument’s value.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSCE 210 Data Structures and Algorithms
CSS342: Objects and Classes
Chapter 1.2 Introduction to C++ Programming
C++ Templates.
Bill Tucker Austin Community College COSC 1315
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Review Bernard Chen Spring 2006.
Object-Oriented Programming (OOP) Lecture No. 16
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Operator Overloading CSCE 121 J. Michael Moore
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Chapter 5 Function Basics
Advanced Program Design with C++
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CS150 Introduction to Computer Science 1
COMS 261 Computer Science I
COP 3330 Object-oriented Programming in C++
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Development and Implementation
Life is Full of Alternatives
Class rational part2.
CMSC 341 C++ and OOP.
Operator Overloading CSCE 121 Based on Slides created by Carlos Soto.
Presentation transcript:

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I Lecture 3. 161006. Carrano Carrano Ch. 3.1-3.2, Appendix D, C++ Book

Agenda Program 1 Questions In Class Code Quiz C++ Fundamentals Value, Reference, Const Reference String Overloading Operators: Rational Class

Program 1 Questions?

In class code Write a function which takes two integers x and y and “swaps” them int x = 3; int y = 8; cout << x << y; Swap(x,y); Result 3883

C++ Fundamentals LET’S CODE

Call by Value, Reference, and Constant Reference struct Rectangle { int length; int width; }; int Area(Rectangle rect) int Area(Rectangle &rect) int Area(const Rectangle &rect) { int temp; temp = rect.length; rect.length = 35; return(temp * rect.width); } int main() { int result; Rectangle r = { 3, 3 }; result = Area(r); cout << "length = " << r.length << endl; cout << "width = " << r.width << endl; cout << "Area = " << result << endl; return 0; }

Call by Value, Reference, and Constant Reference Arrays, Pointers, and Structures Call by Value, Reference, and Constant Reference Which of swap functions is appropriate? void swap(string a, string b) { string tmp = a; a = b; b = tmp; } void swap(string &a, string &b) { string tmp = a; a = b; b = tmp; } void swap(const string &a, const string &b) { string tmp = a; a = b; b = tmp; } Which of findMax functions is appropriate? int findMax(vector<int> a) { int max = a[0]; int i; for (i=1; i < a.size(); i++) if (a[i] > max) max = a[i]; return max; } int findMax(vector<int> &a) { int max = a[0]; int i; for (i=1; i < a.size(); i++) if (a[i] > max) max = a[i]; return max; } int findMax(const vector<int> &a) { int max = a[0]; int i; for (i=1; i < a.size(); i++) if (a[i] > max) max = a[i]; return max; } CSS342: Introduction

string http://www.cprogramming.com/tutorial/string.html Mutable; Copied not shared. string firstName = "jimmy"; string lastName("hoffa"); string fullName; fullName = firstName + " " + lastName; cout << fullName << endl; cout << "First and last letters are:" << fullName[0] << " " << fullName[fullName.length() - 1] << endl; if (fullName == "jimmy hoffa") { cout << "Found !!!! "; } else cout << "oh where oh where have you gone";

Encapsulation Review

Encapsulation and Data Review public: contract exposes functions for class The What Actions Designed first Private: All data, helper functions Base types like int, double, … Arrays, Vectors, Linked Lists Queues, Stacks, Trees, Graphs

C++ Program Lifecycle

Computer Scientist of the week Grace Hopper Invented the first compiler United States Navy Rear Admiral Promoted the idea of machine independent languages Key inventor of COBOL Popularized the term debugging Advocated for Navy to move from centralized computers to distributed computers in the 1970s

C++ Program Execution Lifecycle http://www.technovisitors.com/2014/06/C-program-execution-life-cycle.html

Rational Class Create a class to represent a rational number

Rational Class Create a class to represent a rational number This should allow for multiplication, division, addition, subtraction. Comparison (eg, ==, !=) Printing out to stream

Interface Design Checklist Constructors Setters/Getters Actions (verbs) Operator Overloads Private Data Types

Rational.h: w/o Operating Overloads #include <iostream> #include <algorithm> using namespace std; class Rational { public: Rational(); Rational(int num, int den); ~Rational(); int getNumerator() const; int getDemnominator() const; bool setValue(int num, int den); Rational Multiply(const Rational &rat) const; Rational Divide(const Rational &rat) const; Rational Subtract(const Rational &rat) const; Rational Add(const Rational &rat) const; void PrintRational(ostream &theStream) const; private: int numerator; int denominator; void reduce(); };

Rational.cpp: w/o Operator Overloading Rational::Rational() { numerator = 0; denominator = 1; } Rational::Rational(int num, int den) numerator = num; denominator = den; reduce(); int Rational::getNumerator() const return numerator; int Rational::getDemnominator() const return denominator; bool Rational::setValue(int num, int den) { if (den == 0) return false; } numerator = num; denominator = den; reduce(); return true;

Rational.cpp: w/o Operator Overloading void Rational::reduce() { int gcd = 1; int minimum = min(numerator, denominator); for (int i = 2; i <= minimum; i++) if (((numerator % i) == 0) && ((denominator % i) == 0)) gcd = i; } if (gcd > 1) numerator /= gcd; denominator /= gcd; Rational Rational::Multiply(const Rational &rat) const { Rational temp; temp.numerator = numerator * rat.numerator; temp.denominator = denominator * rat.denominator; temp.reduce(); return temp; } Rational Rational::Add(const Rational &rat) const { Rational temp; temp.numerator = (numerator * rat.denominator) + (rat.numerator * denominator); temp.denominator = denominator * rat.denominator; temp.reduce(); return temp; }

Operator Overload Allowing for operators to work on classes in intuitive ways. Key Examples: Arithmetic: +, -, *, / Comparison: ==, !=, <, >, <=, >= Input: <<, >> General rules for overloading Whenever the meaning of an operator is not obviously clear and undisputed, it should not be overloaded Always stick to the operator’s well-known semantics Always provide all out of a set of related operations Operators retain their precedence order

Overloading +,-,*,/ as member functions class Rational { public: // Op Overloads Rational operator*(const Rational &rat) const; Rational operator/(const Rational &rat) const; Rational operator-(const Rational &rat) const; Rational operator+(const Rational &rat) const; };

Class Bell

Overloading +=,-=,*=,/= as member functions class Rational { public: // Op Overloads Rational operator*=(const Rational &rat); Rational operator/=(const Rational &rat); Rational operator-=(const Rational &rat); Rational operator+=(const Rational &rat); };

But wait… Given we overloaded +, is there a better way to implement +=? Actually, one should implement += first, and then used it to implement + Same for -=/-, *=/*, and / /=

Using += overload to implement + MyClass & MyClass::operator+=(const MyClass &rhs) { ... // Do the compound assignment work. return *this; } MyClass MyClass::operator+(const MyClass &other) const MyClass result = *this; result += other; return result;

Overloading input/output <<, >> class Rational { public: friend ostream& operator<<(ostream &outStream, const Rational &rat); friend istream& operator>>(istream &inStream, Rational &rat); // Op Overloads Rational operator*(const Rational &rat) const; Rational operator/(const Rational &rat) const; Rational operator-(const Rational &rat) const; Rational operator+(const Rational &rat) const;

Implementation of <<, >> overloads ostream& operator<<(ostream &outStream, const Rational &rat) { outStream << rat.numerator << "/" << rat.denominator; return outStream; } istream& operator>>(istream &inStream, Rational &rat) inStream >> rat.numerator >> rat.denominator; if (rat.denominator == 0) rat.numerator = 0; rat.denominator = 1; else rat.reduce(); return inStream;

Overloading ==, !=, +=, etc… class Rational { friend ostream& operator<<(ostream &outStream, const Rational &rat); friend istream& operator>>(istream &inStream, Rational &rat); public: // Op Overloads Rational operator*(const Rational &rat) const; Rational& operator*=(const Rational &rat); Rational operator/(const Rational &rat) const; Rational& operator/=(const Rational &rat); Rational operator-(const Rational &rat) const; Rational& operator-=(const Rational &rat); Rational operator+(const Rational &rat) const; Rational& operator+=(const Rational &rat); bool operator==(const Rational &rat) const; bool operator!=(const Rational &rat) const;

Implementation bool Rational::operator==(const Rational &rat) const { return((numerator == rat.numerator) && (denominator == rat.denominator)); } bool Rational::operator!=(const Rational &rat) const return ((numerator != rat.numerator) || (denominator != rat.denominator)); bool Rational::operator<(const Rational &rat) const return ((numerator * rat.denominator) < (rat.numerator * denominator)); bool Rational::operator<=(const Rational &rat) const return ((numerator * rat.denominator) <= (rat.numerator * denominator)); bool Rational::operator>(const Rational &rat) const return ((numerator * rat.denominator) > (rat.numerator * denominator)); bool Rational::operator>=(const Rational &rat) const return ((numerator * rat.denominator) >= (rat.numerator * denominator));

Resources on overloading. http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html http://courses.washington.edu/css342/dimpsey/ProgramExamples/code.html -- rational class