<|start_filename|>02/Main.cpp<|end_filename|>
#include "MirrorString.h"
#include "PrintRecord.h"
#include "PrintRecords.h"
#include "VectorExample1.h"
#include "VectorExample2.h"

using namespace samples;

int main()
{
	// 1. Mirror String
	MirrorStringExample();

	// 2. StringStream
	PrintRecordExample();

	// 3. File I/O
	ManageRecordsExample();

	// 4. Vector(Header/Cpp)
	VectorExample1();

	// 5. Vector with Member Functions
	VectorExample2();

	return 0;
}

<|start_filename|>02/PrintRecord.cpp<|end_filename|>
#include <iostream>
#include <sstream>
#include <string>
#include "PrintRecord.h"

using namespace std;

namespace samples
{
	void PrintRecordExample()
	{
		cout << "+------------------------------+" << endl;
		cout << "|     Print Record Example     |" << endl;
		cout << "+------------------------------+" << endl;

		string firstName;
		string lastName;
		string studentID;
		int score;

		istringstream inputStream("<NAME> A12345678 80");
		ostringstream outputStream;

		inputStream >> firstName >> lastName >> studentID >> score;
		outputStream << firstName << " " << lastName << " " << studentID << " " << score;
		cout << outputStream.str() << endl;
	}
}


<|start_filename|>03/Vector.cpp<|end_filename|>
#include <iostream>
#include "Vector.h"

namespace samples
{
	Vector::Vector()
		: Vector(0, 0)
	{
	}

	Vector::Vector(int x, int y)
		: mX(x)
		, mY(y)
	{
	}

	int Vector::GetX() const
	{
		return mX;
	}

	void Vector::SetX(int x)
	{
		mX = x;
	}

	void Vector::SetY(int y)
	{
		mY = y;
	}

	int Vector::GetY() const
	{
		return mY;
	}

	bool Vector::IsEqual(const Vector& v) const
	{
		return (mX == v.mX && mY == v.mY);
	}

	Vector Vector::Multiply(const Vector& v) const
	{
		Vector result(mX * v.GetX(), mY * v.GetY());

		return result;
	}

	Vector Vector::Multiply(int multiplier) const
	{
		Vector result(mX * multiplier, mY * multiplier);

		return result;
	}

	void Vector::Scale(const Vector& v)
	{
		mX *= v.mX;
		mY *= v.mY;
	}

	void Vector::Scale(int multiplier)
	{
		mX *= multiplier;
		mY *= multiplier;
	}
}


<|start_filename|>06/PointerVectorExample.cpp<|end_filename|>
#include <iostream>
#include <vector>
#include "PointerVectorExample.h"
#include "Score.h"

using namespace std;

namespace samples
{
	void PointerVectorExample()
	{
		vector<Score*> scores;
		scores.reserve(5);

		Score* cppScore = new Score(30, "C++");
		Score* algoScore = new Score(59, "Algorithm");
		Score* javaScore = new Score(87, "Java");
		Score* dataCommScore = new Score(74, "Data Comm");
		Score* androidScore = new Score(41, "Android");

		scores.push_back(cppScore);
		scores.push_back(algoScore);
		scores.push_back(javaScore);
		scores.push_back(dataCommScore);
		scores.push_back(androidScore);

		PrintVector(scores);

		vector<Score*>::iterator iter = scores.begin();
		while (iter != scores.end())
		{
			Score* score = *iter;
			if (score->GetClassName() == "Java")
			{
				iter = scores.erase(iter);
			}
			else
			{
				iter++;
			}
		}

		PrintVector(scores);

		for (vector<Score*>::iterator iter = scores.begin(); iter != scores.end(); ++iter)
		{
			Score* score = *iter;

			if (score->GetScore() == 30)
			{
				score->SetScore(100);
			}
		}

		cout << "After chaning the score of class 1" << endl;
		PrintVector(scores);

		delete cppScore;
		delete algoScore;
		delete javaScore;
		delete dataCommScore;
		delete androidScore;
	}

	void PrintVector(const vector<Score*>& scores)
	{
		for (vector<Score*>::const_iterator iter = scores.begin(); iter != scores.end(); iter++)
		{
			Score* score = *iter;
			score->PrintVariables();
		}
		cout << endl;
	}
}

<|start_filename|>04/Tiger.h<|end_filename|>
#pragma once

#include "Animal.h"

namespace samples
{
	class Tiger : public Animal
	{
	public:
		Tiger::Tiger(int age);

		void PretendToBeZebra() const;
	};
}

<|start_filename|>03/InheritanceExample.cpp<|end_filename|>
#include <iostream>
#include "Animal.h"
#include "Cat.h"
#include "Dog.h"
#include "InheritanceExample.h"

using namespace std;

namespace samples
{
	void InheritanceExample()
	{
		cout << "+------------------------------+" << endl;
		cout << "|      Inheritance Example     |" << endl;
		cout << "+------------------------------+" << endl;

		Cat cat(3);
		Dog dog(5);

		cout << "Cat's Age: " << cat.GetAge() << endl;
		cout << "Cat's Purring: " << cat.Purr() << endl;

		cout << "Dog's Age: " << dog.GetAge() << endl;
		cout << "Dog's Barking: " << dog.Bark() << endl;
	}
}

<|start_filename|>01/AddIntegers.h<|end_filename|>
#pragma once

namespace samples
{
	void AddIntegersExample();
}


<|start_filename|>06/VectorCalculatingSumExample.cpp<|end_filename|>
#include <iostream>
#include <vector>
#include "VectorCalculatingSumExample.h"

using namespace std;

namespace samples
{
	int CalculateSum(const vector<int>& scores)
	{
		int sum = 0;

		for (size_t i = 0; i < scores.size(); i++)
		{
			sum += scores[i];
		}

		return sum;
	}

	void VectorCalculatingSumExample()
	{
		vector<int> myScores;
		myScores.reserve(5);

		vector<int> yourScores;
		yourScores.reserve(5);

		myScores.push_back(13);
		myScores.push_back(21);
		myScores.push_back(35);
		myScores.push_back(76);
		myScores.push_back(94);

		yourScores.push_back(61);
		yourScores.push_back(27);
		yourScores.push_back(83);
		yourScores.push_back(91);
		yourScores.push_back(100);

		yourScores.clear();

		cout << "myScores" << endl;
		cout << "Sum : " << CalculateSum(myScores) << endl << endl;

		cout << "yourScores" << endl;
		cout << "Sum : " << CalculateSum(yourScores) << endl << endl;
	}
}

<|start_filename|>02/Vector1.h<|end_filename|>
#pragma once

class Vector1
{
public:
	Vector1();
	Vector1(int x, int y);

private:
	int mX;
	int mY;
};

<|start_filename|>02/Vector2.cpp<|end_filename|>
#include "Vector2.h"

Vector2::Vector2()
	: mX(0)
	, mY(0)
{
}

Vector2::Vector2(int x, int y)
	: mX(x)
	, mY(y)
{
}

int Vector2::GetX() const
{
	return mX;
}

int Vector2::GetY() const
{
	return mY;
}

void Vector2::SetX(int x)
{
	mX = x;
}

void Vector2::SetY(int y)
{
	mY = y;
}

void Vector2::Add(const Vector2& vector)
{
	mX += vector.mX;
	mY += vector.mY;
}


<|start_filename|>04/Animal.h<|end_filename|>
#pragma once

namespace samples
{
	class Animal
	{
	public :
		Animal(int age);
		virtual ~Animal();
		
		virtual void Move() const;
		virtual void Speak() const;

		int GetAge() const;

	private:
		int mAge;
	};
}

<|start_filename|>04/PolymorphismExample.cpp<|end_filename|>
#include "Animal.h"
#include "Cat.h"
#include "Dog.h"
#include "PolymorphismExample.h"

namespace samples
{
	void PolymorphismExample()
	{
		Animal* cat = new Cat(3);
		Animal* dog = new Dog(5);

		cat->Move();
		cat->Speak();

		dog->Move();
		dog->Speak();

		delete cat;
		delete dog;
	}
}

<|start_filename|>04/Main.cpp<|end_filename|>
#include "PolymorphismExample.h"
#include "MultiInheritanceExample.h"
#include "InterfaceExample.h"

using namespace samples;

int main()
{
	// 1. Polymorphism 
	PolymorphismExample();

	// 2. Multiple Inheritance... YUCK
	MultiInheritanceExample();

	// 3. Interface
	InterfaceExample();

	return 0;
}

<|start_filename|>01/ReverseInputString.cpp<|end_filename|>
#include "ReverseInputString.h"
#include <iostream>

using namespace std;

namespace samples
{
	void ReverseInputStringExample()
	{
		cout << "+------------------------------+" << endl;
		cout << "|    Reverse String Example    |" << endl;
		cout << "+------------------------------+" << endl;

		const int LINE_SIZE = 512;
		char line[LINE_SIZE];

		cout << "Please enter a string to reverse" << endl
			<< "or EOF to quit: ";

		cin.getline(line, LINE_SIZE);
		if (cin.fail())
		{
			cin.clear();
			return;
		}

		char* p = line;
		char* q = line + strlen(line) - 1;
		while (p < q)
		{
			char temp = *p;
			*p = *q;
			*q = temp;

			++p;
			--q;
		}
		
		cout << "Reversed string: " << line << endl;
	}
}

<|start_filename|>03/Cat.h<|end_filename|>
#pragma once

#include <string>
#include "Animal.h"

namespace samples
{
	class Cat : public Animal
	{
	public:
		Cat(int age);

		std::string Purr() const;
	};
}

<|start_filename|>06/VectorAddingElementsExample.cpp<|end_filename|>
#include <iostream>
#include <vector>
#include "VectorAddingElementsExample.h"

using namespace std;

namespace samples
{
	void VectorAddingElementsExample()
	{
		vector<int> scores;
		scores.reserve(5);

		scores.push_back(30);
		scores.push_back(50);
		scores.push_back(80);
		scores.push_back(65);
		scores.push_back(73);

		PrintScores(scores);

		scores.pop_back();
		scores.pop_back();

		PrintScores(scores);

		scores.resize(10);

		PrintScores(scores);
	}

	void PrintScores(const vector<int>& scores)
	{
		cout << "Current elements : ";
		for (vector<int>::const_iterator iter = scores.begin(); iter != scores.end(); ++iter)
		{
			cout << *iter << " ";
		}
		cout << endl;

		cout << "Current capacity : " << scores.capacity() << endl;
		cout << "Current size : " << scores.size() << endl << endl;
	}
}


<|start_filename|>02/MirrorString.cpp<|end_filename|>
#include <iostream>
#include <string>
#include "MirrorString.h"

using namespace std;

namespace samples
{
	void MirrorStringExample()
	{
		cout << "+------------------------------+" << endl;
		cout << "|    Mirror String Example     |" << endl;
		cout << "+------------------------------+" << endl;

		string line = "Hello World!";

		cout << "string to mirror: " << line << endl;

		for (int i = (int)line.size() - 1; i >= 0; --i)
		{
			line += line[i];
		}
		cout << "mirrored string" << line << endl;
	}
}

<|start_filename|>06/VectorSwappingElementsExample.cpp<|end_filename|>
#include <iostream>
#include <vector>
#include "VectorSwappingElementsExample.h"
#include "VectorAddingElementsExample.h"

using namespace std;

namespace samples
{
	void VectorSwappingElementsExample()
	{
		vector<int> scores;
		scores.reserve(2);

		scores.push_back(85);
		scores.push_back(73);

		cout << "scores" << endl;
		PrintScores(scores);

		vector<int> anotherScores;
		anotherScores.assign(7, 100);

		scores.swap(anotherScores);

		cout << "scores" << endl;
		PrintScores(scores);

		cout << "anotherScores" << endl;
		PrintScores(anotherScores);
	}
}

<|start_filename|>03/Vector.h<|end_filename|>
#pragma once

namespace samples
{
	class Vector
	{
	public:
		Vector();
		Vector(int x, int y);

		int GetX() const;
		void SetX(int x);

		int GetY() const;
		void SetY(int y);
		
		bool IsEqual(const Vector& v) const;
		Vector Multiply(const Vector& v) const;
		Vector Multiply(int multiplier) const;

		void Scale(const Vector& v);
		void Scale(int multiplier);

	private:
		int mX;
		int mY;
	};
}


<|start_filename|>02/VectorExample1.cpp<|end_filename|>
#include <iostream>
#include "Vector1.h"
#include "VectorExample1.h"

using namespace std;

namespace samples
{
	void VectorExample1()
	{
		cout << "+------------------------------+" << endl;
		cout << "|       Vector Example 1       |" << endl;
		cout << "+------------------------------+" << endl;

		Vector1 v1;
		Vector1 v2(10, 55);
	}
}

<|start_filename|>03/String.h<|end_filename|>
#pragma once

namespace samples
{
	class String
	{
	public:
		String(const char* str);
		String(const String& str);
		~String();

		void Print();

	private:
		char* mString;
		size_t mSize;
	};
}


<|start_filename|>03/String.cpp<|end_filename|>
#include <iostream>
#include <string>
#include "String.h"

using namespace std;

namespace samples
{
	String::String(const char* str)
		: mSize(strlen(str) + 1)
	{
		mString = new char[mSize];
		memcpy(mString, str, mSize);
	}

	String::String(const String& str)
		: mSize(str.mSize)
	{
		mString = new char[mSize];
		memcpy(mString, str.mString, mSize);
	}

	String::~String()
	{
		delete[] mString;
	}

	void String::Print()
	{
		cout << "Member string : " << mString << endl;
	}
}

<|start_filename|>02/Vector2.h<|end_filename|>
#pragma once

class Vector2
{
public:
	Vector2();
	Vector2(int x, int y);
	
	int GetX() const;
	int GetY() const;
	void SetX(int x);
	void SetY(int Y);

	void Add(const Vector2& vector);
	
private:
	int mX;
	int mY;
};

<|start_filename|>04/Animal.cpp<|end_filename|>
#include <iostream>
#include "Animal.h"

namespace samples
{
	Animal::Animal(int age)
		: mAge(age)
	{
	}

	Animal::~Animal()
	{
	}

	void Animal::Move() const
	{
		std::cout << "An animal is moving" << std::endl;
	}

	void Animal::Speak() const
	{
		std::cout << "An animal is speaking" << std::endl;
	}

	int Animal::GetAge() const
	{
		return mAge;
	}
}


<|start_filename|>02/PrintRecord.h<|end_filename|>
#pragma once

namespace samples
{
	void PrintRecordExample();
}


<|start_filename|>01/ReverseInputString.h<|end_filename|>
#pragma once

namespace samples
{
	void ReverseInputStringExample();
}

<|start_filename|>04/Cat.h<|end_filename|>
#pragma once

#include "Animal.h"

namespace samples
{
	class Cat : public Animal
	{
	public:
		Cat(int age);

		virtual void Move() const;
		virtual void Speak() const;
	};
}


<|start_filename|>01/Swap.h<|end_filename|>
#pragma once

namespace samples
{
	void SwapExample();
	void Swap(int& number1, int& number2);
}

<|start_filename|>03/Cat.cpp<|end_filename|>
#include <iostream>
#include "Cat.h"

using namespace std;

namespace samples
{
	Cat::Cat(int age)
		: Animal(age)
	{
	}

	string Cat::Purr() const
	{
		return string("Purr");
	}
}

<|start_filename|>01/AddIntegers.cpp<|end_filename|>
#include "AddIntegers.h"
#include <iostream>

using namespace std;

namespace samples
{
	void AddIntegersExample()
	{
		cout << "+------------------------------+" << endl;
		cout << "|     Add Integers Example     |" << endl;
		cout << "+------------------------------+" << endl;

		int number;
		int sum = 0;

		while (true)
		{
			cout << "Please enter an integer or EOF: ";
			cin >> number;
			if (cin.eof())
			{
				break;
			}

			if (cin.fail())
			{
				cout << "Invalid input" << endl;
				cin.clear();
				cin.ignore(LLONG_MAX, '\n');
				continue;
			}
			sum += number;
		}
		cin.clear();

		cout << "The sum is " << sum << endl;
	}
}

<|start_filename|>01/Swap.cpp<|end_filename|>
#include <iostream>
#include "Swap.h"

using namespace std;

namespace samples
{
	void SwapExample()
	{
		cout << "+------------------------------+" << endl;
		cout << "|         Swap Example         |" << endl;
		cout << "+------------------------------+" << endl;
		int number1 = 1;
		int number2 = 2;

		cout << "Before swap: " << number1 << ", " << number2 << endl;
		Swap(number1, number2);
		cout << "After swap: " << number1 << ", " << number2 << endl;
	}

	void Swap(int& number1, int& number2)
	{
		int temp = number1;
		number1 = number2;
		number2 = temp;
	}
}


<|start_filename|>02/VectorExample2.cpp<|end_filename|>
#include <iostream>
#include "Vector2.h"
#include "VectorExample2.h"

using namespace std;

namespace samples
{
	void VectorExample2()
	{
		cout << "+------------------------------+" << endl;
		cout << "|       Vector Example 2       |" << endl;
		cout << "+------------------------------+" << endl;

		Vector2 v1;
		Vector2 v2(13, 4);

		cout << "v1: (" << v1.GetX() << ", " << v1.GetY() << ")" << endl;
		cout << "v2: (" << v2.GetX() << ", " << v2.GetY() << ")" << endl;

		v1.SetX(10);
		v1.SetY(20);

		v2.SetX(30);
		v2.SetY(40);

		cout << "v1: (" << v1.GetX() << ", " << v1.GetY() << ")" << endl;
		cout << "v2: (" << v2.GetX() << ", " << v2.GetY() << ")" << endl;

		v1.Add(v2);

		cout << "v1: (" << v1.GetX() << ", " << v1.GetY() << ")" << endl;
	}
}