#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
typedef double single;
class SinglePersonProblem : protected exception
{
public:
SinglePersonProblem(string reason = "The biggest problem is that you are Single") throw():m_reason(reason){};
~SinglePersonProblem()throw(){};
const char* what() const throw(){return m_reason.c_str();};
private:
mutable string m_reason;
};
class Person
{
private:
static const single MagicianStartAge;
single single_age;
bool isMagician;
void AddAge(single age);
public:
explicit Person(single age = 21);
Person operator ++(int);
Person& operator ++();
Person& operator + (single age);
double operator+ (const Person& anotherSinglePerson);
double operator* (const Person& anotherSinglePerson);
bool operator==(const Person& anotherSinglePerson) const;
bool operator!=(const Person& anotherSinglePerson) const;
};
const single Person::MagicianStartAge = 30;
Person::Person(single age): single_age(0), isMagician(false)
{
if (age < 0)
{
SinglePersonProblem problem("An \"anti-single\" person should be a dead person");
throw problem;
}
AddAge(age);
}
Person Person::operator ++(int)
{
Person earlierPerson(single_age);
AddAge(1);
return earlierPerson;
}
Person& Person::operator ++()
{
AddAge(1);
return *this;
}
Person& Person::operator + (single age)
{
AddAge(age);
return *this;
}
double Person::operator+ (const Person& anotherSinglePerson)
{
SinglePersonProblem problem("Two Single Person all together are still Two Single Person! It will never return a double!");
throw problem;
}
double Person::operator* (const Person& anotherSinglePerson)
{
SinglePersonProblem problem("Two Single Person multiple each other are still Two Single Person! It will never return a double!");
throw problem;
}
bool Person::operator==(const Person& anotherSinglePerson) const
{
bool ret = false;
if (NULL != &anotherSinglePerson)
if (this->single_age == anotherSinglePerson.single_age)
{
cout << "Good, you got a buddy now!" << endl;
ret = true;
}
return ret;
}
bool Person::operator!=(const Person& anotherSinglePerson) const
{
return !(*this == anotherSinglePerson);
}
void Person::AddAge(single age)
{
if (age < 0)
{
SinglePersonProblem problem("Forget about that! Don't try to decrease your single age!");
throw problem;
}
single_age += age;
if (single_age >= MagicianStartAge && !isMagician)
{
isMagician = true;
cout << "Congratulations! You're a " << single_age << "-year-old Magician Now!" << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
try
{
Person a, b, c, d(25);
d++;
a*b;
}
catch (SinglePersonProblem& e)
{
cout<< "Exception: " << e.what() << endl;
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
typedef double single;
class SinglePersonProblem : protected exception
{
public:
SinglePersonProblem(string reason = "The biggest problem is that you are Single") throw():m_reason(reason){};
~SinglePersonProblem()throw(){};
const char* what() const throw(){return m_reason.c_str();};
private:
mutable string m_reason;
};
class Person
{
private:
static const single MagicianStartAge;
single single_age;
bool isMagician;
void AddAge(single age);
public:
explicit Person(single age = 21);
Person operator ++(int);
Person& operator ++();
Person& operator + (single age);
double operator+ (const Person& anotherSinglePerson);
double operator* (const Person& anotherSinglePerson);
bool operator==(const Person& anotherSinglePerson) const;
bool operator!=(const Person& anotherSinglePerson) const;
};
const single Person::MagicianStartAge = 30;
Person::Person(single age): single_age(0), isMagician(false)
{
if (age < 0)
{
SinglePersonProblem problem("An \"anti-single\" person should be a dead person");
throw problem;
}
AddAge(age);
}
Person Person::operator ++(int)
{
Person earlierPerson(single_age);
AddAge(1);
return earlierPerson;
}
Person& Person::operator ++()
{
AddAge(1);
return *this;
}
Person& Person::operator + (single age)
{
AddAge(age);
return *this;
}
double Person::operator+ (const Person& anotherSinglePerson)
{
SinglePersonProblem problem("Two Single Person all together are still Two Single Person! It will never return a double!");
throw problem;
}
double Person::operator* (const Person& anotherSinglePerson)
{
SinglePersonProblem problem("Two Single Person multiple each other are still Two Single Person! It will never return a double!");
throw problem;
}
bool Person::operator==(const Person& anotherSinglePerson) const
{
bool ret = false;
if (NULL != &anotherSinglePerson)
if (this->single_age == anotherSinglePerson.single_age)
{
cout << "Good, you got a buddy now!" << endl;
ret = true;
}
return ret;
}
bool Person::operator!=(const Person& anotherSinglePerson) const
{
return !(*this == anotherSinglePerson);
}
void Person::AddAge(single age)
{
if (age < 0)
{
SinglePersonProblem problem("Forget about that! Don't try to decrease your single age!");
throw problem;
}
single_age += age;
if (single_age >= MagicianStartAge && !isMagician)
{
isMagician = true;
cout << "Congratulations! You're a " << single_age << "-year-old Magician Now!" << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
try
{
Person a, b, c, d(25);
d++;
a*b;
}
catch (SinglePersonProblem& e)
{
cout<< "Exception: " << e.what() << endl;
}
return 0;
}