Object Hierarchy employee 程序 - 取消引用 cout 的指针

Object Hierarchy employee program - dereferencing pointer for cout

我是 c++ 的新手,正在为 class 开发一个项目。我知道我的某些功能不正确。我正在努力达到至少可以看到输出以继续处理它的程度。我已经包含了我正在尝试做的简短描述。

#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;
}

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

#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 因为变量名是相同的。接下来,在您的主要功能中,您必须拥有 Employee class,然后将其分配给 HourlyEmployeeSalariedEmployee,因为这些是多态性的原则。希望我对你有所帮助,如果你有任何问题,请不要犹豫。

`