From 239dc05410aeba8265f2f46006d7217ec5079ffa Mon Sep 17 00:00:00 2001 From: Ashok Bakthavathsalam Date: Sun, 19 Apr 2026 09:30:24 +0000 Subject: [PATCH 1/3] Fixed #874 --- .../learn-cpp.org/en/Generic Programming.md | 93 ++++++++++--------- 1 file changed, 50 insertions(+), 43 deletions(-) diff --git a/tutorials/learn-cpp.org/en/Generic Programming.md b/tutorials/learn-cpp.org/en/Generic Programming.md index c8421bda3..92b432cfb 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,43 +39,43 @@ 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 @@ -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; } From c9c741e43967ad05509a20c0993a81ce4d9527f1 Mon Sep 17 00:00:00 2001 From: Ashok Bakthavathsalam Date: Mon, 20 Apr 2026 18:02:22 +0000 Subject: [PATCH 2/3] Fixed #874 related output section --- tutorials/learn-cpp.org/en/Generic Programming.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tutorials/learn-cpp.org/en/Generic Programming.md b/tutorials/learn-cpp.org/en/Generic Programming.md index 92b432cfb..485fefc63 100644 --- a/tutorials/learn-cpp.org/en/Generic Programming.md +++ b/tutorials/learn-cpp.org/en/Generic Programming.md @@ -81,8 +81,8 @@ Tutorial Code Expected Output --------------- - p1: x:100 y:100 - p2: x:5 y:5 + p1: x: 100 y: 100 + p2: x: 5 y: 5 Solution -------- From da6da778b9422bf2e32c9c72d8fc1eeae5bc3255 Mon Sep 17 00:00:00 2001 From: Ashok Bakthavathsalam Date: Mon, 20 Apr 2026 18:05:28 +0000 Subject: [PATCH 3/3] Fixed #874 for inheritance.md --- tutorials/learn-cpp.org/en/Inheritance.md | 112 ++++++++++++++-------- 1 file changed, 70 insertions(+), 42 deletions(-) diff --git a/tutorials/learn-cpp.org/en/Inheritance.md b/tutorials/learn-cpp.org/en/Inheritance.md index 247ef0bc2..2f66e02b8 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; }