Creating object within object

So I have a header file which consists of a class called "Date" which is a class that stores attributes for dates such as, day, month, year, checks if its a leap year etc.

Then, I have a seperate cpp which consists of my main function called "Patient" which will create an object consisting of attributes of a patient.

My problem is this: One of the parameters for patient is the object date itself. So for example:

the constructor looks like this:
 
Patient :: Patient(int iden, const char *fname, const char *lname , Date newDate, int dID); //Put in default values  


So when I create a patient object, I would put:

Patient John(2303, John, Malcovich, ????, 19910);

I'm confused as to what I'm supposed to put for the Date parameter? Since Date is a seperate object from a header file

I'm having a little trouble understanding what you mean. From what I gather, though, this should work:
1
2
3
4
5
6
7
8
9
10
11
// date.h
#ifndef __DATE_H__
#define __DATE_H__

class Date {
public:
    Date(/* ... */);
    // ...
};

#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
// patient.h
#ifndef __PATIENT_H__
#define __PATIENT_H__

#include "date.h"

class Patient {
public:
    Patient(int id, const char* f, const char* l, Date d, int dID);
    // ...
};

#endif 

1
2
3
4
5
6
// main.cpp
#include "patient.h"
#include "date.h"

// ...
Patient john(2303, "John", "Malcovich", Date(/* ... */), 19910);

Does that answer your question?

EDIT:
If your Date constructor only takes one parameter, such as a string, you could just pass the parameter to the function, but it's less readable. If this is not something you want to be able to happen, you can put explicit in front of the constructor:
1
2
3
4
5
class Date {
public:
    explicit Date(std::string date);
    // ...
};
Last edited on
Yes that does answer my question! Why do we have to do it like Date(...)? I thought it would be more like John(...) or Date John(...)
Another question:

How do I compare values from a main class to an object?

Lets say we have a patient object called John(2303,...);

the 2303 is his ID number

I ask the user to enter an ID number and save it to a variable called checkID;

then I need an if statement that will check if checkID == a patients ID, how do I perform this condition?
alex067 wrote:
Why do we have to do it like Date(...)?

You are passing an object of type Date to your Patient constructor. Because I'm creating the object inline with the constructor call, I just create an instance of Date by giving the type and calling it's constructor. If you like, it's shorthand for this:
1
2
Date johns_date(/* ... */);
Patient john(2303, "John", "Malcovich", johns_date, 19910);

The only difference is that my previous example we don't give the variable a name, and we can't use the date somewhere else in the program.

alex067 wrote:
How do I compare values from a main class to an object?

Create a function in the class to facilitate that. There are two ways you can do that: a 'get' function that returns the ID; or a 'check' function that compares an ID with itself. Here are examples of both:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Patient {
public:
    // store the parameter in a variable
    Patient(int id /*, ...*/) : _id(id) {}

    // 'get' function; just returns the value
    int getID() const { 
        return this->_id; 
    }

    // 'check' function; compare IDs
    bool compareID(int id) const { 
        return this->_id == id; 
    } 

private:
    int _id;
};

// ...

Patient john(2303 /*, ...*/);
int checkID;

std::cin >> checkID;

if (checkID == john.getID()) { 
    // ...
}

// or

if (john.compareID(checkID)) {
    // ...
}
Thank you so much for your input!!

Another question:

When I create a new patient object, I need to place this object in an array of objects.

1
2
3
4
5
6
7
8
9
10
11
12
++counter;
			int d, m, y;
			string first, last;
			int id = 0, Did;
			id = counter;
			cout << "Enter the first and last name of the new patient:" << endl;
			cin >> first; cin.ignore() >> last;
			cout << "Enter the birthdate (MM/DD/YY): " << endl;
			cin >> m; cin.ignore('/'); cin >> d; cin.ignore('/'); cin >> y;
			cout << "Enter the doctor's ID: " << endl;
			cin >> Did;
			Patient first(id, first, last, Date(m, d, y), Did);


I have a problem where, when I try to add this Patient object to an array of patient objects called CheckIn, I would do:

CheckIn[--id] = first;

where ID is just a simple increment, so if this runs for the first time, the patient will get the id of 1, and I want to place this patient onto my array of CheckIn as the first element.

When I do this, it says that first is a string, which it is, but I want it to be first as in the object, not the string
EDIT: On your example where you do the ID check, you're already comparing it to a known patient.

what i'm asking is, how do you compare the ID with all the patients ID to check if it exists? In this case, there wouldn't be a Patient John, because we don't know if john exists.
I'm going to make some assumptions about your array here.
1
2
const unsigned MAX_PATIENTS = 10; // or some number
Patient checkIn[MAX_PATIENTS]; // are you creating your array like this? 

If you want an array of patients, I will presume you have something like the above in place. If so, you need a default constructor that creates 'invalid' patients, so the above code works.

I'm also not sure what you're doing with the --id bit; surely, if anything, it's ++id or id++? I need more information.

All these problems can be solved, however, by using the eminent std::vector:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <vector>

// ...

std::vector<Patient> patients; // no need to give a size or default constructor

// ...

Patient p(id, first, last, Date(m, d, y), Did);
patients.push_back(p); // put the patient at the end of the array
// or
patients.emplace_back(id, first, last, Date(m, d, y), Did); // construct inside object

patients[0].getID(); // get the ID of the first patient

// loop over entire array with one of:
for (int i = 0; i < patients.size(); ++i) { patients[i].getID(); ... }
for (auto it = patients.begin(); it != patients.end(); ++it) { it->getID(); ... }
for (auto&& p : patients) { p.getID(); ... }


EDIT:
Just noticed your last sentence. You defined first as a string on line 3, but you're trying to define first again on line 12 as a Patient.

EDIT2:
If you have an array of patients, the easiest method for you would probably be to just loop through and see if it exists:
1
2
3
4
5
6
7
8
// assuming you're using a C-style array
bool doesPatientExist(Patient* patients, int checkID) {
    for (int i = 0; i < NUM_PATIENTS; ++i) {
        if (patients[i].getID() == checkID)
            return true;
    }
    return false;
}
Last edited on
I see I see, thank you I am learning a lot.

Last question (I hope), In my dynamic array of object Patients, I am trying to add an entry to this array.

So I created a temp object which will hold the attributes that I want transferred onto my array of objects.

However, when I create this temp object, one of the values for the parameters say: "no instance of constructor"

It occurs in the (id,...)

 
Patient temp(id, first, last, Date(m, d, y), Did);


The constructor for Patient looks like:
Patient(int ID, const char *, const char *, Date, int); //Put in default values


the variable id IS an int, so why isn't it accepting it?
I do not have enough information to work it out, unfortunately. If you copy/paste the entire error message, and the surrounding code of the error location, I might be able to help.
Topic archived. No new replies allowed.