week 14 - wednesday. what did we talk about last time? more c++ new delete differences for...
TRANSCRIPT
![Page 1: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/1.jpg)
CS222Week 14 - Wednesday
![Page 2: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/2.jpg)
Last time
What did we talk about last time? More C++
new delete Differences for structs OOP
![Page 3: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/3.jpg)
Questions?
![Page 4: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/4.jpg)
Project 6
![Page 5: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/5.jpg)
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
![Page 6: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/6.jpg)
C++ Madness
![Page 7: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/7.jpg)
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
![Page 8: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/8.jpg)
Dividing up code header
class Complex{double real;double imaginary;
public:Complex(double realValue = 0, double imaginaryValue = 0); ~Complex(void);
double getReal();double getImaginary();
};
![Page 9: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/9.jpg)
Dividing up code implementation
Complex::Complex(double realValue, double imaginaryValue)
{real = realValue;imaginary = imaginaryValue;
}
Complex::~Complex(void){}
double Complex::getReal(){ return real; }
double Complex::getImaginary(){ return imaginary; }
![Page 10: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/10.jpg)
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;Vampire dracula = club + hippo;
![Page 11: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/11.jpg)
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!)
![Page 12: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/12.jpg)
(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;
![Page 13: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/13.jpg)
(Partial) overloading operators implementation
Complex& Complex::operator=( const Complex& complex ){real = complex.real;imaginary = complex.imaginary;
return *this;}
![Page 14: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/14.jpg)
Programming practice
Let's finish the Complex type Then, we can ask the user to enter
two complex numbers We can do the appropriate operation
with them
![Page 15: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/15.jpg)
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 int countCabbages() const;
Methods can take const arguments void insert(const Coin money);
Methods can take const reference arguments void photograph(const Castle& fortress);
Why take a const reference when references are used to change arguments?
![Page 16: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/16.jpg)
Templates
![Page 17: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/17.jpg)
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
![Page 18: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/18.jpg)
Template method example
template<class T> void exchange(T& a, T& b )
{T temp = a;a = b;b = temp;
}
![Page 19: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/19.jpg)
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
![Page 20: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/20.jpg)
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;}
};
![Page 21: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/21.jpg)
Programming practice
Let's write an ArrayList class with templates!
![Page 22: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/22.jpg)
Quiz
![Page 23: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/23.jpg)
Upcoming
![Page 24: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/24.jpg)
Next time…
STL Lab 14
![Page 25: Week 14 - Wednesday. What did we talk about last time? More C++ new delete Differences for structs OOP](https://reader035.vdocument.in/reader035/viewer/2022062721/56649f1b5503460f94c3192e/html5/thumbnails/25.jpg)
Reminders
Keep working on Project 6 Due next Friday