对象层次结构员工程序 - 解除引用cout的指针

时间:2016-04-24 19:36:30

标签: c++ polymorphism virtual-functions

我是c ++的新手,正在为一个课堂上的项目工作。我知道我的一些功能不正确。我试图找到一个点,我至少可以看到输出继续工作。我已经简要介绍了我要做的事情。

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

class Employee
{
protected:
    char* name[50];
public:

Employee()
{

}

Employee(char* name)
{
    strcpy(name, name);
}

char* getName()
{
    return *name;
}
void setName(char* name)
{
    name = name;
}

/*virtual ~Employee()
{
    delete[] name;
}*/

virtual void print() = 0;


};

class HourlyEmployee : public Employee
{
private:
    float HourlySalary;
public:


HourlyEmployee()
{
    HourlySalary = 0;
}
HourlyEmployee(char* name, float HourlySalary)
{
    name = name;
    HourlySalary = HourlySalary;
}
double getHourlySalary()
{
    return HourlySalary;
    //cout << "What is your Hourly Salary" << endl;
    //cin >> HourlySalary;
}
void setHourlySalary(double HourlySalary)
{

}
void print()
{
    cout << "Hourly Employee Name: " << &name << endl
        << "Salary: " << &HourlySalary << "per hour" << endl;
}

};

class SalariedEmployee : public Employee

{
private:
    float MonthlySalary;

public:

SalariedEmployee()
{
    MonthlySalary = 0;
}
SalariedEmployee(char* name, float MonthlySalary)
{

}
double getMonthlyySalary()
{
    return MonthlySalary;
    //cout << "What is your Hourly Salary" << endl;
    //cin >> MonthlySalary;
}
void setMonthlySalary(double MonthlySalary)
{

}
void print()
{
        cout << "Hourly Employee Name: " << name << endl
            << "Salary: " << MonthlySalary << "per month" << endl;
}

};

int main() {
SalariedEmployee* S = new SalariedEmployee();
SalariedEmployee S1("Joe Bob", '4500');

HourlyEmployee* H = new HourlyEmployee();
HourlyEmployee H1("Jim Bob", '20');

S1.print();
H1.print();

delete S, H;

system("pause");
return 0;
}

1 个答案:

答案 0 :(得分:1)

从练习的描述中我得出结论,你要求的是这样的东西:

#include <iostream>
using namespace std;

class Employee
{
protected:
    char name[50];
public:

    Employee()
    {

    }

    Employee(char* name)
    {
        strncpy_s(this->name, 49, name, 49);
    }

    char* getName()
    {
        return this->name;
    }

    void setName(char *name)
    {
        strncpy_s(this->name, 49, name, 49);
    }

    virtual void print() = 0;
};

class HourlyEmployee : public Employee
{
private:
    float hourlySalary;
public:


    HourlyEmployee()
    {
        hourlySalary = 0;
    }
    HourlyEmployee(char* name, float HourlySalary)
    {
        strncpy_s(this->name, 49, name, 49);
        this->hourlySalary = HourlySalary;
    }

    double getHourlySalary()
    {
        return hourlySalary;
        //cout << "What is your Hourly Salary" << endl;
        //cin >> HourlySalary;
    }

    void setHourlySalary(double HourlySalary)
    {
        this->hourlySalary = HourlySalary;
    }

    void print()
    {
        cout << "Hourly Employee Name: " << this->name << endl
            << "Salary: " << hourlySalary << " per hour" << endl;
    }

};

class SalariedEmployee : public Employee

{
private:
    float MonthlySalary;

public:

    SalariedEmployee()
    {
        MonthlySalary = 0;
    }

    SalariedEmployee(char* name, float MonthlySalary)
    {
        strncpy_s(this->name, 49, name, 49);
        this->MonthlySalary = MonthlySalary;
    }

    double getMonthlyySalary()
    {
        return MonthlySalary;
        //cout << "What is your Hourly Salary" << endl;
        //cin >> MonthlySalary;
    }

    void setMonthlySalary(double MonthlySalary)
    {
        this->MonthlySalary = MonthlySalary;
    }

    void print()
    {
        cout << "Hourly Employee Name: " << name << endl
            << "Salary: " << MonthlySalary << " per month" << endl;
    }

};

int main() 
{
    Employee * employee[2];
    employee[0] = new SalariedEmployee("Joe Bob", 4000);
    employee[1] = new HourlyEmployee("Jim Bob", 20);

    for (int i = 0; i < 2; i++)
    {
        employee[i]->print();
    }

    for (size_t i = 0; i < 2; i++)
    {
        delete employee[i];
    }
    system("pause");
    return 0;
}

首先,你的name变量必须是char [50]类型,而不是* char [50]。所以我使用strncpy(或安全函数strncpy_s)来复制构造函数中的name。由于您正在处理字符,因此无法像在代码的某些部分中那样分配字符,例如name = name。我使用this->name,因为变量名称是相同的。接下来,在您的main函数中,您必须拥有Employee类,然后将其分配给HourlyEmployeeSalariedEmployee,因为这些是多态的原则。希望我能帮到你,如果你有任何问题,请不要犹豫。

`