Presentation is loading. Please wait.

Presentation is loading. Please wait.

Generic Programming CSCE 121 J. Michael Moore.

Similar presentations


Presentation on theme: "Generic Programming CSCE 121 J. Michael Moore."— Presentation transcript:

1 Generic Programming CSCE 121 J. Michael Moore

2 Data types Writing the same thing three (or more) times.
Similar functions must be written for different data types, even thought the algorithm is exactly the same. void swap(int& a, int& b) { int temp = a; a = b; b = temp; } void swap(double& a, double& b) { double temp = a; a = b; b = temp; } Write algorithm one time and re-use with different data types. void swap(string& a, string& b) { string temp = a; a = b; b = temp; }

3 Generic Programming Wikipedia: a style of computer programming in which algorithms are written in terms of types to-be-specified-later that are then instantiated when needed for specific types provided as parameters. generics in Ada, Delphi, Eiffel, Java, C#, F#, Objective-C, Swift, and Visual Basic .NET; parametric polymorphism in ML, Scala, Haskell (the Haskell community also uses the term "generic" for a related but somewhat different concept) and Julia; templates in C++ and D; and parameterized types in the influential 1994 book Design Patterns. (Wikipedia)

4 Example vector in C++ It can hold any data type as long as the data type fulfill certain rules. More later with templates.

5 Recall: Programming Goals
Correctness Efficiency Robustness Readability Reuse Modularity


Download ppt "Generic Programming CSCE 121 J. Michael Moore."

Similar presentations


Ads by Google