CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 3. 140112. CARRANO 3.1-3.2, APP D, C++ BOOK.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Abstract Data Type Fraction Example
More on Classes. COMP104 ADT / Slide 2 Abstract Data Type * An Abstract Data Type is a class with some special restrictions. * These restrictions can.
Class and Objects.
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.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
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)
1 Standard Containers: Lists Gordon College Resource:
CS 117 Section 2 + KNET Computer accounts – ed to KNET students –Change password Homework 1 Lab Tutors –In lab for next 2 weeks –Will help you with.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 14. User Defined Classes.
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R1. ADTs as Classes.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
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.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CH1, C++ INTERLUDE 1.
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
ITEC 320 C++ Examples.
Pointers OVERVIEW.
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+
CSC 107 – Programming For Science. The Week’s Goal.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
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.
Lecture 7 : Intro. to STL (Standard Template Library)
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
11 Introduction to Object Oriented Programming (Continued) Cats.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
Vectors CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Introduction to Object Oriented Programming Chapter 10.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
Exception Handling How to handle the runtime errors.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO C++ INTERLUDE 2, CHAPT 4, 6.
CSS342: Introduction1 Professor: Munehiro Fukuda.
1 Ugly Realities The Dark Side of C++ Chapter 12.
Yan Shi CS/SE 2630 Lecture Notes
CSCE 210 Data Structures and Algorithms
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
The dirty secrets of objects
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Fraction Abstract Data Type
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
COMS 261 Computer Science I
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Development and Implementation
Class rational part2.
More on C++ G Carl Evans.
Presentation transcript:

CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO , APP D, C++ BOOK

Announcements / Agenda Announcements ◦Questions on HW ◦ ◦Examples to view Agenda ◦Encapsulation ◦Finish Rational Class ◦Peer Design Review for HW

Encapsulation and Data 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 SC3#view=detail&mid=3BEB69AE3513C BEB69AE3513C SC3#view=detail&mid=3BEB69AE3513C BEB69AE3513C

Example program: Rational Class Continued from last week… Create a class to represent a rational number This should allow for multiplication, division, addition, subtraction. Comparison (eg, equals) Printing out

First w/o Operator Overloading. Partial Rational.h class Rational { public: Rational(); Rational(int a, int b); int getNumerator() const; int getDenominator() const; Rational Multiply(Rational rat) const; …. Fill in Divide, Add, Subtract, equals… void PrintRational(std::ostream &outstream) const; ~Rational(); private: int numerator; int denominator; void Reduce(); };

First w/o Operator Overloading. Partial Rational.cpp void Rational::Reduce() { int gcd = 1; for (int i = 2; i <= min(numerator, denominator); i++) { if (((numerator % i) == 0) && ((denominator % i) == 0)) { gcd = i; } if (gcd > 1) { numerator /= gcd; denominator /= gcd; }

First w/o Operator Overloading. Partial Rational.cpp Rational::Rational() { numerator = 0; denominator = 1; } Rational::Rational(int n, int d) { numerator = n; denominator = d; Reduce(); } int Rational::getDenominator() const { return denominator; } Rational Rational::Multiply(const Rational &rat) const { Rational tempRat; tempRat.numerator = numerator * rat.numerator; tempRat.denominator = denominator * rat.denominator; tempRat.Reduce(); return tempRat; } void Rational::PrintRational(ostream &outstream) const { outstream << numerator << " / " << denominator << endl; }

Operator Overload Allowing for operators to work on classes in intuitive ways. Today we will cover Assignment: = 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: Rational(); Rational(int a, int b); int getNumerator() const; int getDenominator() const; Rational operator*(const Rational &rat) const; Rational operator/(const Rational &rat) const; Rational operator+(const Rational &rat) const; Rational operator-(const Rational &rat) const; ………. }

Overloading input/output > class Rational { friend std::ostream& operator<<(std::ostream &outStream, const Rational &rat); friend std::istream& operator>>(std::istream &inStream, Rational &rat); public: Rational(); Rational(int a, int b); int getNumerator() const; int getDenominator() const; Rational operator*(const Rational &rat) const; Rational operator/(const Rational &rat) const; Rational operator+(const Rational &rat) const; Rational operator-(const Rational &rat) const; ………. }

Overloading ==, += as member functions class Rational { public: Rational(); Rational(int a, int b); int getNumerator() const; int getDenominator() const; Rational operator*(const Rational &rat) const; Rational operator/(const Rational &rat) const; Rational operator+(const Rational &rat) const; Rational operator-(const Rational &rat) const; bool operator==(const Rational &rat) const; Rational& operator+=(const Rational &rat); ………. }

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

Resources on overloading rational classhttp://courses.washington.edu/css342/dimpsey/ProgramExamples/code.html

Computer Scientist of the week Alan Turing Turing Machine Code Breaking in WWII for England Improvements to Enigma machine Winston Churchill: Turing made the single biggest contribution to Allied victory in the war against Nazi Germany Father of Artificial Intelligence Turing Test

Peer Design Review LAB1

C++ Fundamentals (continued)

Pointers 1.Pointer variablesint *p, *q; 2.Static allocationint x; 3.Address-of operatorp = &x; 4.Memory cell to which P points*p = 6; 5.Pointer operationsq = p; ??? pqx ?? pqx ?6 pqx 6 pqx

this : pointer to self

Bell Rang

Time of Invocation (constructors) Automatic Local Each time block is executed Static Local Once –first time it is hit Global In order of declaration in translation unit Typically before main() is entered Destroyed in reverse order of construction Dynamic (tbd) malloc/free new/delete

Arrays Arrays: reservation and construction of indexed set of objects or built-in types int x=5; int arr1[100] int arr2[3] = {34, 7, 34}; char cArr1[7]; char cArr2[10][5]; arr1[x] = 32; arr2[0] = arr1[x]; cArr2[3][3] = ‘a’; Arrays v Pointers (following are equivalent) void Foo(int arr[]) { } void Foo(int *arr) { } MyFooClass theFooObj; MyFooClass arrFoo[200]; //default constructors run MyFooClass *pFoo; pFoo = arrFoo; arrFoo[54] = theFooObj; pFoo = &theFooObj;

Vectors #include Using namespace std; int main () { vector first; // empty vector of ints vector second (4,100); // four ints with value 100 vector third (second.begin(),second.end()); // iterating through second vector fourth (third); // a copy of third int myints[] = {16,2,77,29}; vector fifth (myints, myints + sizeof(myints) / sizeof(int) ); for (int i=0; i < second.size(); i++) { cout << second.at(2); cout << second[2]; } second.push_back(56); second.pop_back(34);

Vectors Excellent data structure choice Fast, safe-access, good memory characteristics Built on dynamic array Templatized so that vector vFoo(4);

File IO In C:In C++:In Java: FILE *fp;ifstream inFile(“name.dat”);import java.io.*; FileInputStream infile = istream inFile;new FileInputStream( “name.dat” ); if ((fp = fopen( “name.dat”, “r” ))inFile.open(“name.dat”);ObjectInputStream input = != NULL {if ( inFile ) { // true of falsenew ObjectInputStream( infile ); fscanf( fp, “%d”, &i );inFile >> i;try { i = input.readInt( ); fclose(fp);inFile.close( );} catch ( EOFException e ) { }}input.close( ); } Note: for output, use ofstream.

example.h #ifndef EXAMPLE_H #define EXAMPLE_H Full.h file #endif

Accessing Linux Lab and compiling Good Sites for Linux access: Install putty on your system Transfer files with filezilla: Compile and link all.cpp files (list as many.cpp files as you have for your program) then create an executable file called a.out: g++ file1.cpp file2.cpp Trick will in getting.h files correct if porting from other system Unix simple commands: