#include
using namespace std;
//
// 二维座标类简单实现
//
class Vector2d
{
//构造/析构函数
public:
Vector2d()
{}
Vector2d(double _x, double _y)
:x(_x), y(_y)
{}
~Vector2d()
{}
//成员函数
public:
//输出函数
void Show()
{
cout << "x=" << x << ", y=" << y <<"." << endl;
}
//+重载运算符
Vector2d operator + (const Vector2d& v)
{
Vector2d vec;
vec.x = x + v.x;
vec.y = y + v.y;
return vec;
}
private:
double x;
double y;
};
//////////////////////////////////////////////////////////////////////////
//
// 立方体类简单实现
//
class CBox
{
//构造/析构函数
public:
CBox()
{
m_nSideLength=0;
}
~CBox()
{
}
//成员函数
public:
//设置边长
void SetSideLength(unsigned short nSideL)
{
m_nSideLength=nSideL;
}
//获得边长
unsigned short GetSideLength() const
{
return m_nSideLength;
}
//表面积
unsigned short S()
{
return m_nSideLength*m_nSideLength*6;
}
//体积
unsigned short V()
{
return m_nSideLength*m_nSideLength*m_nSideLength;
}
//成员变量
private:
unsigned short m_nSideLength;
};
//////////////////////////////////////////////////////////////////////////
//
// 员工抽象简单实现
//
#define MAX_NAME 64
class Employee
{
//构造/析构函数
public:
Employee()
{
name=0;
ID=0;
name = new char[MAX_NAME];
}
virtual ~Employee()
{
if (name) delete name;
}
public:
//设置员工信息
void SetEmployeeInfo(const char* _name, unsigned short _ID)
{
strncpy(name, _name, sizeof(char)*MAX_NAME);
ID=_ID;
}
//计算员工工资
virtual int GetSalary() = 0;
//员工信息
virtual void ShowEmployeeInfo()
{
if (name[0]!='\0') cout<< "姓名:" << name << ",员工编号:" << ID << ".";
else cout<< "该员工没有任何信息" <
//成员变量
protected:
char* name;
unsigned short ID;
};
//
// 经理类简单实现
//
class Manager : public Employee
{
//构造/析构函数
public:
Manager()
{
sal=0;
}
virtual ~Manager()
{}
//虚函数继承
public:
//员工信息
virtual void ShowEmployeeInfo()
{
cout<< "经理简介" <
}
//计算员工工资
virtual int GetSalary()
{
return sal;
}
//成员函数
public:
//设置薪水
void SetSalary(int salary)
{
sal = salary;
}
//内部成员变量
private:
int sal;
};
//
// 钟点工简单实现
//
class HourlyWorker : public Employee
{
//构造/析构函数
public:
HourlyWorker()
{
wage=0;
hours=0;
}
virtual ~HourlyWorker()
{}
//虚函数继承
public:
//员工信息
virtual void ShowEmployeeInfo()
{
cout<< "钟点工简介" <
}
//计算员工工资
virtual int GetSalary()
{
return wage*hours;
}
//成员函数
public:
//设置薪水
void SetSalary(int _wage, int _hours)
{
wage=_wage;
hours=_hours;
}
//内部成员变量
private:
int wage;
int hours;
};
//////////////////////////////////////////////////////////////////////////
//
// 函数模版绝对值的简单实现
//
template
Type Abs(Type tVar)
{
if (tVar >= 0) return tVar;
else return -tVar;
}
//////////////////////////////////////////////////////////////////////////
//
// 复数类简单实现
//
class CComplex
{
//构造/析构函数
public:
CComplex()
{}
CComplex(int _a, int _b)
:a(_a), b(_b)
{}
~CComplex()
{}
//成员函数
public:
//设置复数实部虚部
void SetComplex(int _a, int _b)
{
a=_a;
b=_b;
}
//输出函数
void Show()
{
char* signal=b>0?"+":"";
cout << "该复数是:" << a << signal << b <<"i" << endl;
}
//+重载运算符
CComplex operator + (const CComplex& c)
{
CComplex complex;
complex.a = a + c.a;
complex.b = b + c.b;
return complex;
}
//成员变量
private:
int a;
int b;
};
//////////////////////////////////////////////////////////////////////////
//
// 简单形状类简单实现
//
class CShape
{
public:
//获取周长
virtual float GetLength()=0;
};
//
// 四边形类简单实现
//
class CSquare : public CShape
{
//构造/析构函数
public:
CSquare()
{
a=b=c=d=0;
}
~CSquare()
{}
//虚函数继承
public:
virtual float GetLength()
{
return (float)(a+b+c+d);
}
//设置边长
public:
void SetSide(int _a, int _b, int _c, int _d)
{
a=_a;
b=_b;
c=_c;
d=_d;
}
//成员变量
private:
int a, b, c, d;
};
//
// 圆类简单实现
//
#define PI 3.14159265
class CCircle : public CShape
{
//构造/析构函数
public:
CCircle()
{
r=0;
}
~CCircle()
{}
//虚函数继承
public:
virtual float GetLength()
{
return (float)(PI*r*2);
}
//设置半径
public:
void SetRadius(int _r)
{
r=_r;
}
//成员变量
private:
int r;
};
//////////////////////////////////////////////////////////////////////////
//
// 时间类简单实现
//
class CDateInfo
{
//构造/析构函数
public:
CDateInfo()
{
year=2010;
month=8;
day=8;
}
CDateInfo(unsigned short _year, unsigned short _month, unsigned short _day)
:year(_year), month(_month), day(_day)
{
}
~CDateInfo()
{}
public:
//设置日期
void SetDateInfo(unsigned short _year, unsigned short _month, unsigned short _day)
{
year=_year;
month=_month;
day=_day;
}
//日期显示
void ShowDateInfo()
{
cout<< "当前日期是:" << year << "年-" << month << "月-" << day << "日" << endl;
}
//成员变量
private:
unsigned short year,month, day;
};
//////////////////////////////////////////////////////////////////////////
int main()
{
cout<< "第2题:演示2d坐标类" <
v1.Show();
v2.Show();
Vector2d v = v1 + v2;
v.Show();
cout<< "-------------------------------" << endl << endl;
cout<< "第3题:演示立方体" << endl;
CBox b;
b.SetSideLength(5);
cout<< "边长是:" << b.GetSideLength() << ",面积=" << b.S() << ",体积=" << b.V() << "." <
cout<< "第4题:演示员工" << endl;
//经理
Manager m;
m.SetEmployeeInfo("张三", 10001);
m.SetSalary(8500);
m.ShowEmployeeInfo();
cout<< " 薪水:" << m.GetSalary() << "." <
//钟点工
HourlyWorker hw;
hw.SetEmployeeInfo("李四", 10002);
hw.ShowEmployeeInfo();
hw.SetSalary(13, 120);
cout<< " 薪水:" << hw.GetSalary() << "." <
cout<< "第5题:演示模版绝对值" << endl;
cout<< "浮点数" << Abs(-3.14159f) << endl;
cout<< "整数" << Abs(96) << endl;
cout<< "长整数" << Abs(3680L) << endl;
cout<< "-------------------------------" << endl << endl;
cout<< "第6题:演示复数" << endl;
CComplex c(-1, 9);
c.Show();
c.SetComplex(15, -36);
c.Show();
cout<< "-------------------------------" << endl << endl;
cout<< "第7题:填空题" << endl;
cout<< "1. 输出结果是: 1251121" << endl;
cout<< "2. 私有的" << endl;
cout<< "3. 虚基类" << endl;
cout<< "4. 模版函数如下:" << endl;
cout<< "template
cout<< "T DBL(T v)"<
cout<< "-------------------------------" << endl << endl;
cout<< "第8题:演示图形周长" << endl;
CSquare s;
s.SetSide(1,2,3,4);
cout<< "四边形周长:" << s.GetLength() <
cir.SetRadius(1);
cout<< "圆形的周长:" << cir.GetLength() <
cout<< "第9题:演示日期" << endl;
CDateInfo date;
date.ShowDateInfo();
date.SetDateInfo(2012,12,12);
date.ShowDateInfo();
cout<< "-------------------------------" << endl << endl;
return 0;
}
/////////////////////////////////////////////////////////////
兄弟,第10题你自己做好么?参考第六题演示复数的类 自己写个吧。我的手都酸痛了……
我明白了,什么叫坚持到底,手都酸痛了……
兄弟,给哥点吃的吧?