C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira...

61
C++ Functions and Generics C++ Functions and Generics Graph Algorithms C++ for C Programmers by Ira Pohl

Transcript of C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira...

Page 1: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

C++ Functions and Generics

l  C++ Functions and Generics

l  Graph Algorithms

C++ for C Programmers by Ira Pohl

Page 2: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

C++ new stuff in functions

l  default parameters, variable argument lists, l  const parameters, l  multiple Types in a generic, l  operator overloading.

C++ for C Programmers by Ira Pohl

Page 3: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Quiz – sum an array in C

l  Review – how in C do you write a function to sum an array of doubles?

C++ for C Programmers by Ira Pohl

Page 4: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Answer C – sum array

l  double sum(double data[], int size) l  { l  double s = 0.0; int i; l  for(i = 0; i < size; ++i) l  s += data[i]; l  return s; l  }

C++ for C Programmers by Ira Pohl

Page 5: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Generic Programming l  1. Relating to or descriptive of an entire

group or class; general. l  2. Biology Of or relating to a genus. l  3.a. Not having a brand name: generic soap. l  b. Of or being a drug sold under or identified

by its official nonproprietary or chemical name.

l  4. Grammar Specifying neither masculine nor feminine gender:

C++ for C Programmers by Ira Pohl

Page 6: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Generic Programming

l  Writing code that can use an arbitrary type or types; in C++ this is done with template

C++ for C Programmers by Ira Pohl

Page 7: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

C++ generic Sum an array

l  Consider writing a function that sums an array. Assume that most of time you want the sum to start with zero, but sometimes you want a different initial value.

l  Initial value = 0 and add 3, 4, 6, 11 = 24 l  Initial value = 24 and add 6, 7, 9 = 46

C++ for C Programmers by Ira Pohl

Page 8: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Sum an array

l  template <class T> //T is generic type l  T sum(const T data[], int size, T s=0) l  { l  for(int i = 0; i < size; ++i) l  s += data[i]; //+= must work for T l  return s; l  }

C++ for C Programmers by Ira Pohl

Page 9: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Default parameter

l  T sum(const T data[], int size, T s=0) l  Ideas data is not mutable use a const l  s will default to 0

l  sum(scores, 92); l  sum(scores, 92, 58); l  Same function two different signatures l  Note could also default size; think about it

C++ for C Programmers by Ira Pohl \

Page 10: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

C++ Program

l  int main() l  { l  cout << "template for sum()" << endl; l  int a[] = {1, 2, 3}; l  double b[] = {2.1, 2.2, 2.3}; l  cout << sum(a, 3) << endl; l  cout << sum(b, 3) << endl; l  }

C++ for C Programmers by Ira Pohl

Page 11: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

C++ Program

l  Elements of the array source are not be modified .

l  Simple parameters, such as size, are typically call by value and can not be modified by the calling routine.

l  This concept is called “const correctness.”

C++ for C Programmers by Ira Pohl

Page 12: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Quiz – Modify program

l  Iterate subtraction over the data

l  Write a template function that outputs the elements of the array

C++ for C Programmers by Ira Pohl

Page 13: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Answer

l  for(int i = 0; i < size; ++i) l  s -= data[i]; //+= used for sum

l  for(int i = 0; i < size; ++i) l  cout << data[i] << ‘\t’; l  //you can make this prettier

C++ for C Programmers by Ira Pohl

Page 14: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Multiple Template Arguments

C++ for C Programmers by Ira Pohl

Page 15: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Multiple template args

l We have used templates with one template parameter, but it is useful to have more that one distinct type in a template.

l More genericity – but be careful!!

C++ for C Programmers by Ira Pohl

Page 16: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

C++ templates

l  template<class T1, class T2> l  void copy(const T1 source[], T2 destination[],

int size) l  { l  for(int i = 0; i < size; ++i) l  destination[i] = l  static_cast<T1>( source[i]); l  }

C++ for C Programmers by Ira Pohl

Page 17: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

C++ Casts

l  More types means worrying about conversions and more signatures l  These static_cast operators are considered safe. l  The old cast operator (type) is deprecated As a reminder the other casting operators are: l  reinterpret_cast<type> highly unsafe l  dynamic_cast<type> used with classes l  const_cast<type> cast away const-ness

C++ for C Programmers by Ira Pohl

Page 18: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Graph Theory and Algorithms

C++ for C Programmers by Ira Pohl

Page 19: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Quiz – draw a graph

l Review Draw the complete Graph of 4 nodes

C++ for C Programmers by Ira Pohl

Page 20: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Answer K4

l  O O

l  O O

C++ for C Programmers by Ira Pohl

Page 21: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Graphs

l  The Seven Bridges of Königsberg is a historically notable problem in mathematics. Leonhard Euler in 1735 laid the foundations of graph theory examining this problem.

C++ for C Programmers by Ira Pohl

Page 22: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

7 Bridges Problem

C++ for C Programmers by Ira Pohl

Page 23: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Koningsberg

l  The city of Königsberg in Prussia (now Kaliningrad, Russia) was set on both sides of the Pregel River, and included two large islands which were connected to each other and the mainland by seven bridges.

l  The problem was to find a walk through the city that would cross each bridge once and only once. The islands could not be reached by any route other than the bridges, and every bridge must have been crossed completely every time; one could not walk halfway onto the bridge and then turn around and later cross the other half from the other side.

l  Euler proved that the problem has no solution. There could be no non-retracing continuous curve that passed through all seven of the bridges..

C++ for C Programmers by Ira Pohl

Page 24: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Graph as a data structure

l  Connectivity matrix (also distances)

l  Edge List Representation

l  Tradeoffs - Graph as an ADT

C++ for C Programmers by Ira Pohl

Page 25: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

List representation

l  Definition: A representation of a directed graph with n vertices using an array of n lists of vertices. l  List i contains vertex j if there is an edge from vertex i to vertex j. l  A weighted graph may be represented with a list of vertex / weight pairs.

l  An undirected graph may be represented by having vertex j in the list for vertex i and vertex i in the list for vertex j.

C++ for C Programmers by Ira Pohl

Page 26: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Matrix vs List Directed Graph

l  1 2 3 4 1 1 1 1 1 2 1 0 0 0 3 0 1 0 1 4 0 1 1 0

l  1 -> 1 -> 2 -> 3 -> 4 2 -> 1 3 -> 2 -> 4 4 -> 2 -> 3

C++ for C Programmers by Ira Pohl

Page 27: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Drawing of previous Graph

l  My drawing

C++ for C Programmers by Ira Pohl

Page 28: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Quiz

l  Here is an undirected graph – generate an edge list representation and a matrix representation for it.

l  o l  | l  o ___ o ___ o l  | l  o

C++ for C Programmers by Ira Pohl

Page 29: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Answer Matrix

l  1 2 3 4 5 1 0 0 1 0 0 2 0 0 1 0 0

l  3 1 1 0 1 1 4 0 0 1 0 0 5 0 0 1 0 0

C++ for C Programmers by Ira Pohl

Page 30: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Answer Edge List l  1 -> 3

2 -> 3 3 -> 1 -> 2 -> 4 -> 5 4 -> 3

l  5 -> 3

C++ for C Programmers by Ira Pohl

Page 31: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Dijkstra Shortest Path

l  Find a shortest Path Between start and destination node s to d

l  Step 1 - include s in a closed set and all immediate successors of s with their distance in the open set

l  Step 2 - pick the open node of list cost- say this node is n

C++ for C Programmers by Ira Pohl

Page 32: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Dijkstra

l  If the node that is picked is d stop l  Otherwise compute all succesors of n; l  Add the distance from s to n + n edge to k l  And if it improves on value in open set –

store as new value l  Pick current open set distance that is a min

and repeat – above steps.

C++ for C Programmers by Ira Pohl

Page 33: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Dijkstra Termination

l  If the node that is picked is d stop l  No more succesors – failed no path l  A shortest path tree should be maintained

that lets you create the route

C++ for C Programmers by Ira Pohl

Page 34: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Simulate Dijkstra by hand

C++ for C Programmers by Ira Pohl

Page 35: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Home work Turn in

1.  Programming Assignment 2-

Dijkstra Shortest Path Algorithm

C++ for C Programmers by Ira Pohl

Page 36: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

C++ Creating Types

l  C++ classes and OO l  Point as an example

l  If you have the book: l  Chapter 4 and Chapter 5

C++ for C Programmers by Ira Pohl

Page 37: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Quiz

l  Name three native types in C

l  If you have the expression 3 / 4 what is its value?

l  If you have the expression 3.0 / 4 what is its value?

C++ for C Programmers by Ira Pohl

Page 38: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Answers

l  Any of short, int , double, char, long, long double, int* …

l  3 / 4 both int literals answer 0 l  3.0 / 4 double / int – double division 0.75

l  Conclusion type matters a lot

C++ for C Programmers by Ira Pohl

Page 39: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Enum and operator overloading example

l  Enum is a simple int type

l  Typedef enum color{RED, WHITE,GREEN} color;

l  RED is defaulted to 0. WHITE is 1 and GREEN is 2

C++ for C Programmers by Ira Pohl

Page 40: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Quiz: Should you use enum?

A) Can use a named integer – so it is redundant?

B) Can use a #define - keep old C style?

C) Small related set of constants? Pick best answer

C++ for C Programmers by Ira Pohl

Page 41: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Answer

A) Can use a named const integer –so it is redundant? Okay

l  const int TRUE = 1, FALSE = 0;

B) Can use a #define- keep old C style?- safety issues

l  #define TRUE 1 l  #define FALSE 0

C) Small related set of constants? – yes best answer

C++ for C Programmers by Ira Pohl

Page 42: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Example: days

l  typedef enum days{SUN, MON, TUE, WED, THU, FRI, SAT} days;

l  l  inline days operator+ (days d){ l  return static_cast<days>((static_cast<int>(d) + 1) % 7); l  }

C++ for C Programmers by Ira Pohl

Page 43: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Days & operator overloading

l  ostream& operator<< l  (ostream& out, days d){ l  switch (d){ l  case SUN: out << "SUN"; break; l  case MON: out << "MON"; break; l  … } l  return out; l  }

l  //operator << is normally left bit shift

C++ for C Programmers by Ira Pohl

Page 44: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Example Days

l  int main() l  { l  days d = MON, e; l  e = +d; l  cout << d << '\t' << e << endl; l  }

C++ for C Programmers by Ira Pohl

Page 45: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Quiz

l  What native operator is << ?

l  Can its precedence be changed ?

l  How do we know which meaning to apply?

C++ for C Programmers by Ira Pohl

Page 46: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Answer

l  << left bit shift l  int a = 5; // binary: 0000000000000101 l  int b = a << 3; l  // binary: 0000000000101000, or 40 //decimal

l  Precedence and associativity do not change

l  Based on binary signature

C++ for C Programmers by Ira Pohl

Page 47: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Why add a type?

l  Types are related to domains

l  So when you need to talk about widgets-you want the widget type and actions on widgets

l  C had primitive forms of type extensibility

C++ for C Programmers by Ira Pohl

Page 48: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

C Type Extension

l  In C you can “add “ a type using struct.

l  In C++ struct is different – struct is a named scope that can include functions (methods) and have different layers of opacity (data hiding). While C++ retains struct it has the new keyword class.

l  Class is almost equivalent to struct – but with different data hiding defaults.

C++ for C Programmers by Ira Pohl

Page 49: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

C type point as struct

l  typedef struct point{double x, y;} point; l  void add_points(point* p1, point* p2, point* sum){ l  sum->x = p1->x + p2->x; l  sum->y = p1->y + p2->y; l  } l 

C++ for C Programmers by Ira Pohl

Page 50: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

C struct

l  Struct has fields – data members

l  File scope functions manipulate the struct using pointer operations

C++ for C Programmers by Ira Pohl

Page 51: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

C++ point

l  class point{ l  public: l  double x, y; l  };

l  Public, private, protected are access keywords

C++ for C Programmers by Ira Pohl

Page 52: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Better Point

l  class point{ l  public: l  double getx(){return x;} l  void setx(double val){x = v;} l  ….. l  private: l  double x, y; l  };

C++ for C Programmers by Ira Pohl

Page 53: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Q : Review OO

l  In OO why is data hidden?

C++ for C Programmers by Ira Pohl

Page 54: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Answer

l  The principle of the black box – what is under the hood should be left to experts;

l  This includes representation.

C++ for C Programmers by Ira Pohl

Page 55: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Point functions

l  point operator+ (point& p1, point& p2){ l  point sum = {p1.x + p2.x, p1.y + p2.y}; l  return sum; l  } l  ostream& operator<< (ostream& out, point& p){ l  out << "( " << p.x << ", " << p.y <<" )"; l  return out; l  } l  .

C++ for C Programmers by Ira Pohl

Page 56: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

C++

l  int main() l  { l  point a = {3.5, 2.5}, b = {2.5, 4.5}, c; l  cout << "a = " << a << " b = " << b <<endl; l  cout << " sum = " << a + b << endl; l  }

C++ for C Programmers by Ira Pohl

Page 57: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

User Defined Types

l  Using point in main() looks very much like using a native type.

l  Indeed this is one of our key goals in OO.

l  We accomplished this by having point be a class -a user defined type. We overload the standard operators like + and << to give them appropriate "point” semantics.

C++ for C Programmers by Ira Pohl

Page 58: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Methods

l  class point{ l  public: l  double getx(){return x;} l  void setx(double val){x = v;} l  Class member functions have automatic

access to private members. l  p1.getx() p2.setx(2.5) p2.sety(-1.8)

C++ for C Programmers by Ira Pohl

Page 59: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

How to build an object

l  Object construction is needed for OO

l  Built in objects such as int double, fixed arrays are built by the compiler.

l  Big part of OO/C++ mastery is how to build objects :

C++ for C Programmers by Ira Pohl

Page 60: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

A special method - constructor

l  point(){ x = y = 0.0;}

l  Or l  point(){ this -> x = 0.0; this ->y = 0.0}

l  Default constructor – the constructor whose signature is void.

C++ for C Programmers by Ira Pohl

Page 61: C++ Functions and Generics Graph Algorithms · operator overloading. C++ for C Programmers by Ira Pohl . Quiz – sum an array in C ! Review – how in C do you write a function to

Finally

l  Constructors and destructors are our next major topic – in reading chapter 5

l  To be continued.

C++ for C Programmers by Ira Pohl