使用父构造函数时出现问题

Trouble with using parent constructors

本文关键字:问题 构造函数      更新时间:2023-10-16

我目前正在做一项任务,以扩展我们之前制作的程序,涉及使用头文件和父类。在原版中,我有 2 个头文件。Person.h和OCCCDate.h。在新的中,我正在创建一个名为OCCCPerson.h的。这是一个非常简单的类,基本上只使用 Person,只是添加了 1 个变量。

我的问题是,我不知道如何正确使用父构造函数。

这是 Person.h 文件。

#ifndef PERSON_H
#define PERSON_H
#include <string>
#include "OCCCDate.h"
using namespace std;
class Person{
private:
string firstName;
string lastName;
OCCCDate dob;
public:
Person();
Person(string, string);
Person(string, string, OCCCDate);
string getFirstName();
string getLastName();
void setFirstName(string);
void setLastName(string);
int getAgeInYears();
bool equals(Person);
string toString();
};
#endif

这是我的OCCCPerson.h文件

#ifndef OCCCPERSON_H
#define OCCCPERSON_H
#include <string>
#include "OCCCDate.h"
#include "Perosn.h"
using namespace std;
class OCCCPerson : Person{
protected:
string studentID;
public:
OCCCPerson(string firstName, string lastName, OCCCDate dob, string studentID);
OCCCPerson(Person p, string studentID);
string getStudentID();
bool equals(OCCCPerson p);
string toString();
};
#endif;

我似乎无法要求父母构造函数来获取诸如名字,姓氏和dob(出生日期)之类的东西。从我的讲义中,它说父构造函数必须初始化:Person(参数),其中参数是父类中的东西。但是,我不知道把它放在哪里。对不起写了这么多。我只是不知道如何缩小它。

哦,这是OCCCDate.h以防万一

#ifndef OCCCDATE_H
#define OCCCDATE_H
#include<string>
using namespace std;
class OCCCDate{
private:
    bool OCCCDate_US;
    bool OCCCDate_EURO;
    int dayOfMonth, monthOfYear, year;
    bool dateFormat;
public:
    OCCCDate();
    OCCCDate(int dayOfMonth, int monthOfYear, int year);
    int getDayOfMonth();
    int getMonth();
    string getNameOfMonth();
    int getYear();
    string getDate();
    int getDifference(OCCCDate d1, OCCCDate d2);
    int getDifference(OCCCDate d1);
    void setDateFormat(bool);
    bool equals(OCCCDate d);
    string toString();
};
#endif

这是我的OCCCDate.cpp文件

#include<iostream>
#include<ctime>
#include "OCCCPerson.h"
using namespace std;
OCCCPerson::OCCCPerson(string firstName, string lastName, OCCCDate dob, string studentID):Person(firstName, lastName, dob){
    string firstName = Person::getFirstName();
    string lastName = Person::getLastName();
    OCCCDate dob = dob;
    this->studentID = studentID;
}
OCCCPerson::OCCCPerson(Person p, string studentID){
    Person p = p;
    this->studentID = studentID;
}

您需要的是成员初始值设定项列表。从 cpp 首选项:

在类的构造函数的定义中,成员初始值设定项列表指定直接和虚拟基子对象以及非静态数据成员的初始值设定项

一个简单的例子:

class MyClass : BaseClass
{
  public:
    MyClass(int arg) : BaseClass(arg) {
      //Rest of code
    }
}

在您的情况下,您可以执行以下操作:

OCCCPerson(string firstName, string lastName, OCCCDate dob, string studentID) :
  Person(firstName, lastName, dob) {
  this.studentID = studentID;
}

在OCCCPerson.cpp中,

OCCCPerson(string firstName, string lastName, OCCCDate dob, string 
studentID) : Person(firstName, lastName, dob)
{
//more stuff
}

您基本上必须使用初始值设定项列表。在此处阅读更多内容:调用超类构造函数的规则是什么?

这样做是它调用父构造函数Person(string, string, OCCCDate)并基本上执行this->firstName = firstName。同样适用于lastNamedob.但不是studentID因为Person(string, string, OCCCDate)构造函数不提供studentID初始化。