- 1.Target
/*综合运用类、继承、多态等技术,完成一个公司人员管理类层次结构,用来描述人员信息等,
重载各种运算符,完成数据库内容的赋值、添加、工资增长等。*/
2.Code
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include<cstdlib>
#define TECH const string name, const int age, const string sex, const string dept, const double salary, const int rank
//using SA = Sale[];
//typedef Technology; *TE;
using namespace std;
class Person
{
private:
string strName;
int intAge;
string strSex;
public:
friend std::istream& operator >> (std::istream&, Person&);
friend std::ostream& operator<<(std::ostream&, const Person&);
friend bool operator==(const Person&, const Person&);
friend bool operator!=(const Person&, const Person&);
//friend Person operator+(const Person& lp, const Person& rp);
//Person& operator+=(const Person&);
Person();
Person(const string name, const int age,const string sex);
Person(const Person &p);
~Person()
{
cout << "Now destroying the instance of Person" << endl;
}
void SetName(const string name);
void SetAge(const int age);
void SetSex(const string sex);
string GetName() const;
int GetAge() const;
string GetSex() const;
void ShowMe() const;
};
std::istream&
operator >> (std::istream& in, Person& s) {
in >> s.strName>>s.intAge>> s.strSex;
//check that he inputs succeeded
if (!in)
s = Person(); //input failed:reset object to default state
return in;
}
std::ostream&
operator<<(std::ostream& out, const Person& s) {
out << s.GetName() << " " << s.GetAge() << " "
<< s.GetSex() << " " << endl;
return out;
}
inline bool
operator==(const Person& lp, const Person& rp) {
//must be made a friend of Person
return lp.GetAge() == rp.GetAge()&&
lp.GetName() == rp.GetName()&&
lp.GetSex() == rp.GetSex();
}
inline bool
operator!=(const Person& lp, const Person& rp) {
return !(lp == rp); //!= defined in terms of operator
}
/*Person operator+(const Person& lp, const Person& rp) {
Person res(lp);// copy (|lp|) into a local object that we‘ll return
res += rp; // add in the contents of (|rp|)
return res; // return (|res|) by value
}*/
Person::Person()
{
strName = "XXX";
intAge = 0;
strSex = "female";
}
Person::Person(const string name,const int age,const string sex) : strName(name),intAge(age), strSex(sex){
}
Person::Person(const Person &p) : strName(p.strName), intAge(p.intAge), strSex(p.strSex){
}
void Person::SetName(const string name) {
strName = name;
}
void Person::SetAge(const int age){
intAge = age;
}
void Person::SetSex(const string sex){
strSex = sex;
}
string Person::GetName() const {
return strName;
}
int Person::GetAge() const{
return intAge;
}
string Person::GetSex() const{
return strSex;
}
void Person::ShowMe() const{
cout << "Name" << ‘\t‘ << "Age" << ‘\t‘ << endl;
cout << GetName() << ‘\t‘ << GetAge() << ‘\t‘ << GetSex() << ‘\t‘ << endl;
}
class Employee : virtual public Person
{
protected:
string strDept;
double douSalary;
public:
friend std::istream& operator >> (std::istream&, Employee&);
friend std::ostream& operator<<(std::ostream&, const Employee&);
friend bool operator==(const Employee&, const Employee&);
friend bool operator!=(const Employee&, const Employee&);
Employee& operator+=(const Employee&);
Employee& operator=(const Person&);
Employee& operator=(const Employee&);
friend Employee operator+(const Employee&, const Employee&);
Employee();
Employee(const string name, const int age,const string sex, const string dept, const double salary);
Employee(const Employee &e);
~Employee()
{
cout << "Now destroying the instance of Employee" << endl;
}
void SetDept(const string dept);
void SetSalary(double salary);
string GetDept() const;
double GetSalary() const;
void ShowMe() const;
};
Employee& Employee:: operator=(const Person&rp) {
SetAge(rp.GetAge());
SetName(rp.GetName());
SetSex(rp.GetSex());
return *this;
}
Employee& Employee:: operator=(const Employee&rp) {
SetAge(rp.GetAge());
SetName(rp.GetName());
SetSex(rp.GetSex());
SetDept(rp.GetDept());
SetSalary(rp.GetSalary());
return *this;
}
Employee& Employee:: operator+=(const Employee&rp) {
douSalary += rp.GetSalary();
return *this;
}
Employee operator+(const Employee&lp, const Employee&rp) {
Employee res(lp);
res += rp;
return res;
}
std::istream&
operator >> (std::istream& in, Employee& s) {
in >> s.douSalary >> s.strDept;
//check that he inputs succeeded
if (!in)
s = Employee(); //input failed:reset object to default state
return in;
}
std::ostream&
operator<<(std::ostream& out, const Employee& s) {
out << s.GetDept() << " " << s.GetSalary() << " "<<endl;
return out;
}
inline bool
operator==(const Employee& lp, const Employee& rp) {
//must be made a friend of Employee
return lp.GetDept() == rp.GetDept() &&
lp.GetName() == rp.GetName() &&
lp.GetSex() == rp.GetSex() &&
lp.GetAge() == rp.GetAge() &&
lp.GetSalary() == rp.GetSalary();
}
inline bool
operator!=(const Employee& lp, const Employee& rp) {
return !(lp == rp); //!= defined in terms of operator
}
Employee::Employee() : douSalary(0.0), strDept("xxxx")
{
}
Employee::Employee(const string name, const int age, const string sex, const string dept, const double salary)
: Person(name, age, sex), douSalary(salary),strDept(dept)
{
}
Employee::Employee(const Employee &e) : Person(e.GetName(), e.GetAge(), e.GetSex()),
douSalary(e.douSalary)
{
strDept = e.strDept;
}
void Employee::SetDept(const string dept)
{
strDept = dept;
}
void Employee::SetSalary(double salary)
{
douSalary = salary;
}
string Employee::GetDept() const
{
return strDept;
}
double Employee::GetSalary() const
{
return douSalary;
}
void Employee::ShowMe() const
{
Person::ShowMe();
cout << "Dept" << ‘\t‘ << "Salary" << endl;
cout << strDept << ‘\t‘ << douSalary << endl;
}
class Technology :virtual public Employee{
protected:
int IntRank;
public:
friend std::istream& operator >> (std::istream&, Technology&);
friend std::ostream& operator<<(std::ostream&, const Technology&);
friend bool operator==(const Technology&, const Technology&);
friend bool operator!=(const Technology&, const Technology&);
Technology();
Technology(const string name, const int age, const string sex, const string dept, const double salary, const int rank);
Technology(const Technology &e);
~Technology(){
cout << "Now destroying the instance of Technology" << endl;
}
void SetRank(const int rank);
int GetRank()const;
void ShowMe() const;
};
std::istream&
operator >> (std::istream& in, Technology& s) {
in >> s.IntRank;
//check that he inputs succeeded
if (!in)
s = Technology(); //input failed:reset object to default state
return in;
}
std::ostream&
operator<<(std::ostream& out, const Technology& s) {
out << s.GetRank() << " " << endl;
return out;
}
inline bool
operator==(const Technology& lp, const Technology& rp) {
//must be made a friend of Technology
return lp.GetDept() == rp.GetDept() &&
lp.GetName() == rp.GetName() &&
lp.GetSex() == rp.GetSex() &&
lp.GetAge() == rp.GetAge() &&
lp.GetSalary() == rp.GetSalary() &&
lp.GetRank() == rp.GetRank();
}
inline bool
operator!=(const Technology& lp, const Technology& rp) {
return !(lp == rp); //!= defined in terms of operator
}
Technology::Technology():IntRank(0) {
}
Technology::Technology(const string name, const int age, const string sex, const string dept, const double salary, const int rank)
: Person(name, age, sex), Employee(name, age, sex, dept, salary),IntRank(rank)
{
}
Technology::Technology(const Technology &e) : Person(e.GetName(), e.GetAge(), e.GetSex()), Employee(e.GetName(), e.GetAge(),e.GetSex(), e.GetDept(), e.GetSalary()),IntRank(e.IntRank) {
}
void Technology::SetRank(const int rank) {
IntRank = rank;
}
int Technology::GetRank()const {
return IntRank;
}
void Technology::ShowMe()const {
Employee::ShowMe();
cout << "Rank" << ‘\t‘ << endl;
cout << IntRank << endl;
}
class Sale :virtual public Employee{
protected:
int IntOver;
public:
Sale();
Sale(const string name, const int age, const string sex, const string dept, const double salary,const int over);
Sale(const Sale &e);
~Sale() {
cout << "Now destroying the instance of Sale" << endl;
}
void SetOver(const int overturn);
int GetOver()const;
void ShowMe() const;
};
Sale::Sale():IntOver(0) {
}
Sale::Sale(const string name, const int age, const string sex, const string dept, const double salary, const int over)
: Person(name, age, sex), Employee(name, age, sex, dept, salary), IntOver(over){
}
Sale::Sale(const Sale &e) : Person(e.GetName(), e.GetAge(), e.GetSex()), Employee(e.GetName(), e.GetAge(), e.GetSex(), e.GetDept(), e.GetSalary()) ,IntOver(e.IntOver){
}
void Sale :: SetOver(const int overturn) {
IntOver = overturn;
}
int Sale::GetOver()const {
return IntOver;
}
void Sale::ShowMe()const {
Employee::ShowMe();
cout << "Over" << ‘\t‘ << endl;
cout << IntOver << endl;
}
class Manager :virtual public Employee {
protected:
int SumEmployee;
//SA SaleEmployeen;
//TE ITEmployee;
public:
Manager();
Manager(const string name, const int age, const string sex, const string dept, const double salary, const int sum);//,const SA SaleMan,const TE ITMan);
Manager(const Manager &e);
~Manager(){
cout << "Now destroying the instance of Manager" << endl;
}
void SetSum(const int sum);
int GetSum()const;
void ShowMe()const;
};
Manager::Manager() :SumEmployee(0)/* SaleEmployeen(), ITEmployee()*/{
}
Manager::Manager(const string name, const int age, const string sex, const string dept, const double salary, const int sum)//, const SA SaleMan, const TE ITMan)
: Person(name, age, sex), Employee(name, age, sex, dept, salary),SumEmployee(sum)
{
/*Sale *p=new Sale[sum];
p = SaleMan;
while (p++ != NULL) {
*SaleEmployeen++ = *p;
}
Technology *q = new Technology[sum];
q = ITMan;
while (q++ != NULL) {
*ITEmployee++ = *q++;
}*/
}
Manager::Manager(const Manager &e):Person(e.GetName(), e.GetAge(), e.GetSex()), Employee(e.GetName(), e.GetAge(), e.GetSex(), e.GetDept(), e.GetSalary()) {
/*Sale *p = new Sale;
p = e.SaleEmployeen;
while (p++ != NULL) {
*SaleEmployeen++ = *p;
}
Technology *q = new Technology;
q = e.ITEmployee;
while (q++ != NULL) {
*ITEmployee++ = *q;
}
*/
}
void Manager::SetSum(const int sum) {
SumEmployee = sum;
}
int Manager::GetSum()const {
return SumEmployee;
}
void Manager::ShowMe()const {
Person::ShowMe();
cout << "The Sum of Employee" << ‘\t‘ << endl;
cout << SumEmployee << endl;
}
class SaleManager :virtual public Sale, virtual public Manager {
protected:
string strRight;
public:
SaleManager();
SaleManager(const string name, const int age, const string sex, const string dept, const double salary, const int sum, /*const SA SaleMan, const TE ITMan,*/ const int over, const int right);
~SaleManager() {
cout << "Now destroying the instance of SaleManager" << endl;
}
void SetRight(const string right);
string GetRight()const;
void ShowMe()const;
};
SaleManager::SaleManager() :strRight("0"){
}
SaleManager::SaleManager(const string name, const int age, const string sex, const string dept, const double salary, const int sum,/* const SA SaleMan, const TE ITMan,*/ const int over, const int right)
: Person(name, age, sex), Employee(name, age, sex, dept, salary), Sale(name, age, sex, dept, salary, over),Manager( name, age,sex, dept,salary, sum/*, SaleMan, ITMan*/)
{
strRight = right;
}
void SaleManager::SetRight(const string right) {
strRight = right;
}
string SaleManager::GetRight()const {
return strRight;
}
void SaleManager::ShowMe()const {
Employee::ShowMe();
cout << "The Manager:" << endl;
cout << "Right:" << endl;
}
int main()
{
Person p1[10];
cout << "Please enter ten persons in details"
<< endl << "Input format is " << "(Name Age Sex) " << endl
<< "Warnning:Separate each data with a space!!!" << endl
<< "For example:LZH 19 male" << endl;
for (int i = 0; i < 3; i++)
cin >> p1[i];
for (int i = 0; i < 3; i++)
cout << p1[i];
cout << endl;
Employee emp1[10];
cout << "Now enter the salary and department for each employee!" << endl
<< "Like:123456 Sale" << endl;
for (int i = 0; i < 3; i++)
{
emp1[i] = p1[i];
cin >> emp1[i];
}
for (int i = 0; i < 3; i++)
cout << emp1[i] << endl;
for (int i = 0; i < 3; i++)
emp1[i].ShowMe();
return 0;
}
- 3.测试截图
- 4.先Mark一下,有时间再改
原文地址:https://www.cnblogs.com/FlyerBird/p/9038616.html
时间: 2024-10-07 18:28:41