Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 52 additions & 45 deletions tutorials/learn-cpp.org/en/Generic Programming.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ needs to be swapped. In the code below, `Swap` can be used as-is to swap two int
#include <iostream>
using namespace std;

template<typename T>
void Swap(T &a, T&b)
template <typename T>
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
Expand All @@ -39,50 +39,50 @@ Tutorial Code

#include <iostream>
using namespace std;

class Point;
std::ostream& operator<<(std::ostream& out, const Point& c);
template<typename T>
void Swap(T &a, T&b)

template <typename T>
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
--------
Expand All @@ -93,37 +93,44 @@ Solution
class Point;
std::ostream& operator<<(std::ostream& out, const Point& c);

template<typename T>
void Swap(T & a, T & b) //"&" passes parameters by reference
template <typename T>
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;
}
112 changes: 70 additions & 42 deletions tutorials/learn-cpp.org/en/Inheritance.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename T>
void Swap(T &a, T &b) { T temp = a; a = b; b = temp; }
template <typename T>
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
Expand Down Expand Up @@ -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
Expand All @@ -95,53 +103,73 @@ Solution

class Complex;

template<typename T>
void Swap(T &a, T &b) { T temp = a; a = b; b = temp; }
template <typename T>
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;
}