Download presentation
Presentation is loading. Please wait.
Published byJean Stevens Modified over 6 years ago
1
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Lecture C++ Interlude 1.3, 5. C++ Book.
2
Announcements / Agenda
Homework due: Tomorrow! Quiz: Move to 10/20 Study Groups? Program 2 assigned Agenda: Finish operating overloading C++ Templates: functions and class Examples: Vector, Swap, Bubble Sort, Insertion Sort Added Bonus if time permitting: Arrays, Pointers, Object lifetimes.
3
Operator Overloading 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
4
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; };
5
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); };
6
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 / /=
7
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;
8
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;
9
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;
10
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;
11
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));
12
Resources on overloading.
-- rational class
13
Vectors
14
Vectors #include <vector> #include <iostream>
using namespace std; int main() { vector<int> first;//empty vector vector<int> second = {100, 200, 300};//vector of 100,200,300 vector<int> third = second;//copy of second vecotry vector<int> fourth(4, 100);//vector of 4, 100s second.push_back(400); for (int i = 0; i < second.size(); i++) cout << "val[" << i << "] = " << second[i] << endl; cout << "val[" << i << "] = " << second.at(i) << endl; } return 0;
15
Vectors Excellent data structure choice
Fast, safe-access, good memory characteristics Built on dynamic array Templatized so that vector<myFooClass> vFoo(4, foos); vector<Bird> birds(3, eagle);
16
Vector with user defined objects
#include "Bird.h" #include <vector> #include <string> #include <iostream> using namespace std; int main() { Bird b1("duck"); Bird b2("goose"); vector<Bird> birds; //empty vecotor of birds vector<Bird> birds2(4, b1);//4 ducks birds2.push_back(b2); for (int i = 0; i < birds2.size(); i++) cout << birds2.at(i).getName() << endl; } return 0;
17
Who really developed the Von Neumann architecture?
CSS430 Operating Systems : Introduction
18
Templates One Function works on ALL types!
19
Templates Polymorphism Works on Function or Class level
Allows for multiple types to be passed to function or class One set of code works across all types Works on Function or Class level Syntax: template <class ItemType> ItemType is the type utilized throughout code Function/Class must be able to handle the types utilized
20
Why Object Oriented Programming (OOP)?
Abstraction Encapsulation Hierarchy Polymorphism
21
Templatize Function SWAP
22
Swap (w/doubles) int main() void SwapInt(double &a, double &b) { {
cout << "a = " << a << " b = " << b << endl; SwapInt(a, b); cin >> a; return 0; } void SwapInt(double &a, double &b) { int temp; temp = a; a = b; b = temp; return; }
23
Swap<> template<class ItemType>
int main() { int a = 3; int b = ; Swap(a, b); cout << a << endl; string nameA = "ChapoGuzman"; string nameB = "JimmyHoffa"; Swap(nameA, nameB); cout << nameA << endl; return 0; } template<class ItemType> void Swap(ItemType &x, ItemType &y) { ItemType temp = x; x = y; y = temp; }
24
Templatize Class SORTED LIST
25
SortedList Class Template Example
Example to show templates on a class which keeps a sorted set of items Add an item Print out items Sort Bonus Points: concatenate two lists Data Structure: Vector Sorts BubbleSort Insertion sort algorithm
26
#ifndef SORTED_LIST_H
#define SORTED_LIST_H #include <iostream> #include <vector> using namespace std; template <class ItemType> class SortedList { public: SortedList(); ~SortedList(); void Add(const ItemType &item); void Print() const; void Sort(); private: vector<ItemType> thelist; }; #include "SortedList.cpp" #endif #ifndef SORTED_LIST_CPP #define SORTED_LIST_CPP #include "SortedList.h" template<class ItemType> SortedList<ItemType>::SortedList(){ } void SortedList<ItemType>::Add(const ItemType &item) { thelist.push_back(item); return; } void SortedList<ItemType>::Print() const for (int i = 0; i < thelist.size(); i++) cout << thelist[i] << endl; #endif
27
Class Bell
28
Bubblesort or Sinking Sort
Used to sort an array of items by traversing (n-1) times and bubbling largest current item to the bottom Graphical Representation : example-300px.gif Bad memory and speed characteristics. “the bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems“, Donald Knuth
29
Insertion Sort Sorted Unsorted Copy 10 Shift 29 Insert 10, copy 14
37 13 Copy 10 Shift 29 29 29 14 37 13 10 29 14 37 13 Insert 10, copy 14 unsortedTop 10 29 29 37 13 Shift 29 10 14 29 37 13 Insert 14; copy 37 10 14 29 37 13 Shift nothing 10 14 29 37 13 Insert 37; Copy 13 10 14 14 29 37 Shift 37, 29 and 14. 10 13 14 29 37 Insert 13 Or Carranno , Or:
30
Function for Insertion Sort
template<class ItemType> void SortedList<ItemType>::Sort() { for (int place = 1; place < thelist.size(); place++) ItemType temp = thelist[place]; int i = place; while ((i > 0) && (thelist[i-1] > temp)) thelist[i] = thelist[i-1]; i--; } thelist[i] = temp;
31
C++ Fundamentals
32
Pointers 4 3 1 5 Pointer variables int *p, *q;
Static allocation int x; Address-of operator p = &x; Memory cell to which P points *p = 6; Pointer operations q = p; 4 3 1 ? p q x ? p q x ? 6 p q x 5 6 p q x
33
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;
34
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.