So its fun when you have questions for hw but neither the teacher nor the textbook show you how to do the HW question. Anyway, I have my base class, a derived class, and class with composition of the derived class. I am not sure how to create the constructor for that. This is what I got but its not right and is regard to the Billing constructor.

#include <iostream>
#include <string>

using namespace std;

class Person
{
protected:
	string name;

public:
	Person():name(""){cout << "Default Person Constructor" << endl;}
	Person(string theName):name(theName){cout << "Parameterized Person Constructor" << endl;}
	Person(const Person& theObject);
	~Person(){cout << "Person Destructor" << endl;}
	string getName() const{return name;}
	Person& operator=(const Person& rtSide)
	{
		string Name;
		Name = rtSide.name;
		return *this;
	}
	friend istream& operator >>(istream& inStream, Person& personObject)
	{
		inStream >> personObject.name;
		return inStream;
	}
	friend ostream& operator <<(ostream& outStream, const Person& personObject)
	{
		outStream << "Person's name: " << personObject.name << endl;
		return outStream;
	}
};

class Doctor
{
protected:
	string specialty;
	double fee;

public:
	Doctor():specialty(""), fee(0){cout << "Default Doctor Constructor" << endl;}
	Doctor(string Specialty):specialty(Specialty), fee(0){ cout << "Specialty Doctor Constructor" << endl;}
	Doctor(double Fee):specialty(0), fee(Fee){cout << "Fee Doctor Constructor" << endl;}
	Doctor(string Specialty, double Fee):specialty(Specialty), fee(Fee){cout << "Parameterized Doctor Constructor" << endl;}
	~Doctor(){cout << "Doctor Destructor" << endl;}
	string getSpecialty() const{return specialty;}
	double getFee() const{return fee;}
	const Doctor& operator=(const Doctor& rtSide)
	{
		string Specialty;
		double Fee;
		Specialty = rtSide.specialty;
		Fee = rtSide.fee;
		return *this;
	}
	Doctor(const Doctor& theObject);
	friend ostream& operator <<(ostream& outStream, const Doctor& docObject)
	{
		outStream << "Doctor's Speciality: " << docObject.specialty << endl;
		return outStream;
	}
};

class Patient: public Person
{
protected:
	Doctor phys;

public:
	Patient(): Person(), phys(){cout << "Default Patient Constructor" << endl;}
	Patient(string Name, string Specialty, double Fee): Person(Name), phys(Specialty, Fee){cout << "Parameterized Patient Constructor" << endl;}
	~Patient(){cout << "Patient Destructor" << endl;}
	//friend ostream& operator <<(ostream& outStream, const Patient& patObject)
	//{
	//	Doctor::operator<<(outStream, patObject);
	//	/*outStream << "Doctor's Speciality: " << phys.getSpecialty;*/
	//	return outStream;
	//}
};

class Billing
{
protected:
	double amtdue;
	Patient pat;
	Doctor doc;

public:
	Billing(): amtdue(-1.0), pat(), doc(){cout << "Default Billing Constructor" << endl;}
	Billing(double Amtdue, string Name, string Specialty, double Fee): amtdue(Amtdue), Patient(Person(Name)), doc(Specialty, Fee){cout << "Parameterized Patient Constructor" << endl;}
	~Billing(){cout << "Billing Destructor" << endl;}
};


int main()
{
	Doctor z1("Oncologist", 50);
	Doctor z2("Pathologist", 100);
	Patient x1("Erica", "Oncologist", 50);
	Patient x2("Tom", "Pathologist", 100);
	Billing y1;
	Billing y2;
	cout << z1;
	cout << z2;



	return 0;
}

Dani AI

Generated

Short addendum to the thread: the original compiler error came from trying to "construct the type instead of the member" in an initializer list, which spotted; fixed that in post #3 by moving the Patient construction into the initializer list. A few practical fixes and cleanups that make the design safer and easier to maintain follow.

Add an accessor to let Billing reuse the Patient's Doctor rather than duplicating data, and prefer taking an existing Patient by const reference when appropriate:

// add to Patient
const Doctor& getDoctor() const { return phys; }

// Billing that accepts a Patient object
Billing(double amt, const Patient& p)
  : amtdue(amt), pat(p), doc(p.getDoctor()) { }

Fix the obvious bugs in the original class implementations: assignment operators must actually assign to the member and should return a non-const reference; single-argument string parameters are best taken as const std::string&; and initializing std::string with 0 is invalid. Examples:

Person& Person::operator=(const Person& rhs) {
  if (this != &rhs) name = rhs.name;
  return *this;
}

Doctor::Doctor(double fee) : specialty(""), fee(fee) { }

Final checklist to avoid subtle problems:

  • Members are initialized in declaration order, not initializer-list order — keep the list consistent with declaration order to avoid surprises.
  • Avoid duplicating Doctor inside both Patient and Billing (or keep them consistent via accessors).
  • Either define or remove the declared copy constructors; prefer compiler-generated defaults if member-wise copying is fine (Rule of Zero/Three).
  • Make the base destructor virtual if polymorphic deletion through Person* is ever intended.
  • Prefer const std::string& parameters and avoid using namespace std; in headers.

These small changes make the constructors and member ownership clear, remove data duplication, and eliminate the most common sources of compile-time and runtime bugs in the posted code.

Recommended Answers

All 3 Replies

In line 91 you're trying to construct the member Patient which is a type not a member.
You did it right everywhere else though.

This, however, is not the only thing wrong with the 'Patient' construction in Billing.
You're constructing a Patient from a Person but you haven't specified a Patient constructor expecting a Person argument.

I fixed up the code and noticed my mistake.

#include <iostream>
#include <string>

using namespace std;

class Person
{
protected:
	string name;

public:
	Person():name(""){cout << "Default Person Constructor" << endl;}
	Person(string theName):name(theName){cout << "Parameterized Person Constructor" << endl;}
	Person(const Person& theObject);
	~Person(){cout << "Person Destructor" << endl;}
	string getName() const{return name;}
	Person& operator=(const Person& rtSide)
	{
		string Name;
		Name = rtSide.name;
		return *this;
	}
	friend istream& operator >>(istream& inStream, Person& personObject)
	{
		inStream >> personObject.name;
		return inStream;
	}
	friend ostream& operator <<(ostream& outStream, const Person& personObject)
	{
		outStream << "Person's name: " << personObject.name << endl;
		return outStream;
	}
};

class Doctor
{
protected:
	string specialty;
	double fee;

public:
	Doctor():specialty(""), fee(0){cout << "Default Doctor Constructor" << endl;}
	Doctor(string Specialty):specialty(Specialty), fee(0){ cout << "Specialty Doctor Constructor" << endl;}
	Doctor(double Fee):specialty(0), fee(Fee){cout << "Fee Doctor Constructor" << endl;}
	Doctor(string Specialty, double Fee):specialty(Specialty), fee(Fee){cout << "Parameterized Doctor Constructor" << endl;}
	~Doctor(){cout << "Doctor Destructor" << endl;}
	string getSpecialty() const{return specialty;}
	double getFee() const{return fee;}
	const Doctor& operator=(const Doctor& rtSide)
	{
		string Specialty;
		double Fee;
		Specialty = rtSide.specialty;
		Fee = rtSide.fee;
		return *this;
	}
	Doctor(const Doctor& theObject);
	friend ostream& operator <<(ostream& outStream, const Doctor& docObject)
	{
		outStream << "Doctor's Speciality: " << docObject.specialty << endl;
		return outStream;
	}
};

class Patient: public Person
{
protected:
	Doctor phys;

public:
	Patient(): Person(), phys(){cout << "Default Patient Constructor" << endl;}
	Patient(string Name, string Specialty, double Fee): Person(Name), phys(Specialty, Fee){cout << "Parameterized Patient Constructor" << endl;}
	~Patient(){cout << "Patient Destructor" << endl;}
	//friend ostream& operator <<(ostream& outStream, const Patient& patObject)
	//{
	//	Doctor::operator<<(outStream, patObject);
	//	/*outStream << "Doctor's Speciality: " << phys.getSpecialty;*/
	//	return outStream;
	//}
};

class Billing
{
protected:
	double amtdue;
	Patient pat;
	Doctor doc;

public:
	Billing(): amtdue(-1.0), pat(), doc(){cout << "Default Billing Constructor" << endl;}
	Billing(double Amtdue): amtdue(Amtdue){cout << "Amount Due Parameter Billing Constructor" << endl;}
	Billing(double Amtdue, string Name, string Specialty, double Fee): amtdue(Amtdue), pat(Name, Specialty, Fee), doc(Specialty, Fee){cout << "Parameterized Patient Constructor" << endl;}
	~Billing(){cout << "Billing Destructor" << endl;}
	double getamt() const{return amtdue;};
	const Billing operator +(const Billing& rhs) const
	{
		double adue = amtdue + rhs.amtdue;
		return Billing(adue);
	}
};


int main()
{
	Doctor z1("Oncologist", 50);
	Doctor z2("Pathologist", 100);
	Patient x1("Erica", "Oncologist", 50);
	Patient x2("Tom", "Pathologist", 100);
	Billing y1(50);
	Billing y2(100);
	cout << z1;
	cout << z2;
	Billing i = y1 + y2;
	cout << "Sum of Bill 1 and Bill 2: " << i.getamt() << endl;

	return 0;
}

Write a simple application that can store multiple students' names, student number & email address. The application will store grades for the module and be able to predict the overall mark. The application should be able to:

Add a new student with their name, student number & email address.

List the student's names & student numbers with current total mark.

Input marks for assignments, or update existing marks already in the application

Show the students marks for all 4 C++ assignments

Grade predictor: enter test grades into the 4 assignments to see what the overall mark is.

Show a list of all students in grade order.

Save data to a file

Read data from a file

Once marks for the first 3 assignments are in, the application will be able to tell the user the mark the student needs in the final assignment to achieve 70% all together.

Data can be stored in a file called "studentmarks.txt" C++ programming is scored out of 100% overall.

Tutorial Exercises 1 is worth 10% of the overall mark

Tutorial Exercises 2 is worth 20% of the overall mark

Practical Coursework 3 is worth 30% of the overall mark

Practical Coursework 4 is worth 40% of the overall mark

Grade Classifications are given as follows Overall Grade

Percentage Classification <40% Fail 40% - 49%

Third Class Honours 50%- 59% Lower

Second-Class Honours (2:2) 60%- 69%

Upper Second-Class Honours (2:1)

70% - 100% First Class Honours

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.