类构造函数不保存变量

时间:2019-04-13 20:10:37

标签: c++ class oop this

当尝试使用getter获取Employee类中变量的值时,不会返回或输出任何内容。

我尝试使用setter,因为它们仍然使用this->方法,所以不会做很多事情,但是没有达到我设置的值。

class Employee {
 Employee(int empNum, std::string name, std::string address, std::string 
phone);

private: 
 int empNum;
 std::string name;
 std::string address;
 std::string phone;
};

class HourlyEmployee : public Employee {
 HourlyEmployee(int empNum, std::string name, std::string address, 
std::string phone, double hourlyWage, double hoursWorked);

//getters
 double getHoursWorked();
 double getHourlyWage();

//setters
 void setHoursWorked(double hoursWorked);
 void setHourlyWage(double hourlyWage);

private:
 double hoursWorked;
 double hourlyWage;
}

//CPP file
Employee::Employee(int empNum, std::string name, std::string address, 
std::string phone) {
 this->empNum = empNum;
 this->name = name;
 this->address = address;
 this->phone = phone;
}
HourlyEmployee::HourlyEmployee(int empNum, std::string name, std::string 
address, std::string phone, double hoursWorked, double hourlyWage) {
 Employee(empNum, name, address, phone);
 this->hoursWorked = hoursWorked;
 this->hourlyWage = hourlyWage;
}

//main
HourlyEmployee hourly1(1, "H. Potter", "Privet Drive", "201-9090", 12.00, 
40.00);
cout << hourly1.getPhone() << " " << hourly1.getName() << " " << 
hourly1.getHoursWorked();

这不是完整的代码,但是它应该输出电话,姓名和工作时间,但是由于某种原因,它只输出两个空格,然后输出hoursWorked。我只能假定名称,电话等变量尚未实际设置,因此它们未返回任何内容。那么,如何设置这些变量呢?

3 个答案:

答案 0 :(得分:4)

C ++在构造函数主体中不专门处理构造函数调用。下一行

Employee(empNum, name, address, phone);

将构造一个Employee的新的,完全独立的实例,然后将其丢弃,因为没有将结果对象分配给任何对象。该对象字段的值不会复制到您正在创建的HourlyEmployee的实例中。

要在子类中使用超类构造函数,请在子类构造函数的定义中使用documentation

HourlyEmployee::HourlyEmployee(int empNum, std::string name, std::string 
address, std::string phone, double hoursWorked, double hourlyWage)
        : Employee(empNum, name, address, phone) {
    this->hoursWorked = hoursWorked;
    this->hourlyWage = hourlyWage;
}

这样,C ++将调用Employee的构造函数来初始化HourlyEmployee的构造函数正在初始化的同一对象。

您甚至可以走得更远,并将构造函数简化为成员初始值设定项列表。一个很好的副作用是,这避免了必须键入this->或提出不同的命名方案。

Employee::Employee(int empNum, std::string name, std::string address, 
std::string phone) 
        : empNum(empNum),
          name(name),
          address(address),
          phone(phone) {}

HourlyEmployee::HourlyEmployee(int empNum, std::string name, std::string 
address, std::string phone, double hoursWorked, double hourlyWage)
        : Employee(empNum, name, address, phone),
          hoursWorked(hoursWorked),
          hourlyWage(hourlyWage) {}

您可以从上面的链接中了解更多信息,但引用一些简短的解释即可:

  

在构成构造函数功能体的复合语句开始执行之前,所有直接基数,虚拟基数和非静态数据成员的初始化已完成。成员初始化程序列表是可以指定这些对象的非默认初始化的地方。

答案 1 :(得分:1)

您需要在子类的构造函数中使用 initializer list ,如下所示:

#include <iostream>

class Employee {
public:
 Employee(int empNum, std::string name, std::string address, std::string phone) {
   this->empNum = empNum;
   this->name = name;
   this->address = address;
   this->phone = phone;
 }

 std::string getPhone() { return phone; }
 std::string getName() { return name; }

private: 
 int empNum;
 std::string name;
 std::string address;
 std::string phone;
};

class HourlyEmployee : public Employee {
public:
 HourlyEmployee(int empNum, std::string name, std::string address, std::string phone, double hoursWorked, double hourlyWage) 
 : Employee(empNum, name, address, phone) {
   this->hoursWorked = hoursWorked;
   this->hourlyWage = hourlyWage;
 }

 double getHoursWorked() { return hoursWorked; }
 double getHourlyWage() { return hourlyWage; }

private:
 double hoursWorked;
 double hourlyWage;
};

int main(void)
{
  HourlyEmployee hourly1(1, "H. Potter", "Privet Drive", "201-9090", 12.00, 40.00);
  std::cout << hourly1.getPhone() << " " << hourly1.getName() << " " << 
  hourly1.getHoursWorked();
  return 0;
}

输出:

  

201-9090 H. Potter 12

但是,我强烈建议您将初始化列表用于所有类的数据成员,在这种情况下,即同时针对基类和派生类。

答案 2 :(得分:1)

派生类HourlyEmployee的构造函数以错误的方式调用基本构造函数。

您应该这样编写代码:

HourlyEmployee::HourlyEmployee(int empNum, std::string name, std::string 
    address, std::string phone, double hoursWorked, double hourlyWage) :
    Employee(empNum, name, address, phone)
{
    this->hoursWorked = hoursWorked;
    this->hourlyWage = hourlyWage;
}

此外,所有构造函数的参数名称都与成员名称相同,这不是一个好主意。最好用不同的名称命名。