应用c++类,派生类,多态性,文件,数据的输入与输出,计算统计等设计一个综合性系统
就帮你实现一个统计学生信息的吧。
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string.h>
using namespace std;
class Student //基类
{
public:
virtual int getTotalScore()=0; //获取总分
int m_math=0; //数学分数
int m_english=0; //英语分数
};
class Girl:public Student //女生信息
{
public:
Girl(int math,int english)
{
m_math=math;
m_english=english;
}
Girl(){}
void SetMath(int math)
{
m_math=math;
}
void SetEnglish(int english)
{
m_english=english;
}
int getTotalScore()
{
return m_math+m_english;
}
void disPlay()
{
cout<<"数学成绩为 "<<m_math<<" 英语成绩为 "<<m_english<<"总分为 "<<getTotalScore()<<endl;
}
};
class Boy:public Student
{
public:
Boy(int math,int english)
{
m_math=math;
m_english=english;
}
Boy(){}
void SetMath(int math)
{
m_math=math;
}
void SetEnglish(int english)
{
m_english=english;
}
int getTotalScore()
{
return m_math+m_english;
}
void disPlay()
{
cout<<"数学成绩为 "<<m_math<<" 英语成绩为 "<<m_english<<"总分为 "<<getTotalScore()<<endl;
}
};
int main() {
Boy boy;
Girl girl;
int math,english;
cout<<"输入男生的数学成绩和英语成绩,用空格分开"<<endl;
cin>>math>>english;
boy.SetMath(math);
boy.SetEnglish(english);
boy.disPlay();
cout<<"输入女生的数学成绩和英语成绩,用空格分开"<<endl;
cin>>math>>english;
girl.SetMath(math);
girl.SetEnglish(english);
girl.disPlay();
return 0;
}
运行结果及代码如下:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#define MAXNMB 100
//定义员工类型,也可以用枚举
#define EMP_TYPE_MANAGER (int)1
#define EMP_TYPE_COMMON (int)2
#define EMP_TYPE_WOKER (int)3
class Employee
{
protected:
int id; //工号
string name; //名字
int solary; //工资
int type; //员工类型
public:
Employee() {}
void setId(int d) { id = d; }
int getId() { return id; }
void setName(string n) { name = n; }
string getName() { return name; }
void setSolary(int s) { solary = s; }
int getSolary() { return solary; }
//void setType(int t) { type = t; }
int getType() { return type; }
virtual void show() { cout << "Employee" << endl; }
};
//管理类
class Manager :public Employee
{
private:
string zhiwu; //职务
public:
Manager() { type = EMP_TYPE_MANAGER; }
void setZhiwu(string z) { zhiwu = z; }
string getZhiwu() { return zhiwu; }
void show()
{
cout << "工号:" << id << ",姓名:" << name <<",类别:管理" << ",职务:" << zhiwu << ",待遇:" << solary << endl;
}
};
//普通职工
class Common :public Employee
{
public:
Common() { type = EMP_TYPE_COMMON; }
void show()
{
cout << "工号:" << id << ",姓名:" << name << ",类别:普工" << ",待遇:" << solary << endl;
}
};
//小时工
class Worker :public Employee
{
private:
int price; //每小时工资
int hour; //工作小时数
public:
Worker() { price = 0; hour = 0; type = EMP_TYPE_WOKER; }
void setPrice(int p) { price = p; }
int getPrice() { return price; }
void setHour(int h) { hour = h; solary = price * hour; }
int getHour() { return hour; solary = price * hour; }
void show()
{
cout << "工号:" << id << ",姓名:" << name << ",时薪:" << price <<",工作时长:"<< hour << ",待遇:" << solary << endl;
}
};
//统计
void Tongji(Employee* a[], int n)
{
int opt, i;
int sum[3] = { 0,0,0 };
for (i = 0; i < n; i++)
{
if (a[i]->getType() == EMP_TYPE_MANAGER)
sum[0] += a[i]->getSolary();
else if (a[i]->getType() == EMP_TYPE_COMMON)
sum[1] += a[i]->getSolary();
else
sum[2] += a[i]->getSolary();
}
cout << "管理人员薪资:" << sum[0] << endl;
cout << "普通员工薪资:"<< sum[1] << endl;
cout << "小时工薪资:" << sum[2] << endl;
}
//录入数据
void Input(Employee* a[], int& nmb)
{
system("cls");
int type;
cout << "请输入人员类别:"<<endl;
cout << " 1.管理" << endl;
cout << " 2.普工" << endl;
cout << " 3.小时工" << endl;
while (1)
{
cin >> type;
if (type >= 1 && type <= 3)
break;
else
cout << "输入错误,请重新输入:";
}
int id, sl, p, h;
string name,zhiwu;
cout << "请输入员工工号:";
cin >> id;
cout << "请输入姓名:";
cin >> name;
if (type == EMP_TYPE_MANAGER)
{
cout << "请输入职务:";
cin >> zhiwu;
cout << "请输入待遇:";
cin >> sl;
Manager* ss = new Manager();
ss->setId(id);
ss->setName(name);
ss->setSolary(sl);
ss->setZhiwu(zhiwu);
a[nmb] = ss;
nmb++;
}
else if (type == EMP_TYPE_COMMON)
{
cout << "请输入待遇:";
cin >> sl;
Common* c = new Common();
c->setId(id);
c->setName(name);
c->setSolary(sl);
a[nmb] = c;
nmb++;
}
else
{
cout << "请输入时薪和工作时长:";
cin >> p >> h;
Worker *w = new Worker();
w->setId(id);
w->setName(name);
w->setPrice(p);
w->setHour(h);
a[nmb] = w;
nmb++;
}
}
//显示所有信息
void ShowAll(Employee* a[],int nmb)
{
system("cls");
cout << "所有员工信息:" << endl;
for (int i = 0; i < nmb; i++)
{
if (a[i]->getId() == EMP_TYPE_MANAGER)
{
Manager* m = (Manager*)a[i];
m->show();
}
else if (a[i]->getId() == EMP_TYPE_COMMON)
{
Common* c = (Common*)a[i];
c->show();
}
else
{
Worker* w = (Worker*)a[i];
w->show();
}
}
}
//保存到文件
void Save(Employee* a[], int nmb)
{
ofstream os;
os.open("myinfo.txt", ios::out);
for (int i = 0; i < nmb; i++)
{
if (a[i]->getType() == EMP_TYPE_MANAGER)
{
Manager* m = (Manager*)a[i];
os << m->getId() << " " << m->getName() << " 管理 " << m->getZhiwu() << " " << m->getSolary() << endl;
}
else if (a[i]->getType() == EMP_TYPE_COMMON)
{
Common* c = (Common*)a[i];
os <<c->getId() << " " << c->getName() << " 普工 " << c->getSolary() << endl;
}
else
{
Worker* w = (Worker*)a[i];
os << w->getId() << " " << w->getName() << " 小时工 " << w->getPrice() << " " << w->getHour() << endl;
}
}
os.close();
cout << "文件保存成功" << endl;
}
int main()
{
Employee* a[MAXNMB];
int nmb = 0;
int opt;
int bgo = 1;
while (bgo)
{
system("cls");
cout << "-----员工薪资管理系统-----" << endl;
cout << "1.录入员工信息 " << endl;
cout << "2.显示所有员工信息" << endl;
cout << "3.统计员工信息" << endl;
cout << "4.保存员工信息" << endl;
cout << "0.退出系统 " << endl;
cin >> opt;
switch (opt)
{
case 1:
Input(a, nmb);
break;
case 2:
ShowAll(a, nmb);
break;
case 3:
Tongji(a, nmb);
break;
case 4:
Save(a, nmb);
break;
case 0:
bgo = 0;
break;
}
system("pause");
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<string>
#include<vector> // vector 需要的包含的头文件
#include<algorithm>
class CGirl{ // 定义一个超女类
public:
char m_name[50]; // 姓名
int m_age; // 年龄
int m_height; // 体重
char m_sc[30]; // 身材
char m_yz[30]; // 颜值
// virtual int Show()=0; // 这是定义了6.2中的纯虚函数.
// virtual int Show() // 添加了virtual关键字, 注意执行结果的区别
int Show(); //显示超女基本信息的成员函数
};
class CKCon:public CGirl{ // 定义王妃类, 从超女继承
public:
char m_ch[50]; // 称号
char m_palace[50]; //居住的宫殿
int m_sal; //俸禄
int Show();
};
int main() {
CKCon KCon; // 实例化一个KCon对象
strcpy(KCon.m_name, "张大");
KCon.m_age=19;
KCon.m_height=49;
strcpy(KCon.m_sc, "苗条");
strcpy(KCon.m_yz, "漂亮");
strcpy(KCon.m_ch, "王妃");
strcpy(KCon.m_yz, "什么宫");
KCon.m_height=449;
// KCon.Show(); // 执行派生类的成员函数
CGirl *p1; // 基类的指针
CKCon *p2; // 派生类的指针
p1=p2=&KCon; // 都指向派生类
p1->Show(); // 将调用基类的Show成员函数
p2->Show(); // 将调用派生类的Show成员函数
return 0;
}
int CGirl::Show(){
printf("name=%s, age=%d, height=%d, sc=%s, yz=%s\n", m_name, m_age, m_height,m_sc,m_yz);
return 0;
}
int CKCon::Show(){
printf("name=%s, age=%d, height=%d, sc=%s, yz=%s, 称号=%s, palace=%s, sal=%d\n",m_name,m_age,m_height,m_sc,m_yz, m_ch, m_palace,m_sal);
return 0;
}