Advertisement
bwukki

Untitled

Feb 13th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. Midterm exam notes: Basic initializer list stuff:
  2.  
  3. #include<iostream>
  4. using namespace std;
  5.  
  6. class Point {
  7. private:
  8.     int x;
  9.     int y;
  10. public:
  11.     Point(int i = 0, int j = 0):x(i), y(j) {}
  12.     /*  The above use of Initializer list is optional as the
  13.         constructor can also be written as:
  14.         Point(int i = 0, int j = 0) {
  15.             x = i;
  16.             y = j;
  17.         }
  18.     */  
  19.      
  20.     int getX() const {return x;}
  21.     int getY() const {return y;}
  22. };
  23.  
  24. int main() {
  25.   Point t1(10, 15);
  26.   cout<<"x = "<<t1.getX()<<", ";
  27.   cout<<"y = "<<t1.getY();
  28.   return 0;
  29. }
  30.  
  31. Basic operator overloading:
  32.  
  33. std::ostream& operator<<(std::ostream& out, const Account& acct) {
  34.     out << acct.toString();
  35.     return out;
  36. >
  37.  
  38. to_string is a thing!
  39.  
  40. you can also do a function in a class like:
  41.  
  42. string toString() {
  43.     string result{};
  44.     something something
  45.     return result;
  46. }
  47.  
  48. also think about getters and setters
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement