0

I'm currently working on an assignment to expand on a program we previously made, involving the use of header files, and parent classes. In the original, I have 2 header files. Person.h, and OCCCDate.h. In the new one, I am creating one called OCCCPerson.h. It's an incredibly simple class that basically just uses Person, just with 1 added variable.

My problem is, I cant figure out how to use the parent constructor properly.

Here is the Person.h file.

#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

And here is my OCCCPerson.h file

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

I cant seem to call on the parents constructor to get things like the firstname, lastname, and dob(date of birth). From my handout, it says the parent constructor has to be initialized with, : Person(parameters), where parameters are things in the parent class. However, I have no idea where to put that. Sorry for writing so much. I just couldn't figure out how to shrink that down.

Oh, and here is OCCCDate.h just in case

#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

And here is my OCCCDate.cpp file

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

}
Historiun
  • 103
  • 2
  • 8
  • Pass strings by const reference. If you are returning a string member, you can return it by const reference. – Neil Kirk Mar 12 '15 at 01:20

2 Answers2

0

What you need is member initializer lists. From cppreference:

In the definition of a constructor of a class, member initializer list specifies the initializers for direct and virtual base subobjects and non-static data members.

A simple example:

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

}

In your case, you can do:

OCCCPerson(string firstName, string lastName, OCCCDate dob, string studentID) :
  Person(firstName, lastName, dob) {
  this.studentID = studentID;
}
Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54
0

In OCCCPerson.cpp,

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

You basically have to use an initializer list. Read more here: What are the rules for calling the superclass constructor?

What this does is it calls the parent constructor Person(string, string, OCCCDate) and basically performs this->firstName = firstName. Similarly for lastName and dob. But not studentID because the Person(string, string, OCCCDate) constructor doesn't provide initialization for studentID.

Community
  • 1
  • 1
Don Larynx
  • 685
  • 5
  • 15
  • I see. So I don't need the actual parameter names, just the types? – Historiun Mar 12 '15 at 01:19
  • I fixed it. You need the names, and not the types. This is because you're calling the parent constructor and not re-declaring it. Mistakes were made; it's been fixed. – Don Larynx Mar 12 '15 at 01:21
  • Thank you. I was able to call the parent from the constructor in OCCCPerson.cpp. However, it now gives me an error. function 'OCCCPerson::OCCCPerson(std::string,std::string,OCCCDate,std::string)' already has a body. Im using visual studio 2012 btw – Historiun Mar 12 '15 at 01:23
  • Yes, that's because you're re-defining the constructor method in OCCCDate.cpp. You would need to take out yours for mine or leave yours intact. – Don Larynx Mar 12 '15 at 01:27
  • But I did that. Yours is the only one there in OCCCDate.cpp. – Historiun Mar 12 '15 at 01:30
  • No, you also have 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; } – Don Larynx Mar 12 '15 at 01:31
  • Well then what am I supposed to have written in the constructor? where yours says //more stuff? I have to get those values somehow. – Historiun Mar 12 '15 at 01:33
  • You already did, however, when you called the parent constructor with an initialization list. It set the OCCCPerson's object values equal to the parent constructor's parameters. All that is missing is `studentID` – Don Larynx Mar 12 '15 at 01:37
  • Sorry. I'm still confused. I have the constructor definition in the OCCCPerson.h file, then the actual constructor in OCCCPerson.cpp. Written as you said it should be. Dont I need something like, this->firstName = firstName? How do I assign the values? – Historiun Mar 12 '15 at 01:39
  • I've edited my answer. If this doesn't clear things up, let me know. @Historiun – Don Larynx Mar 12 '15 at 01:49
  • Oh I see. So within that constructor, all I need is, this->studentID = studentID? – Historiun Mar 12 '15 at 01:51