Download presentation
Presentation is loading. Please wait.
Published byAdele Marshall Modified over 9 years ago
1
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 3. 140112. CARRANO 3.1-3.2, APP D, C++ BOOK
2
Announcements / Agenda Announcements ◦Questions on HW ◦-12 -30 -30 ◦Examples to view Agenda ◦Encapsulation ◦Finish Rational Class ◦Peer Design Review for HW
3
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 http://www.bing.com/videos/search?q=c%2b%2b+encapsulation&FORM=HDR SC3#view=detail&mid=3BEB69AE3513C70139013BEB69AE3513C7013901 http://www.bing.com/videos/search?q=c%2b%2b+encapsulation&FORM=HDR SC3#view=detail&mid=3BEB69AE3513C70139013BEB69AE3513C7013901
4
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
5
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(); };
6
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; }
7
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; }
8
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
9
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; ………. }
10
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; ………. }
11
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); ………. }
12
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; }
13
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 classhttp://courses.washington.edu/css342/dimpsey/ProgramExamples/code.html
14
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
15
Peer Design Review LAB1
16
C++ Fundamentals (continued)
17
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 1 3 4 5
18
this : pointer to self
19
Bell Rang
20
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
21
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;
22
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);
23
Vectors Excellent data structure choice Fast, safe-access, good memory characteristics Built on dynamic array Templatized so that vector vFoo(4);
24
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.
25
example.h #ifndef EXAMPLE_H #define EXAMPLE_H Full.h file #endif
26
Accessing Linux Lab and compiling Good Sites for Linux access: http://depts.washington.edu/cssuwb/wiki/connecting_to_the_linux_lab http://depts.washington.edu/cssuwb/wiki/connecting_to_the_linux_lab http://www.bothell.washington.edu/css/facilities/unix Install putty on your system Transfer files with filezilla: http://www.washington.edu/itconnect/connect/web-publishing/shared-hosting/sftp/http://www.washington.edu/itconnect/connect/web-publishing/shared-hosting/sftp/ 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: http://courses.washington.edu/css342/zander/css332/basicunix.htmlhttp://courses.washington.edu/css342/zander/css332/basicunix.html
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.