Good evening folks. Im a Computing student in 2nd year of Computer Science at university. My current problem is the idea of a class within a class.

I have developed a .h and.cpp file for a Date class(day, moth year), and a .h and.cpp file for a patients record. I have only included name and date of operation forthe patient to keep things simple.

My problem is, we were given the .h patient file and told to develop the .cpp. This is what we were given:

#include <iostream>; 
#include <string>; 
#include "Date.h";

using namespace std; 

class Patient {
public:
// ****************** CONSTRUCTORS ************************************
Patient();
// Default Constructor
Patient (string, Date);
// ****************** SET FUNCTIONS ************************************
void setName(string);
void setDate(int, int, int);
// ****************** GET FUNCTIONS ************************************
string getName() const;
Date getDate() const;
// ****************** DISPLAY FUNCTION ************************************
void displayAll() const;
void printName() const;
void printDate() const;
private:
int patientNumber;
Date dateOfOp;
};

However as you can see above --> Patient (string, Date);

This line is givng me trouble. Ive had to take out "Date" and put in "int, int, int" instead because for some reason I am finding it difficult to link in the required information. But I have a feeling this is the very aspect of the program tha Im going to be assessed on tomrrow and I just cant get it.

This is the restof my code:

Patient.cpp

#include <iostream>
#include<string>
#include "Patient.h"
using namespace std;

//Constructors

Patient::Patient()
{
	name = "";
	dateOfOp.setDay(0);
	dateOfOp.setMonth(0);
	dateOfOp.setYear(0);
}

Patient::Patient(string pName, int pDay, int pMonth, int pYear)
{
	name = pName;
	dateOfOp.setDay(pDay);
	dateOfOp.setMonth(pMonth);
	dateOfOp.setYear(pYear);
}

//set methods
void Patient::setName(string pName)
{
	name = pName;
}

void Patient::setDate(int pDay, int pMonth, int pYear)
{
	dateOfOp.setDay(pDay);
	dateOfOp.setMonth(pMonth);
	dateOfOp.setYear(pYear);
	
}

//get methods
string Patient::getName() const
{
	return name;
}

Date Patient::getDate() const
{
	return dateOfOp;
}

//display methods

void Patient::displayName() const
{
	cout<<"Name is: "<<name<<endl;
}



void Patient::displayAll() const
{
	cout<<"Date of op: "<<name<<"\n"<<dateOfOp.getDay()<<", "<<dateOfOp.getMonth()<<", "<<dateOfOp.getYear()<<endl;

}

Date.h

#include <iostream>
#include <string>

using namespace std;

class Date
{
public:
	//Constructors
	Date(); 
	Date(int,int,int);

	//set functions
	void setDate(int,int,int);
	void setDay(int);
	void setMonth(int); 
	void setYear(int); 

	//get functions
	int getDay() const; 
	int getMonth() const; 
	int getYear() const; 

	//display function
	void displayDate() const;
	void displayDay() const; 

	void displayMonth() const;

	void displayYear() const;


private:
	int day, month, year;

};
#include<iostream>
#include<string>
#include "Date.h"

using namespace std;

//constructors
Date::Date()
{
	day = 1;
	month = 1;
	year = 2001;

}

Date::Date(int pDay, int pMonth, int pYear)
{
	day = pDay;
	month = pMonth;
	year = pYear;
}

//set functions
void Date:: setDate(int pDay, int pMonth, int pYear)
{
	day = pDay;
	month = pMonth;
	year = pYear;
}


void Date::setDay(int pDay)
{
day = pDay;
}

void Date::setMonth(int pMonth)
{
month = pMonth;
}

void Date::setYear(int pYear)
{
year = pYear;
}

//get functions

int Date::getDay() const
{
	return day;
}

int Date::getMonth() const
{
	return month;
}

int Date::getYear() const
{
	return year;
}

//Display methods

void Date::displayDate() const
{
	displayDay();
	displayMonth();
	displayYear();

}

void Date::displayDay() const
{
	cout<<"Day is: "<<day<< endl;

}
void Date::displayMonth() const
{
	cout<<"Month is: "<<month<< endl;

}
void Date::displayYear() const
{
	cout<<"Year is: "<<year<< endl;

}

I really just want to know how I can use date above instead of int, int, int. Then how can I print this information out ?

Sorry about the length but I wanted to include everything that may be important.

Edited 6 Years Ago by mdpepper: n/a

Your function implementation doesn't match your function declaration.

Implementation of overloaded constructor from Patient.cpp:

Patient::Patient(string pName, int pDay, int pMonth, int pYear)
{
	name = pName;
	dateOfOp.setDay(pDay);
	dateOfOp.setMonth(pMonth);
	dateOfOp.setYear(pYear);
}

This is how you've implemented your overloaded constructor. Have a closer look at the function's header line:

Patient::Patient(string pName, int pDay, int pMonth, int pYear)

Does this match the function's declaration within the class?

Patient (string, Date);

As FBody said, of course, you need to match the function declaration in the cpp file to take a Date as second parameter instead of three ints. Then, it should work. Just in case you previously changed it to three ints because you had trouble creating a patient object, here is how:

class Patient {
//...
    Patient(string, Date);
//...
};

int main() {
  Patient p1("roger",Date(11,11,2010));
};
This article has been dead for over six months. Start a new discussion instead.