银行账户类,头文件
#ifndef HEAD_H_
#define HEAD_H_
#include<iostream>
#include<string>
using std::cout;
using std::endl;
using std::string;
namespace BANKID
{
class BankID
{
public:
BankID(const string&, const double = 0);
void show(void)const;
void add(const double);
void get(const double);
~BankID();
private:
static int sm_makeID;
string m_name;
int m_ID;
double m_depoist;
};
}
#endif // !HEAD_H_
源文件
namespace BANKID
{
BankID::BankID(const string& name, const double deposit):m_name(name),m_depoist(deposit)
{
this->m_ID=BankID::sm_makeID;
++BankID::sm_makeID;
}
BANKID::BankID::~BankID()
{
--BankID::sm_makeID;
}
void BANKID::BankID::show(void)const
{
cout << this->m_name << " " << this->m_ID << " " << this->m_depoist << endl;
}
void BANKID::BankID::add(const double deposit)
{
this->m_depoist += deposit;
}
void BANKID::BankID::get(const double deposit)
{
this->m_depoist -= deposit;
}
int BankID::sm_makeID = 1000;
}
时间类,头文件
#ifndef HEAD_H_
#define HEAD_H_
#include<iostream>
using std::cout;
using std::endl;
using std::ostream;
namespace TIME
{
class Time
{
friend ostream& operator<<(ostream&, const Time&);
public:
Time(void);
Time(int, int minutes=0);
const Time operator+(const Time&)const;
const Time operator-(const Time&)const;
const Time operator*(const int)const;
const Time operator/(const int)const;
Time& operator=(const Time&);
Time& operator+=(const Time&);
Time& operator-=(const Time&);
Time& operator*=(const int);
Time& operator/=(const int);
const bool operator<(const Time&)const;
const bool operator<=(const Time&)const;
const bool operator>(const Time&)const;
const bool operator>=(const Time&)const;
int change_minutes(void)const;
void show(void)const;
private:
//运算辅助函数,把一个分制时间改成时分制时间
inline const Time count(const int minutes)const
{
Time temp;
temp.m_hours = minutes / 60;
temp.m_minutes = minutes % 60;
return temp;
}
//时分制时间转分制时间
inline const int change(void)const
{
int minutes = this->m_hours * 60 + this->m_minutes;
return minutes;
}
int m_hours;
int m_minutes;
};
}
#endif // !HEAD_H_
源文件
namespace TIME
{
//构造
TIME::Time::Time()
{
this->m_hours = this->m_minutes = 0;
}
TIME::Time::Time(int hours, int minutes)
{
if (minutes > 60)
exit(EXIT_FAILURE);
this->m_hours = hours;
this->m_minutes = minutes;
}
//重载算数运算
const Time TIME::Time::operator+(const Time& t) const
{
unsigned minutes = this->change() + t.change();
return count(minutes);
}
const Time TIME::Time::operator-(const Time& t) const
{
int minutes = this->change() - t.change();
if (minutes < 0)
exit(EXIT_FAILURE);
return count(minutes);
}
const Time TIME::Time::operator*(const int x) const
{
if (x < 0)
exit(EXIT_FAILURE);
int minutes = this->change() * x;
return count(minutes);
}
const Time TIME::Time::operator/(const int x) const
{
if (x <= 0)
exit(EXIT_FAILURE);
int minutes = this->change() / x;
return count(minutes);
}
//重载(复合)赋值运算
Time& TIME::Time::operator=(const Time& t)
{
this->m_hours = t.m_hours;
this->m_minutes = t.m_hours;
return *this;
}
Time& TIME::Time::operator+=(const Time& t)
{
return *this = *this + t;
}
Time& TIME::Time::operator-=(const Time& t)
{
return *this = *this - t;
}
Time& TIME::Time::operator*=(const int x)
{
return *this = (*this) * x;
}
Time& TIME::Time::operator/=(const int x)
{
return *this = (*this) / x;
}
//重载条件运算
const bool TIME::Time::operator<(const Time& t) const
{
return this->change() < t.change();
}
const bool TIME::Time::operator>(const Time& t) const
{
return this->change()> t.change();
}
const bool TIME::Time::operator<=(const Time& t) const
{
return!(*this > t);
}
const bool TIME::Time::operator>=(const Time& t) const
{
return !(*this < t);
}
int TIME::Time::change_minutes(void) const
{
return this->change();
}
ostream& TIME::operator<<(ostream& mycout, const Time& t)
{
mycout << t.m_hours <<"时" << t.m_minutes<<"分";
return mycout;
}
void TIME::Time::show() const
{
cout << this->m_hours << " " << this->m_minutes << endl;
}
}
复数类,头文件
#ifndef COMPLEX_H_
#define COMPLEX_H_
#include<iostream>
using std::ostream;
using std::istream;
using std::cout;
using std::cin;
namespace COMPLEX
{
class Complex
{
friend ostream& operator<<(ostream&, const Complex&);
friend istream& operator>>(istream&, Complex&);
friend const Complex operator+(const double, const Complex&);
friend const Complex operator-(const double, const Complex&);
friend const Complex operator*(const double, const Complex&);
public:
Complex();
Complex(double,double);
//隐式类型转换,重载>>无线递归??
Complex(double);//转换!!!
const Complex operator+(const Complex&)const;
const Complex operator-(const Complex&)const;
const Complex operator*(const Complex&)const;
Complex& operator~();
private:
double m_true;
double m_imaginary;
};
}
#endif // !COMPLEX_H_
源文件
namespace COMPLEX
{
COMPLEX::Complex::Complex() :m_true(0.0), m_imaginary(0.0)
{
}
COMPLEX::Complex::Complex(double a, double b) :m_true(a), m_imaginary(b)
{
}
COMPLEX::Complex::Complex(double a) :m_true(a),m_imaginary(0.0){}
//成员运算
const Complex COMPLEX::Complex::operator+(const Complex& c) const
{
Complex temp(*this);
temp.m_true += c.m_true;
temp.m_imaginary += c.m_imaginary;
return temp;
}
const Complex COMPLEX::Complex::operator-(const Complex& c) const
{
Complex temp(*this);
temp.m_true -= c.m_true;
temp.m_imaginary -= c.m_imaginary;
return temp;
}
const Complex COMPLEX::Complex::operator*(const Complex& c) const
{
Complex temp;
temp.m_true = this->m_true * c.m_true - this->m_imaginary * c.m_imaginary;
temp.m_imaginary = this->m_imaginary * c.m_true + this->m_true * c.m_imaginary;
return temp;
}
Complex& COMPLEX::Complex::operator~()
{
if (!this->m_imaginary)
return *this;
this->m_imaginary = -this->m_imaginary;
return *this;
}
//友元运算
const Complex COMPLEX::operator+(const double n, const Complex& c)
{
Complex temp(c);
temp.m_true += n;
return temp;
}
const Complex COMPLEX::operator-(const double n, const Complex& c)
{
Complex temp(c);
temp.m_true -= n;
return temp;
}
const Complex COMPLEX::operator*(const double n, const Complex& c)
{
Complex temp(c);
temp.m_true *= n;
return temp;
}
//输入输出
ostream& COMPLEX::operator<<(ostream& mycout, const Complex& c)
{
mycout << '(' << c.m_true << ',' << c.m_imaginary << "!)";
return mycout;
}
istream& COMPLEX::operator>>(istream& mycin, Complex& c)
{
cout << "请输入实部: ";
mycin >> c.m_true;
cout << "请输入虚部: ";
mycin >> c.m_imaginary;
return mycin;
}
}