diff --git a/tutorials/learn-cpp.org/en/Generic Programming.md b/tutorials/learn-cpp.org/en/Generic Programming.md index c8421bda..485fefc6 100644 --- a/tutorials/learn-cpp.org/en/Generic Programming.md +++ b/tutorials/learn-cpp.org/en/Generic Programming.md @@ -8,24 +8,24 @@ needs to be swapped. In the code below, `Swap` can be used as-is to swap two int #include using namespace std; - template - void Swap(T &a, T&b) + template + void Swap(T& a, T& b) { - T temp = a; - a = b; - b = temp; + T temp = a; + a = b; + b = temp; } - + int main() { string hello = "world!", world = "Hello, "; Swap(world, hello); - cout << hello << world << endl; - + cout << hello << world << endl; + int a = 5, b = 11; Swap(a, b); - cout << "a:" << a << " b:" << b << endl; - return 0; + cout << "a: " << a << " b: " << b << endl; + return 0; } ### Templates, Classes and Operator Overloading @@ -39,50 +39,50 @@ Tutorial Code #include using namespace std; - + class Point; std::ostream& operator<<(std::ostream& out, const Point& c); - - template - void Swap(T &a, T&b) + + template + void Swap(T& a, T& b) { T temp = a; a = b; - b = temp; + b = temp; } - + class Point { // Your code goes here - + Point& operator=(Point rhs) { // your code goes here - + } }; - - int main() { - - Point p1(5,5), p2(100, 100); - Swap (p1, p2); - + + int main() + { + Point p1(5, 5), p2(100, 100); + Swap(p1, p2); + cout << p1 << p2 << std::endl; return 0; } - + std::ostream& operator<<(std::ostream& out, const Point& c) { - out<< "x:" << c.x << " "; - out<< "y:" << c.y << "\n"; - return out; + out << "x: " << c.x << " "; + out << "y: " << c.y << "\n"; + return out; } Expected Output --------------- - p1: x:100 y:100 - p2: x:5 y:5 + p1: x: 100 y: 100 + p2: x: 5 y: 5 Solution -------- @@ -93,37 +93,44 @@ Solution class Point; std::ostream& operator<<(std::ostream& out, const Point& c); - template - void Swap(T & a, T & b) //"&" passes parameters by reference + template + void Swap(T& a, T& b) // "&" passes parameters by reference { - T temp = b; - b = a; - a = temp; + T temp = b; + b = a; + a = temp; } - class Point { + class Point + { public: int x, y; - Point (int c1, int c2) { x = c1; y = c2;} - Point& operator=(Point rhs) { - x = rhs.x; y = rhs.y; + Point(int c1, int c2) + { + x = c1; + y = c2; + } + + Point& operator=(Point rhs) + { + x = rhs.x; + y = rhs.y; return *this; } }; int main() { - - Point p1(5,5), p2 (100,100); - Swap (p1, p2); + Point p1(5, 5), p2(100, 100); + Swap(p1, p2); cout << "p1: " << p1 << "p2: " << p2 << endl; return 0; } std::ostream& operator<<(std::ostream& out, const Point& c) { - out<< "x:" << c.x << " "; - out<< "y:" << c.y << "\n"; - return out; + out << "x: " << c.x << " "; + out << "y: " << c.y << "\n"; + return out; } diff --git a/tutorials/learn-cpp.org/en/Inheritance.md b/tutorials/learn-cpp.org/en/Inheritance.md index 247ef0bc..2f66e02b 100644 --- a/tutorials/learn-cpp.org/en/Inheritance.md +++ b/tutorials/learn-cpp.org/en/Inheritance.md @@ -9,42 +9,49 @@ Let's extend the `class Point` from the previous tutorial to handle __Complex__ class Point; std::ostream& operator<<(std::ostream& out, const Point& c); - template - void Swap(T &a, T &b) { T temp = a; a = b; b = temp; } + template + void Swap(T& a, T& b){ T temp = a; a = b; b = temp; } - class Point { + class Point + { public: int x, y; - Point (int c1, int c2) { x = c1; y = c2;} - Point& operator=(Point rhs) { - x = rhs.x; y = rhs.y; + Point(int c1, int c2){ x = c1; y = c2; } + + Point& operator=(Point rhs) + { + x = rhs.x; + y = rhs.y; return *this; } }; - class Complex : public Point { - private: - int &real, &imag; - public: - Complex(int r, int i) : Point (r, i), real (x), imag (y) - { cout << "Forming..." << *this; } - + class Complex : public Point + { + private: + int& real; + int& imag; + + public: + Complex(int r, int i) : Point(r, i), real(x), imag(y) + { + cout << "Forming..." << *this; + } }; int main() { - - Complex c1(15, 15), c2 (100, 100); + Complex c1(15, 15), c2(100, 100); return 0; } std::ostream& operator<<(std::ostream& out, const Point& c) { - out<< "x:" << c.x << " "; - out<< "y:" << c.y << "\n"; - return out; + out << "x: " << c.x << " "; + out << "y: " << c.y << "\n"; + return out; } Exercise @@ -77,8 +84,9 @@ Expected Output Forming...x (real): 15 15 y (imag): 30 30 Forming...x (real): 100 100 y (imag): 200 200 - c1: (Point) x:100 y:200 - c2: x:15 y:30 + + c1: (Point) x: 100 y: 200 + c2: x: 15 y: 30 c1: x (real): 100 100 y (imag): 200 200 c2: x (real): 15 15 y (imag): 30 30 @@ -95,53 +103,73 @@ Solution class Complex; - template - void Swap(T &a, T &b) { T temp = a; a = b; b = temp; } + template + void Swap(T& a, T& b) + { + T temp = a; + a = b; + b = temp; + } - class Point { + class Point + { public: int x, y; - Point (int c1, int c2) { x = c1; y = c2;} - Point& operator=(Point rhs) { - x = rhs.x; y = rhs.y; + Point(int c1, int c2) + { + x = c1; + y = c2; + } + + Point& operator=(Point rhs) + { + x = rhs.x; + y = rhs.y; return *this; } }; - class Complex : public Point { - private: - int &real, &imag; - public: - Complex(int r, int i) : Point (r, i), real (x), imag (y) - { cout << "Forming..." << *this; } + class Complex : public Point + { + private: + int& real; + int& imag; + + public: + Complex(int r, int i) : Point(r, i), real(x), imag(y) + { + cout << "Forming..." << *this; + } + friend std::ostream& operator<<(std::ostream& out, const Complex& c); }; std::ostream& operator<<(std::ostream& out, const Complex& c) { - // write obj to stream out << "x (real): " << c.x << " " << c.real << " y (imag): " << c.y << " " << c.imag << endl; + return out; } int main() { - Complex c1(15, 30), c2 (100, 200); - Point& p_c1 = c1; Point& p_c2 = c2; + Complex c1(15, 30), c2(100, 200); + Point& p_c1 = c1; + Point& p_c2 = c2; cout << endl; - - Swap (p_c1, p_c2); // swapping Complex objects as Point objects - - cout << "c1: (Point) " << p_c1 << "c2: " << p_c2 << endl; + + Swap(p_c1, p_c2); + + cout << "c1: (Point) " << p_c1 << "c2: " << p_c2 << endl; cout << "c1: " << c1 << "c2: " << c2 << endl; - + return 0; } std::ostream& operator<<(std::ostream& out, const Point& c) { - out<< "x:" << c.x << " "; - out<< "y:" << c.y << "\n"; + out << "x: " << c.x << " "; + out << "y: " << c.y << "\n"; return out; }