1、建筑物类Building的派生类
#include
class Building
{
private:
int N;//编号
int floor;//层数
int room;//房间数
double area;//总面积
public:
Building(int n=1,int f=5,int r=20,double a=2000)
{
N=n,floor=f,room=r,area=a;
}
};
class TeachBuilding:public Building
{
TeachBuilding(int n,int f,int r,double a):Building(n,f,r,a){}
};
class DormBuilding:public Building
{
int stuNum;//容纳学生总人数
//默认实参要在形参列表的结尾
DormBuilding(int n,int f,int r,double a,int stuNum=1000):Building(n,f,r,a)
{
this->stuNum=stuNum;
}
};
2、共同派生类
#include
class Table
{
private:
double height;//高度
public:
Table(double h):height(h){}
};
class Circle
{
private:
double area;//面积
public:
Circle(double a):area(a){}
};
//每一个父类前都要有继承方式
class RoundTable:public Table,public Circle
{
public:
RoundTable(double h,double a):Table(h),Circle(a){}
};
3、人员类Person的派生(虚基类的使用)
#include
#include
using namespace std;
#include
class Date
{
private:
int year,month,day;//年月日
public:
Date(int y=1900,int m=1,int d=1)
{
year=y,month=m,day=d;
}
void show()
{
cout<
private:
string name;//姓名
string sex;//性别
char id[25];//身份证号
Date birthday;//出生年月
public:
Person(){}
Person(string n,string s,const char i[],int y,int m,int d):birthday(y,m,d)//birthday要拆开来赋值
{
name=n,sex=s;
strcpy(id,i);
}
void show()
{
cout<<"name:"<
private:
char stuId[25];//学号
double score;//成绩
public:
//要传入const类型字符串,表示只读
Student(string name,string sex,const char id[],int y,int m,int d,const char stuid[],double sc):
Person(name,sex,id,y,m,d)
{
strcpy(stuId,stuid);
score=sc;
}
void show()
{
Person::show();
cout<<"stuId:"<
private:
string job;//教师职称
public:
Teacher(string name,string sex,const char id[],int y,int m,int d,string job):
Person(name,sex,id,y,m,d)
{
this->job=job;
}
void show()
{
cout<<"job:"<
public:
//这个构造函数,要使用全部基类进行赋值,包括间接基类Person
StuTech(string name,string sex,const char id[],int y,int m,int d,const char stuId[],double score,string job):
Person(name,sex,id,y,m,d),Student(name,sex,id,y,m,d,stuId,score),Teacher(name,sex,id,y,m,d,job){}
void show()
{
Student::show();
Teacher::show();
}
};
int main()
{
StuTech st("zhangsan","man","123456789",2002,6,1,"001",95.5,"jiangshi");
st.show();
return 0;
}
4、带AM,PM的时钟类Clock
#include
#include//可以获取当前时间
#include
using namespace std;
class Clock
{
private:
int hour,minute,second;
public:
//Clock();
Clock(int h=0,int m=0,int s=0)
{
hour=(h>=0&&h<24) ? h:0;
minute=(m>=0&&m<60) ? m:0;
second=(s>=0&&s<60) ? s:0;
}
int getHour()
{
return hour;
}
//24小时制
void showTime24()
{
cout<
if(hour==0)
{
cout<
cout<
cout<
second=(second+1)%60;
if(second==0)
{
minute=(minute+1)%60;
if(minute==0)
{
hour=(hour+1)%24;
}
}
}
};
int main()
{
int hour,minute,second;
double nowTime;
cout << "请设置当前时间(以24小时制按顺序输入时 分 秒):" << endl;
cin>>hour>>minute>>second;
cout<<"t";
Clock clock(hour,minute,second);
int cnt=0;
while(cnt<5)
{
nowTime=time(0);
while(time(0)<(nowTime+1));
clock.timeAdd();
cout<<"r";
cout<<"24小时制:";
clock.showTime24();
cout<
cout<<"AM ";
clock.showTimeAM();
}else{
cout<<"PM ";
clock.showTimePM();
}
cnt++;
}
return 0;
}
5、Employee类实现职员月工资管理
#include
using namespace std;
#include
const double sales = 200000;//销售额
class Employee
{
private:
int id;//编号
string name;//姓名
string job;//岗位
double monthMoney;//月工资
public:
Employee(int id, string name, string job)
{
this->id = id, this->name = name;
this->job = job;
monthMoney= getMonthMoney();
}
double getMonthMoney()
{
if (job.compare("manager") == 0)//使用string的compare函数比较字符串
{
monthMoney = 8000;
}
else if (job.compare("technician") == 0)
{
monthMoney = 100 * 8 * 30;
}
else if (job.compare("salesman") == 0)
{
monthMoney = sales * 0.04;
}
else if (job.compare("salesmanager") == 0)
{
monthMoney = 5000 + sales * 0.005;
}
return monthMoney;
}
void showInformation()
{
cout << "编号:" << id << endl;
cout << "姓名:" << name << endl;
cout << "岗位:" << job << endl;
cout << "月薪:" << monthMoney << endl;
}
};
int main()
{
Employee p1(1, "zhangsan", "manager");
Employee p2(2, "lisi", "technician");
Employee p3(3, "wangwu", "salesman");
Employee p4(4, "zhaoliu", "salesmanager");
p1.showInformation();
cout << endl;
p2.showInformation();
cout << endl;
p3.showInformation();
cout << endl;
p4.showInformation();
return 0;
}