1 / 38

CS222

Week 14 - Wednesday. CS222. Last time. What did we talk about last time? More C++ new d elete Differences for structs Lab 13. Questions?. Project 6. Quotes.

ledell
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 - Wednesday CS222

  2. Last time • What did we talk about last time? • More C++ • new • delete • Differences for structs • Lab 13

  3. Questions?

  4. Project 6

  5. Quotes If you think C++ is not overly complicated, just what is a protected abstract virtual base pure virtual private destructor, and when was the last time you needed one? Tom Cargill

  6. C++ Warmup • Let's write a complete C++ program that reads in: • A string • An integer • Then, it prints out the string however many times the integer specified

  7. OOP in C++

  8. Object Oriented Programming • Let's see how objects work in C++ by looking at classically important elements of OOP • Encapsulation • Dynamic dispatch • Polymorphism • Inheritance • Self-reference

  9. Encapsulation • Information hiding • We want to bind operations and data tightly together • Consequently, we don't want you to touch our privates • Encapsulation in C++ is provided by the private and protected keywords • Unlike Java, you mark sections as public, private, or protected, not individual members and methods • Hardcore OOP people think that all data should be private and most methods should be public

  10. Encapsulation example class A { private: int a; public: intgetA() { return a; } voidsetA(int value) { a = value; } };

  11. Inheritance • Allows code reuse • Is thought of as an "is-a" relationship • C++ allows multiple inheritance, but you usually only use it if you know what you're doing, usually as part of a design pattern • Deriving a subclass usually means creating a "refined" or "more specific" version of a superclass

  12. Inheritance example class B : public A { //has member and methods from A }; class C : public A { private: //has A stuff and more intc; public: intgetC(){ return c; } void increment() { c++; } };

  13. Polymorphism • A confusing word whose underlying concept many programmers misunderstand • Polymorphism is when code is designed for a superclass but can be used with a subclass • If AudiRS5 is a subtype of Car, then you can use an AudiRS5 where you could use a Car

  14. Polymorphism example void drive( Car* c ); //defined somewhere … class AudiRS5 : public Car {}; … Car car; AudiRS5 audi; drive( &audi ); //okay drive( &car ); //okay

  15. Dynamic dispatch • Polymorphism can be used to extend the functionality of an existing method using dynamic dispatch • In dynamic dispatch, the method that is actually called is not known until run time

  16. Dynamic dispatch example class A { public: virtualvoid print() { cout << "A"; } }; class B : public A { public: void print() { cout << "B"; } };

  17. Dynamic dispatch example A a; B b; A* p; a.print(); // A b.print(); // B p = &a; p->print(); // A p = &b; p->print(); // B

  18. Self-reference • Objects are able to refer to themselves • This can be used to explicitly reference variables in the class • Or, it can be used to provide the object itself as an argument to other methods • Self-reference in C++ is provided in part through the this keyword • this is a pointer to the object you're inside of

  19. Self reference example class Stuff { private: int things; public: voidsetThings(int things) { this->things = things; } };

  20. Self reference example classSelfAdder { public: voidaddToList(List& list) { list.add(this); } };

  21. C++ Madness

  22. Dividing up code • In industrial strength C++ code, the class declaration is usually put in a header file (.h) while the class definition is in an implementation file (.cpp) • Benefits: • Easy to see members and methods • Header files can be sent to clients without divulging class internals • Separate compilation (faster) • Easier to take care of circular dependencies

  23. Dividing up code header class Complex { double real; double imaginary; public: Complex(doublerealValue = 0, doubleimaginaryValue = 0); ~Complex(void); doublegetReal(); doublegetImaginary(); };

  24. Dividing up code implementation Complex::Complex(doublerealValue, doubleimaginaryValue) { real = realValue; imaginary = imaginaryValue; } Complex::~Complex(void) {} double Complex::getReal() { return real; } double Complex::getImaginary() { return imaginary; }

  25. 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;

  26. 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!)

  27. (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;

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

  29. 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?

  30. Templates

  31. 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

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

  33. 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

  34. 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; } };

  35. Quiz

  36. Upcoming

  37. Next time… • STL • Lab 14

  38. Reminders • Keep working on Project 6 • Due next Friday

More Related