NYCU Object-Oriented Programming in C++

DS & OOP 課程的前半學期筆記。

Class

Operator Overloading

cppreference

ExpressionAs member functionAs non-member functionExample
@a(a).operator@ ( )operator@ (a)!std::cin calls std::cin
a@b(a).operator@ (b)operator@ (a, b)std::cout << 42 calls std::cout.operator<<(42)
a=b(a).operator= (b)cannot be non-memberGiven std::string s;, s = "abc"; calls s.operator=("abc")
a(b…)(a).operator()(b…)cannot be non-memberGiven std::random_device r;, auto n = r(); calls r.operator()()
a[b](a).operatorcannot be non-memberGiven std::map<int, int> m;, m[1] = 2; calls m.operator[](1)
a->(a).operator-> ( )cannot be non-memberGiven std::unique_ptr<S> p;, p->bar() calls p.operator->()
a@(a).operator@ (0)operator@ (a, 0)Given std::vector<int>::iterator i;, i++ calls i.operator++(0)

Assignment

1
2
3
4
5
6
// copy assignment
T& operator=(const T& other) { ... }
// move assignment
T& operator=(T&& other) noexcept { ... }
// copy assignment (copy-and-swap idiom)
T& T::operator=(T other) noexcept { ... }

Increment and decrement

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
struct X {
    // ++X, prefix increment
    T& operator++() { ... }
    // X++, postfix increment
    X operator++(int) { ... }
    // --X, prefix decrement
    X& operator--() { ... }
    // X--, postfix decrement
    X operator--(int) { ... }
};

Comparison operators

1
2
3
4
5
6
7
8
struct T {
    int a, b, c;
    friend bool operator<(const T& l, const T& r) {
        return std::tie(l.a, l.c, l.b)
             < std::tie(r.a, r.c, r.b);
        // keep the same order
    }
};

Static Members

declaration 和 definition 分開。cppreference

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class C {
private:
    static int I; // declaration, can be incomplete type
    inline static int J = 2; // since c++17
    static const int K = 3;
    static const int L;
    // constexpr static int M; // CE require initializer
};
int C::I = 1; // definition, need complete type
const int C::L = 4;

Friend Declaration

Non-member functions can access priavate & protected members. cppreference

1
2
3
4
5
6
7
8
9
class C {
private:
    int x;
public:
    C(int _x): x(_x) {}
    friend std::ostream& operator<<(std::ostream& out, const C it) {
        return out << it.x;
    }
};

Inheritance

  • Base Class
  • Derived Class
    • protected same as private, but derived class can access. cppreference
    • not inherited by default
      • constructor
      • destructor
      • assignment operator
        • force inherent, derived class variable will copy
        • move assignment will call base copt assignment
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <bits/stdc++.h>
using namespace std;

class B {
public:
    int x, y;
    B(): x(0x1111) {};
    B(int i): x(i) {};
    B& operator=(const B& r) {
        cout << "=B " << r.x << endl;
        this->x = 0x5555;
        return *this;
    }
};

class D: public B {
public:
    int x;
    D(): x(0x2222) {};
    // y(0x3333) // CE
    using B::B;
    using B::operator=;
    /*
    D& operator=(const D& r) {
        cout << "=D " << r.x << endl;
        return *this;
    }
    //*/
};

int main() {
    cout << hex;
    D d;
    assert(0x2222 == d.x);
    assert(0x1111 == d.B::x);
    D e(0x4444);
    // e.x unknown
    assert(0x4444 == e.B::x);
    e = d;
    assert(0x5555 == e.B::x);
    d = move(e);
    assert(0x5555 == d.B::x);
    return 0;
}
Built with Hugo
Theme Stack designed by Jimmy