实验五-继承与派生
第一题
(1)已知有point类如下:
class point
{
protected :
int x,y;
public:
point(int a=0;int b=0){x=a;y=b;}
void setpoint(int a,int b){x=a;y=b;}
int getx()const{return x;}
int gety()const{return y;}
};
先由point类派生出circle(圆)和square(正方形)类,再由circle类派生出cylinder(圆柱)类,由square类派生出cube(立方体)类,并编写主函数实现一个圆、一个圆柱体、一个正方形和一个立方体的定义,输出圆的周长、正方形的面积、圆柱体的表面积和正方体的体积。在主函数中完成测试。
/*******************************************/
//作者*************************林波
//完成时间**********************2022/5/15
/*******************************************/
#include <iostream>
#define pi 3.141592
using namespace std;
class point
{
protected:
int x, y;
public:
point(int a = 0, int b = 0) { x = a; y = b; }
void setpoint(int a, int b) { x = a; y = b; }
int getx()const { return x; }
int gety()const { return y; }
};
class circle :public point //派生圆类
{
public:
int r;//圆半径
circle(int a, int b, int c) :r(c), point(a, b) {}//圆类构造函数
float circle_C()
{
return 2 * pi * r;//圆周长
}
void ciccle_display()
{
cout << "圆周长C=" << " " << circle_C() << endl;
}
};
class cyclinder :public circle//派生圆柱类
{
public:
int h;//圆柱高度
cyclinder(int a, int b, int c, int d) :h(d), circle(a, b, c) {}//圆柱类构造函数
float cyclinder_S()
{
return 2 * pi * r * r + 2 * pi * r * h;//圆柱表面积
}
void cyclinder_display()
{
cout << "圆柱表面积S=" << " " << cyclinder_S() << endl;
}
};
class square :public point//派生正方形类
{
public:
int bc;//正方形边长
square(int a, int b, int c) :bc(c), point(a, b) {}//正方形类构造函数
float square_S()
{
return bc * bc;//正方形面积
}
void square_display()
{
cout << "正方形面积S=" << " " << square_S() << endl;
}
};
class cube :public square//派生立方体类
{
public:
cube(int a, int b, int c) : square(a, b, c) {}//立方体类构造函数
float cube_V()
{
return bc * bc * bc;
}
void cube_display()
{
cout << "立方体体积V=" << " " << cube_V() << endl;
}
};
int main()
{
circle a(0, 0, 5);
cyclinder b(0, 0, 5, 8);
square c(0, 0, 5);
cube d(0, 0, 5);
a.ciccle_display();
b.cyclinder_display();
c.square_display();
d.cube_display();
}
第二题
分别声明Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。要求:
①在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。
②在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务)。在Teacher_Cadre类中还包含数据成员wages(工资)。
③对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
④在类中声明成员函数,在类外定义成员函数。
⑤在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话,然后再用count语句输出职务与工资。
⑥在主函数中完成测试。
/*******************************************/
//作者*************************林波
//完成时间**********************2022/5/15
/*******************************************/
#include <iostream>
using namespace std;
int i = 0;
class Teacher
{
public:
char name[10];//姓名
int old;//年龄
char sex[10];//性别
char address[100];//地址
char Tel[20];//电话
char title[10];//职称
Teacher(char a[10], int b, char c[10], char d[100], char e[20], char f[10]);
void dispaly_Teacher(void);
};
class Cadre
{
public:
char name[10];//姓名
int old;//年龄
char sex[10];//性别
char address[100];//地址
char Tel[20];//电话
char post[10];//职务
Cadre(char a[10], int b, char c[10], char d[100], char e[20], char f[10]);
};
Cadre::Cadre(char a[10], int b, char c[10], char d[100], char e[20], char f[10])
{
for (i = 0; i <= 9; i++)
{
name[i] = a[i];
}
old = b;
for (i = 0; i <= 9; i++)
{
sex[i] = c[i];
}
for (i = 0; i <= 99; i++)
{
address[i] = d[i];
}
for (i = 0; i <= 19; i++)
{
Tel[i] = e[i];
}
for (i = 0; i <= 9; i++)
{
post[i] = f[i];
}
}
Teacher::Teacher(char a[10], int b, char c[10], char d[100], char e[20], char f[10])
{
for (i = 0; i <= 9; i++)
{
name[i] = a[i];
}
old = b;
for (i = 0; i <= 9; i++)
{
sex[i] = c[i];
}
for (i = 0; i <= 99; i++)
{
address[i] = d[i];
}
for (i = 0; i <= 19; i++)
{
Tel[i] = e[i];
}
for (i = 0; i <= 9; i++)
{
title[i] = f[i];
}
}
void Teacher::dispaly_Teacher()
{
cout << "姓名:" << " " << name << endl;
cout << "年龄:" << " " << old << endl;
cout << "性别:" << " " << sex << endl;
cout << "地址:" << " " << address << endl;
cout << "电话:" << " " << Tel << endl;
cout << "职称:" << " " << title << endl<<endl;
}
class Teacher_Cadre :public Teacher , public Cadre
{
public:
int wage;//工资
Teacher_Cadre(char a[10], int b, char c[10], char d[100], char e[20],char f[10],char h[10],int i) :wage(i), Teacher(a, b, c, d, e, f), Cadre(a, b, c, d, e, h){}
void show(void);
};
void Teacher_Cadre::show()
{
Teacher::dispaly_Teacher();
cout << "职务" << " " << post << endl << "工资:" << " " << wage<<"元" << endl;
}
int main()
{
char a[10] = "林波";
int b = 19;
char c[10] = "男";
char d[100] = "花果山水帘洞";
char e[20] = "18894567845";
char f[10] = "副教授";
char o[8] = " 主任";
int i = 8485;
Teacher_Cadre W1(a,b,c,d,e,f,o,i);
W1.show();
}
第三题
定义日期类Date和时间类Time,在两个类中添加成员函数可以完成日期和时间的增加和减少功能。从Date和Time派生一个DateAndTime类 ,添加相应的成员函数,当时间递增到新的一天时能够修改日期值。
/*******************************************/
//作者*************************林波
//完成时间**********************2022/5/15
//程序说明**********************可实现年月日时分的走时(调用了标准库中的延时函数)
/*******************************************/
#include <iostream>
#include <ctime>
using namespace std;
int i = 0;
class Data
{
public:
int year,month,data;
Data()
{
year = 2022;
month = 1;
data = 1;
}
void Set_Data()
{
cout << "请输入日期..........:" << endl;
cin >> year >> month >> data;
}
void Show_data()
{
cout << "日期:" << " " << year << "/" << month << "/" << data << endl;
}
void Data_Add(int a)
{
data += a;
}
void Data_Reduce(int a)
{
data -= a;
}
};
class Time
{
public:
int hour, minte;
Time()
{
hour = 12;
minte = 30;
}
void Set_Time()
{
cout << "请输入时间..........:" << endl;
cin >> hour >> minte;
}
void Show_Time()
{
cout << "时间:" << " " << hour << ":" << minte << endl;
}
void Time_Add(int a)
{
minte += a;
}
void Time_Reduce(int a)
{
minte -= a;
}
};
class DateAndTime:public Data,public Time
{
public:
DateAndTime()
{}
void SHOW_DT()
{
Data::Show_data();
Time::Show_Time();
}
void DT_Add(int a,int b )
{
Data::Data_Add(a);
cout << "日期加" << " " << a << "号" << endl;
Time::Time_Add(b);
cout << "时间加" << " " << b << "分钟" << endl;
}
void DT_Reduce(int a, int b)
{
Data::Data_Reduce(a);
Time::Time_Reduce(b);
}
void DataAndTime_updata()
{
if (minte == 60)
{
minte = 1;
hour++;
}
if (minte == 0)
{
minte = 59;
hour--;
}
if (hour == 24)
{
hour = 0;
data++;
}
if (hour == 0)
{
hour == 23;
data--;
}
if (data==31)
{
data = 1;
month++;
}
if (data ==0)
{
data = 30;
month--;
}
if (month==13)
{
month = 1;
year++;
}
if (month==00)
{
month=12;
year--;
}
}
};
void Delay(int time)//time*1000为秒数
{
clock_t now = clock();
while (clock() - now < time);
}
int main()
{
DateAndTime DT;
DT.SHOW_DT();
while(1)
{
Delay(1 * 1000); //延时1秒
DT.DT_Add(1, 1);
DT.DataAndTime_updata();
DT.SHOW_DT();
cout << endl;
}
}
第四题
编写一个程序,计算学生的平均成绩和职工的平均工资。
编程思路:学生与职工有一些共同的属性:姓名,性别,出生日期,另外还有一些自己的特性,学生:学号、成绩;职工:职工号、工资。因此,为了代码的重用,设计一个人类,包含学生与职工的共同属性,其中,出生日期是日期类(包含3个成员:年、月、日)的对象。学生类和职工类继承人类的特性,再添加自己的成员。为了计算学生的平均成绩和职工的平均工资,在类中使用静态成员。
/*******************************************/
//作者*************************林波
//完成时间**********************2022/5/15
/*******************************************/
#include <iostream>
#include<cstring>
using namespace std;
int i = 0;//用于循环
#define n 3//学生和员工各五名
class human
{
protected:
char name[100];//姓名
char sex[100];//性别
char birth[100];//出生日期
public:
human()
{
char a[100] = "0";
strcpy_s(name, a);
strcpy_s(sex, a);
strcpy_s(birth, a);
}
human(char a[100], char b[100], char c[100])
{
for (i = 0; i <= 9; i++)
{
name[i] = a[i];
}
for (i = 0; i <= 9; i++)
{
sex[i] = b[i];
}
for (i = 0; i <= 19; i++)
{
birth[i] = c[i];
}
}
};
class student :public human
{
protected:
char s_NO[100];//学号
int Grade;//成绩
static float Grade_Aver;//平均成绩
static float Grade_Sum;//总成绩
public:
student()
{
char a[100] = "0";
strcpy_s(s_NO, a);
Grade = 0;
}
void Set_Student()
{
char a[100], b[100], c[100], d[100];
int e;
cout << "请输入姓名" << " ";
cin >> a;
cout << endl;
cout << "请输入性别" << " ";
cin >> b;
cout << endl;
cout << "请输入出生日期" << " ";
cin >> c;
cout << endl;
cout << "请输入学号" << " ";
cin >> d;
cout << endl;
cout << "请输入成绩" << " ";
cin >> e;
cout << endl;
strcpy_s(name, a);
strcpy_s(sex, b);
strcpy_s(birth, c);
strcpy_s(s_NO, d);
Grade = e;
}
static float Get_Grade_Aver()//获取平均成绩
{
Grade_Aver = Grade_Sum / n;
return Grade_Sum / n;
}
static void Get_Grade_Sum(student a)//获取总成绩
{
Grade_Sum += a.Grade;
}
static void display(student a)
{
cout << a.name << " " << a.sex << " " << a.birth << " " << a.s_NO << " " << a.Grade <<"分" << endl;
}
static void display()
{
cout << "总成绩为" << " " << Grade_Sum<<"分"<< endl;
cout << "平均成绩为" << " " << Grade_Aver <<"分" << endl;
}
};
class worker :public human
{
protected:
char w_NO[100];//职工号
int Wage;//工资
static float Wage_Aver;//平均工资
static float Wage_Sum;//总成绩
public:
worker()
{
char a[100] = "0";
strcpy_s(w_NO, a);
Wage = 0;
}
void Set_Worker()
{
char a[100], b[100], c[100], d[100];
int e;
cout << "请输入姓名" << " ";
cin >> a;
cout << endl;
cout << "请输入性别" << " ";
cin >> b;
cout << endl;
cout << "请输入出生日期" << " ";
cin >> c;
cout << endl;
cout << "请输入职工号" << " ";
cin >> d;
cout << endl;
cout << "请输入工资" << " ";
cin >> e;
cout << endl;
strcpy_s(name, a);
strcpy_s(sex, b);
strcpy_s(birth, c);
strcpy_s(w_NO, d);
Wage = e;
}
static float Get_Wage_Aver()//获取平均工资
{
Wage_Aver = Wage_Sum / n;
return Wage_Sum / n;
}
static void Get_Grade_Sum(worker a)//获取总工资
{
Wage_Sum += a.Wage;
}
static void display(worker a)
{
cout << a.name << " " << a.sex << " " << a.birth << " " << a.w_NO << " " << a.Wage <<"元" << endl;
}
static void display()
{
cout << "总工资为" << " " << Wage_Sum <<"元" << endl;
cout << "平均工资为" << " " << Wage_Aver <<"元" << endl;
}
};
float student::Grade_Aver = 0;
float student::Grade_Sum = 0;
float worker::Wage_Sum = 0;
float worker::Wage_Aver = 0;
void main()
{
student a[3];
worker b[3];
cout << "请输入学生和员工信息..........." << endl;
for (i = 0; i <= 2; i++)
{
a[i].Set_Student();
b[i].Set_Worker();
}
cout << "学生信息-姓名-性别-出生日期-学号-成绩:" << endl;
for (i = 0; i <= 2; i++)
{
a[i].display(a[i]);
}
cout << "职工信息-姓名-性别-出生日期-职工号-工资:" << endl;
for (i = 0; i <= 2; i++)
{
b[i].display(b[i]);
}
for (i = 0; i <= 2; i++)
{
student::Get_Grade_Sum(a[i]);
worker::Get_Grade_Sum(b[i]);
}
student::Get_Grade_Aver();
worker::Get_Wage_Aver();
student::display();
worker::display();
}