Need insight with my inherited class

Hello! I'm having trouble getting my code to give me the correct answer when the 'Doctors' do not match. Thank you for any insight you can give me!

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <iostream>
using namespace std;

class Person
{
private:
    string fName;
    string lName;
public:
    //a default constructor to initialize both data members to empty strings,
    Person();
    //a constructor to accept two string parameters and use them to initialize the first and last name,
    Person(string x, string y);
    //a copy constructor.
    Person copyPerson(Person a);
    //Also include appropriate accessor and mutator member functions
    string getFName();
    string getLName();
    
    friend bool operator ==(const Person a, const Person b);
    friend bool operator !=(const Person a, const Person b);
    friend bool operator >>(const Person a, const Person b);
    friend bool operator <<(const Person a, const Person b);
    Person operator =(const Person b);
};

class Doctor : public Person
{
private:
    double hourRate;
public:
    //default constructor to initialize the rate to 0
    Doctor();
    //constructor that takes a double and a reference to an object of type Person and initializes the data
    //members to their appropriate values.
    Doctor(double x, Person &a);
    //Also include an accessor and mutator member functions for the doctor's hourly rate.
    double getRate();
    friend bool operator ==(const Doctor a, const Doctor b);
    void setRate(double& z);
    //In Doctor class, redefine the operator = such that it not only copies the first and last name, but also
    //copies the hourly rate
    Doctor operator =(Doctor d);
};

class Patient : public Person
{
private:
    //This class should have a data member to hold the patient's primary physician (of
    //class Doctor).
    Doctor m;
public:
    //a default constructor that would call the default constructors of class Doctor and Person to initialize the object;
    Patient();
    // a constructor that accepts a reference to an object of class Doctor and Person
    //and initializes the Patient object's data members to their respective values.
    Patient(Doctor x, Person y);
    //Add accessor and mutator member functions to access or set the primary
    //physician.
    Doctor getDoctor();
    void setDoctor(Doctor s);
};

class Billing : public Person
{
private:
    //This class should have the following data members: an object of
    // type Doctor to hold the doctor to whom the money is paid, an object of type
    // Patient to hold the patient who pays the money, and a variable of type double
    Doctor g;
    Patient c;
    double amount;
public:
    //default constructor that
    //initializes amount due to 0.0 and calls the default constructors for Patient
    //and Doctor objects and a constructor that accepts references to Doctor and
    //Patient objects and the amount of hours (type int)
    Billing();
    //* if the doctor involved in the bill is the patient's primary physician,
    //then the amount due is hours multiplied by the doctor's hourly rate;
    // * if the doctor involved is not the patient's primary physician, then
    //the amount due is hours times doctor's hourly rate times 1.25.
    Billing(Doctor &n, Patient &m, int hours);
    double getAmount();
};

/*
 Write a main function that would prompt the user to enter the patient's
 name, their primary physician's name and rate, another doctor's name and rate,
 and the amount of hours spent in the doctor's office. Then the program will
 calculate and output the amount patient owes for doctor's services.
 
 Note: two doctors are considered the same if and only if their names match
 (i.e. you can assume no doctors with the same name and different rates exist).*/

//-----------------------------------------------------------------------------------------

int main ()
{
    string first, last, dfirst, dlast;
    double rate;
    int hours;
    
    cout << "Enter: the patient's name: ";
    cin >> first >> last;
    Person newPers(first, last);
    
    cout << "Enter: primary physician's name and their rate: ";
    cin >> first >> last >> rate;
    Person primPhy(first, last);
    Doctor Ann(rate, primPhy);
    Patient x(Ann, newPers);
    x.setDoctor(Ann);
    
    cout << "Enter: a doctor's name and their hourly rate: ";
    cin >> dfirst >> dlast >> rate;
    cout << "Enter: amount of hours: ";
    cin >> hours;
    Person Doc(dfirst, dlast);
    Doctor Phil(rate, Doc);
    Billing bill = {Phil, x, hours};
    
    cout << "You owe: " << bill.getAmount() << " dollars." << endl;

    return 0;
}

//-----------------------------------------------------------------------------------------
//-Person---------------------------------------------------

Person::Person(){
    fName = "";
    lName = "";
}

Person::Person(string x, string y){
    fName = x;
    lName = y;
}

Person Person::copyPerson(Person z){
    Person newPerson;
    newPerson = z;
    return newPerson;
}

string Person::getFName(){
    return fName;
}

string Person::getLName(){
    return lName;
}

bool operator ==(Person a, Person b)
{
    return ((a.fName == b.fName) && (a.lName == b.lName));
}

bool operator !=(const Person a, const Person b){
    return ((a.fName != b.fName) && (a.lName != b.lName));
}

istream& operator >>(istream in, Person& f){
    char blank;
    string a, b;
    cin >> a;
    cin >> blank;
    cin >> b;
    f ={a, b};
    return cin;
}

ostream& operator <<(ostream& out, Person f){
    out << f.getFName() << " " << f.getLName();
    return out;
}

Person Person::operator =(const Person b){
    Person newPerson = b;
    return newPerson;
}

//-Doctor---------------------------------------------------

Doctor::Doctor(){
    Person();
    hourRate = 0.0;
}

Doctor::Doctor(double x, Person &a){
    Person(a.getFName(), a.getLName());
    hourRate = x;
}

double Doctor::getRate(){
    return hourRate;
}

bool operator ==(Doctor a, Doctor b)
{
    return a.getFName() == b.getFName() && a.getLName() == b.getLName();
}

void Doctor::setRate(double& z){
    hourRate = z;
}
Doctor Doctor::operator =(Doctor d){
    Doctor newDoc = d;
    double r = d.getRate();
    newDoc.setRate(r);
    return newDoc;
}

//-Patient---------------------------------------------------

Patient::Patient(){
    Doctor();
    Person();
}

Patient::Patient(Doctor x, Person y){
    Doctor m(x.getRate(), x);
    Person(y.getFName(), y.getLName());
}

Doctor Patient::getDoctor(){
    return m;
}

void Patient::setDoctor(Doctor s){
    m = s;
}

//-Billing---------------------------------------------------

Billing::Billing(){
    amount = 0;
}
Billing::Billing(Doctor &n, Patient &g, int hours){
    bool isPrimary;
    if((g.getFName() != n.getFName()) && (g.getLName() != n.getLName()))
        isPrimary = false;
    else
        isPrimary = true;
    
    if(isPrimary == false)
        amount += n.getRate() * hours * 1.25;
    else
        amount += n.getRate() * hours;
}

double Billing::getAmount(){
    return amount;
}
I'm having trouble getting my code to give me the correct answer when the 'Doctors' do not match.
What answer? What do you mean by 'do not match'?

By the way: Your assignment operators (operator =(...)) are wrong. They basically do nothing. You need to assign the internal data. E, g,:

1
2
3
4
5
Person &Person::operator =(const Person &b){ // Note: &
    fName = b.fName 
    lName = b.lName;
    return *this;
}
Further more: Since your assignment operator does what the default (compiler generated) would do it can be completely omitted.

The function copyPerson(...) is useless.

And finally: Billing is certainly not a Person. Better remove this inheritance.
Topic archived. No new replies allowed.