class Employee
{
private:
string Name;
Date Birthday;
string Security_Number;
public:
Employee(string name, int y, int m, int d, string num)
:Name(name),Birthday(y,m,d), Security_Number(num)
{}
string getName()const
{
return Name;
}
string getSecurity_Number()const
{
return Security_Number;
}
virtual double Earning() = 0;
virtual void Print()
{
cout << typeid(*this).name() << ": "<< getName() << endl;
cout << "Birthday: ";
Birthday.Date_Print();
cout << "Security Number: " << getSecurity_Number() << endl;
}
Date getBirthday()
{
return Birthday;
}
#include<iostream>
#include<array>
#include <windows.h>
#include <typeinfo>
#include <vector>
using namespace std;
array<string, 13> Month_Name = { "","January","February","March","April","May","June","July","August","September","October","November","December" };
class Date
{
friend Date TIME();
friend class SalariedEmployee;
friend class CommisionEmployee;
private:
int year;
int month;
int day;
public:
Date(int y, int m, int d):year(y),month(m),day(d)
{}
void Date_Print()
{
cout << Month_Name[month] << " " << day << ", " << year << endl;
}
};
class Employee
{
private:
string Name;
Date Birthday;
string Security_Number;
public:
Employee(string name, int y, int m, int d, string num)
:Name(name),Birthday(y,m,d), Security_Number(num)
{}
string getName()const
{
return Name;
}
string getSecurity_Number()const
{
return Security_Number;
}
virtual double Earning() = 0;
virtual void Print()
{
cout << typeid(*this).name() << ": "<< getName() << endl;
cout << "Birthday: ";
Birthday.Date_Print();
cout << "Security Number: " << getSecurity_Number() << endl;
}
Date getBirthday()
{
return Birthday;
}
};
class SalariedEmployee:public Employee
{
private:
double Weekly_Salary;
public:
SalariedEmployee(string name, int y, int m, int d, string num, double salary)
:Employee(name, y, m, d, num), Weekly_Salary(salary){}
virtual double Earning()
{
return Weekly_Salary;
}
virtual void Print()
{
Employee::Print();
cout << "weekly salary: " << Weekly_Salary << endl;
if (getBirthday().month == TIME().month && getBirthday().day == TIME().day)
{
cout << "HAPPY BIRTHDAY!" << endl;
cout << "earing: " << Earning()+100.0 << endl;
}
else
{
cout << "earing: " << Earning() << endl;
}
cout << endl;
}
};
class CommisionEmployee :public Employee
{
private:
double Gross_Sales;
double Rate;
public:
CommisionEmployee(string name, int y, int m, int d, string num, double gross, double rate)
:Employee(name, y, m, d, num), Gross_Sales(gross),Rate(rate) {}
virtual double Earning()
{
return Gross_Sales * Rate;
}
virtual void Print()
{
Employee::Print();
cout << "gross rate: " << Gross_Sales << " rate:" << Rate << endl;
if (getBirthday().month == TIME().month && getBirthday().day == TIME().day)
{
cout << "HAPPY BIRTHDAY!" << endl;
cout << "earing: " << Earning() + 100.0 << endl;
}
else
{
cout << "earing: " << Earning() << endl;
}
cout << endl;
}
};
Date TIME()
{
SYSTEMTIME systm;
GetLocalTime(&systm);
Date time(systm.wYear, systm.wMonth, systm.wDay);
return time;
}
void main()
{
vector<Employee*> employeeRef(3);
employeeRef[0] = &SalariedEmployee("Leona", 1990, 4, 4, "11-1111-111", 800);
employeeRef[1] = &CommisionEmployee("Kevin", 1989, 8, 1, "22-2222-222", 100000, 0.06);
employeeRef[0]->Print();
employeeRef[1]->Print();
}
修改如下:
employeeRef[0] = new SalariedEmployee("Leona", 1990, 4, 4, "11-1111-111", 800);
employeeRef[1] = new CommisionEmployee("Kevin", 1989, 8, 1, "22-2222-222", 100000, 0.06);
应该是最后指针赋值的时候出了问题,要用new运算符,不能直接新建一个匿名对象然后取地址,但是这是为什么呢,我发现这么做只有string类型的数据会出问题