C++ Testing and Classes G Carl Evans
Catch2 Catch is a testing framework for C++ it is used in many places including in the CS225. The tutorial is here https://github.com/catchorg/Catch2/blob/master/docs/tutorial.md#top
How hard was Adventure 2 code review assignment? Easy Moderate Challenging Unreasonable
How long did Adventure 2 assignment take? Less than 3 hours 3 to 6 hours 6 to 9 hours 9 to 12 hours More than 12 hours
Things you should doing allready Get C++ (Xcode/Visual Studio/gcc) Make a helloworld.cpp program and build it.
Classes in C++ File layout .h .cpp Declare structure of the object including member variables Declare member functions (methods) .cpp Define functions of the class
Constructors Run when an object is created before the object is available to make the object exist in a coherent state. Automatic Default Constructor Automatic Default: created by the compiler Default: takes no arguments Initializes member objects using their default constructor Non-object values undefined Only generated if no constructors written
Constructors User Defined Functions with the same name as the class define constructors Initialization Lists Comma separated list member_variable_(expression) Run before the body of the constructor Default Arguments Provide values for missing agruments explicit StudentRecord(std::string name, int grade = 0); StudentRecord::StudentRecord(std::string name, int grade) : name_(name), grade_(grade) { //body }
Gradebook class
What System do you use for this class? Windows Macintosh Linux More than one of the above
What development environment are you setting up for C++? Visual Studio Xcode Editors and command line Visual Studio Code and Clang or gcc Other (Eclipse/Clion/???) What is a development environment?
Operator Overloading Make code more readable and intuitive by allowing more natural expression Change the behavior of many of the standard operators based on the types that are being used. We have seen the overload of [] with map and vector We have seen the overload of -> and * with the iterator in map
Operators that can be overloaded in C++ Operator Category Operators Arithmetic + - * / % ++ -- Bitwise & | ^ ~ >> << Relational < <= > >= == != Logical ! && || Assignment = += -= *= /= %= ˆ= &= |= >>= <<= <= >= Other () [] -> , ->
Stream extraction and insertion std::ostream& operator<<(std::ostream& os, const T& obj){ // write obj to stream return os; } std::istream& operator>>(std::istream& is, T& obj){ // read obj from stream if( /* T could not be constructed */ ) is.setstate(std::ios::failbit); return is;
Student Record