【问题标题】:Trouble with using parent constructors使用父构造函数的问题
【发布时间】:2015-03-12 01:12:17
【问题描述】:

我目前正在处理一项任务,以扩展我们之前制作的程序,涉及头文件和父类的使用。在原始文件中,我有 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(parameters),其中参数是父类中的东西。但是,我不知道该放在哪里。抱歉写了这么多。我只是不知道如何缩小它。

哦,这里是 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;

}

【问题讨论】:

  • 通过 const 引用传递字符串。如果你要返回一个字符串成员,你可以通过 const 引用返回它。

标签: c++ inheritance constructor parent-child


【解决方案1】:

您需要的是成员初始化列表。 来自cppreference

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

一个简单的例子:

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

【讨论】:

    【解决方案2】:

    在 OCCCPerson.cpp 中,

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

    您基本上必须使用初始化列表。 在这里阅读更多:What are the rules for calling the superclass constructor?

    它的作用是调用父构造函数Person(string, string, OCCCDate) 并基本上执行this-&gt;firstName = firstNamelastNamedob 也是如此。但不是studentID,因为Person(string, string, OCCCDate) 构造函数没有为studentID 提供初始化。

    【讨论】:

    • 我明白了。所以我不需要实际的参数名称,只需要类型?
    • 我修好了。您需要名称,而不是类型。这是因为您正在调用父构造函数而不是重新声明它。犯了错误;它已被修复。
    • 谢谢。我能够从 OCCCPerson.cpp 中的构造函数调用父级。但是,它现在给了我一个错误。函数 'OCCCPerson::OCCCPerson(std::string,std::string,OCCCDate,std::string)' 已经有一个主体。我正在使用 Visual Studio 2012 顺便说一句
    • 是的,那是因为您在 OCCCDate.cpp 中重新定义了构造方法。你需要把你的拿出来换我的,或者让你的完好无损。
    • 但我做到了。你是 OCCCDate.cpp 中唯一的一个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 2018-01-31
    • 1970-01-01
    • 2011-08-23
    • 2021-04-05
    相关资源
    最近更新 更多