Advertisement
bwukki

CS162 exam notes

May 9th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. //Cs162 exam notes
  2.  
  3. Good Practices
  4. If a function is not changing anything, declare it const
  5. Make getters and setters for EVERY class and USE them - this future proofs your code
  6. Have a copy class for your classes to assign one object to another
  7. Pass by reference whenever passing data structures
  8. When you overload the << operator it cannot be a member function of the class (declare friend if it needs to access private info)
  9. Only make something a friend of a class IF it needs to directly access private stuff
  10.  
  11. Pointer: int*
  12. Dereference: *x
  13. vector<int*> x{};
  14. v.push_back(new int{23});
  15. cout << *v[0] << endl;
  16. v[0]->method(thing);
  17. When you dynamically allocate memory you get back a pointer
  18.  
  19. Common includes/include guard
  20. #ifndef INTEGERSET_H //include guard
  21. #define INTEGERSET_H
  22. #include <vector>//vector
  23. #include <iostream> //cout
  24. #include <stdexcept> //exceptions
  25. #include <sstream> //ostream and ostringstream (use .str() on ostringstreams)
  26. #endif // INTEGERSET_H //end include guard
  27.  
  28. Basic Class outline
  29. .h file:
  30.  
  31. #ifndef WEAPON_H
  32. #define WEAPON_H
  33. class Weapon {
  34. public:
  35.     Weapon(const int);
  36.     void setDamage(const int) const;
  37.     int getDamage() const;
  38. private:
  39.     int damage;
  40. };
  41. #endif // WEAPON_H
  42.  
  43. .cpp file:
  44.  
  45. #include "Weapon.h"
  46.  
  47. Weapon::Weapon(const int in) {
  48. }
  49.  
  50. void Weapon::setDamage(const int in) const {
  51. }
  52.  
  53. int Weapon::getDamage() const {
  54. }
  55.  
  56. Operator Overloading       
  57. object operator+(object input) {
  58.     object result{};
  59.     result.thing = this->thing + input.thing;
  60.     return result;
  61. }
  62.  
  63. std::ostream& operator<<(std::ostream& oin,Rational in) { //overloads left shift operator
  64.     oin << in.toString();
  65.     return oin;
  66. }
  67. // Define prefix increment operator.  
  68. Point& Point::operator++()  
  69. {  
  70.    _x++;  
  71.    _y++;  
  72.    return *this;  
  73. }  
  74.  
  75. // Define postfix increment operator.  
  76. Point Point::operator++(int)  
  77. {  
  78.    Point temp = *this;  
  79.    ++*this;  
  80.    return temp;  
  81. }  
  82.  
  83.  
  84. Looping structures
  85. for (int i = 0; i < target; i++) {
  86. }
  87.  
  88. do {
  89. stuff;
  90. } while (condition);
  91.  
  92. while(condition) {
  93.    statement(s);
  94. }
  95. Dynamic memory allocation:
  96.     Array<int*,10> XXX{nullptr}; //when we use new it always gives us a pointer to an object
  97.     XXX[0] = new int{234}; //allocates memory for a new int in this array
  98.     Delete XXX[0]; //deletes element of array, array still exists
  99.     XXX[0] = nullptr; //wipes memory where that element was
  100. Errors:
  101. #include <stdexcept>
  102. throw std::invalid_argument(“invalid input”);
  103.     try{
  104.         Clock x{43201};
  105.     }
  106.     catch (invalid_argument& e) {
  107.         cout << "An exception occurred I think, here's what it might say:" << e.what() << endl;
  108.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement