1 / 25

CS222

Week 14 - Friday. CS222. Last time. What did we talk about last time? OOP in C++ Encapsulation Dynamic dispatch Polymorphism Inheritance Self-reference. Questions?. Project 6. Quotes.

nicki
Download Presentation

CS222

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 14 - Friday CS222

  2. Last time • What did we talk about last time? • OOP in C++ • Encapsulation • Dynamic dispatch • Polymorphism • Inheritance • Self-reference

  3. Questions?

  4. Project 6

  5. Quotes C makes it easy to shoot yourself in the foot. C++ makes it harder, but when you do, it blows away your whole leg. BjarneStroustrup Developer of C++

  6. C++ Madness

  7. Overloading operators • In C++, you can overload operators, meaning that you can define what + means when used with classes you design • Thus, the following could be legal: Hippopotamus hippo; Sandwich club; Vampiredracula = club + hippo;

  8. Overloading operators • But, what does it mean to "add" a Hippopotamus to a Sandwich and get a Vampire? • Overloading operators is a bad idea • You can get confusing code • Most languages don't allow it • It C++ it is useful in two cases: • To make your objects easy to input/output using iostream • To perform mathematical operations with numerical classes (like Complex!)

  9. (Partial) overloading operators header Complex& operator=( const Complex& complex ); Complex operator+( const Complex& complex ) const; Complex operator-( const Complex& complex ) const; Complex operator-() const; Complex operator*( const Complex& complex ) const;

  10. (Partial) overloading operators implementation Complex& Complex::operator= ( const Complex& complex ) { real = complex.real; imaginary = complex.imaginary; return *this; }

  11. What's all that const? • const, of course, means constant in C++ • In class methods, you'll see several different usages • Const methods make a guarantee that they will not change the members of the object they are called on • intcountCabbages() const; • Methods can take const arguments • void insert(const Coin money); • Methods cant take const reference arguments • void photograph(const Castle& fortress); • Why take a const reference when references are used to change arguments?

  12. Templates

  13. Templates • Allow classes and functions to be written with a generic type or value parameter, then instantiated later • Each necessary instantiation is generated at compile time • Appears to function like generics in Java, but works very differently under the covers • Most of the time you will use templates, not create them

  14. Template method example template<class T> void exchange(T& a, T& b ) { T temp = a; a = b; b = temp; }

  15. Template classes • You can make a class using templates • The most common use for these is for container classes • e.g. you want a list class that can be a list of anything • The STL filled with such templates • Unfortunately, template classes must be implemented entirely in the header file • C++ allows template classes to be separate from their headers, but no major compiler fully supports it

  16. Template class example template<class T> class Pair { private: T x; T y; public: Pair( const T& a, const T& b ) { x = a; y = b; } T getX() const { return x; } T getY() const { return y; } void swap() { T temp = x; x = y; y = temp; } };

  17. STL Standard Template Library

  18. Containers • list • map • multimap • set • multiset • stack • queue • deque • priority_queue • vector

  19. Iterators • Generalization of pointers • No iterators for: • stack • queue • priority_queue • Regular iterator operations: • Postfix and prefix increment and decrement • Assignment • == and != • Dereference • deque and vectoriterators also have <, <=, >, >=, +, -, +=, -+, and []

  20. STL example #include <iostream> #include <vector> #include <string> using namespace std; intmain() { intcount, i; vector<string> words; vector<string>::iterator index; string word; cout << "How many words will you enter? "; cin >> count; for( i = 0; i < count; i++ ) { cin >> word; words.push_back( word ); } for( index = words.begin(); index != words.end(); index++ ) cout << *index << endl; return 0; }

  21. Algorithms • Shuffle • Find • Sort • Count • Always use the ones provided by the container, if available • Functors provided in <functional>

  22. Lab 14

  23. Upcoming

  24. Next time… • Review up to Exam 1 • IDEA evaluations

  25. Reminders • Keep working on Project 6

More Related