My professor has assigned a lab that we will later be using to learn how to abuse the various types of data structures. In this first lab he asks for the default constructor, a copy constructor, and an overloaded constructor to accept the appropriate data, which you'll see in a moment. My question is what exactly is the overloaded constructor supposed to do and am I supposed to create the innards like I would a getter/setter? I posted my definitions below with the exception of the overloaded constructor because I'm not sure how to write it:

Contributor::Contributor()
{
	Donation=0.0;
	Sex=none;
	IDKey=0;
}

Contributor::Contributor (string Name, double Donation, gender sex, int IDKey)
{

}

		
Contributor::Contributor (const Contributor& InContributor)
{
	Name=InContributor.Name;
	Donation=InContributor.Donation;
	Sex=InContributor.Sex;
	IDKey=InContributor.IDKey;
}

Recommended Answers

All 7 Replies

"Overloading" (at least in C++) means that you have more than one function with the same name. In this case, you have three different constructors, but they all have the same name.

The difference is the number and type of arguments it accepts.
The first takes no arguments, so it is the default constructor.
The second takes a list of values to initialize the class, so it is an initializing constructor.
The last takes a reference to another object of the same type (or 'class'), and copies its values, so it is a copy constructor.

In the body of the default constructor, you gave all your object's variable fields default values.
In the body of the initializing constructor, use the arguments to assign values to your object's variable fields.

Keep in mind that since the argument and the field names are identical, you must use this to refer to the object's fields: this->Donation = Donation; Hope this helps.

Okay, I get the whole overloading thing, and I set it up with your suggestion, and here is what I have now.....it compiles fine, but does it look right? Also, not sure how to set up the bool at the end for overloading that operator.

#include "Contributor class.h"


Contributor::Contributor()
{
	Donation=0.0;
	Sex=none;
	IDKey=0;
}

Contributor::Contributor (string Name, double Donation, gender Sex, int IDKey)
{
	this->Name=Name;
	this->Donation=Donation;
	this->Sex=Sex;
	this->IDKey=IDKey;
}

		
Contributor::Contributor (const Contributor& InContributor)
{
	Name=InContributor.Name;
	Donation=InContributor.Donation;
	Sex=InContributor.Sex;
	IDKey=InContributor.IDKey;
}

Contributor::~Contributor()
{
	cout<< "I have cleaned up yer mess" << endl;
}

ostream& operator << (ostream& out, Contributor& InContributor)
{
	out << "The Contributor's information is: " << endl << endl;
	out << " Name: " << InContributor.Name << endl;
	out << " Donation: " << InContributor.Donation << endl;
	out << " Sex: ";
		switch (InContributor.Sex)
		{
			case male: out << " Male "; break;
			case female: out << " Female "; break;
			default: out << " No information available ";
		}
		out << endl;
		return out;

	out << " IDKey: " << InContributor.IDKey << endl;
}

istream& operator >> (istream& in, Contributor& InContributor)
{
	int sel=0;
	in.clear();
	in.ignore(in.rdbuf()->in_avail(),'\n');
	
	cout << "Please enter the name of the contributor: "<<endl;
	getline (in, InContributor.Name);

	cout << "Please enter the amount of the donation from the contributor: "<<endl;
	in >> InContributor.Donation;

	cout << "Please enter number of the sex of the contributor: "<<endl << endl;
		cout << "1 = The Contributor is male"<<endl;
		cout << "2 = The Contributor is female "<<endl;
	in >> sel;

	switch (sel)
	{
		case 1: InContributor.Sex = male; break;
		case 2: InContributor.Sex = female; break;
		default: InContributor.Sex = none; break;
	}

	cout << "Please enter the ID Key of the contributor: "<<endl;
	in >> InContributor.IDKey;

	return in;
}

Contributor& Contributor::operator = (const Contributor& rhs)
{
	
}
		
bool Contributor::operator <  (const Contributor& rhs)
{

}

Oh, I should probably give my header also since I've gotten this far into it.

#pragma once
#include <string>
#include <iostream>
using namespace std;

enum gender {none,male,female};

class Contributor
{
	public:
		Contributor();
		Contributor (string Name, double Donation =0.0, gender sex=none, int IDKey=0);
		Contributor (const Contributor& InContributor);
		~Contributor();
		friend ostream& operator << (ostream& out, Contributor& InContributor);
		friend istream& operator >> (istream& in, Contributor& InContributor);
		Contributor& operator = (const Contributor& rhs);
		bool operator <  (const Contributor& rhs);
		bool operator >  (const Contributor& rhs);
		bool operator == (const Contributor& rhs);
		bool operator != (const Contributor& rhs);

	private:
		string Name;
		double Donation;
		int IDKey;
		gender Sex;
};

Just a quick glance it looks OK.

The insertion operator prototype should list its second argument as const: ostream& operator << (ostream& out, const Contributor& InContributor) What makes one Contributor less than another? Answer that and you can write the contents of your < operator.

Hope this helps.

[edit] Oh yeah, your assignment operator's insides should look very much like your copy constructor's insides.

Okay folks,

A new day, a new problem. After taking Duoas' advice, I am still having problems.

First problem: I am still screwing up the overloaded operator function near the end of the cpp file, I can't seem to find the right variable or syntax for making it work.

Second problem: I now have a new set of errors and I'm not sure where they come from. In the ostream statement, it's giving me the "cannot access private data members. See declaration of Contributor". While I understand what that means, it was compiling fine until I incorporated the recommended changes. I know, either it was wrong to begin with and this just uncovered it, or I did something wrong when I put in the recommended changes. So I commented out the changes, and the errors are still there. What did I do wrong and how do I fix it?

//header file
#pragma once
#include <string>
#include <iostream>
using namespace std;

enum gender {none,male,female};

class Contributor
{
	public:
		Contributor();
		Contributor (string Name, double Donation =0.0, gender sex=none, int IDKey=0);
		Contributor (const Contributor& CCContributor);
		~Contributor();
		friend ostream& operator << (ostream& out, Contributor& InContributor);
		friend istream& operator >> (istream& in, Contributor& InContributor);
		Contributor& operator = (const Contributor& rhs);
		bool operator <  (const Contributor& rhs)const;
		bool operator >  (const Contributor& rhs)const;
		bool operator == (const Contributor& rhs)const;
		bool operator != (const Contributor& rhs)const;

	private:
		string Name;
		double Donation;
		int IDKey;
		gender Sex;
};
//the cpp file
#include "Contributor class.h"


Contributor::Contributor()
{
	Donation=0.0;
	Sex=none;
	IDKey=0;
}

Contributor::Contributor (string Name, double Donation, gender Sex, int IDKey)
{
	this->Name=Name;
	this->Donation=Donation;
	this->Sex=Sex;
	this->IDKey=IDKey;
}

		
Contributor::Contributor (const Contributor& CCContributor)
{
	Name=CCContributor.Name;
	Donation=CCContributor.Donation;
	Sex=CCContributor.Sex;
	IDKey=CCContributor.IDKey;
}

Contributor::~Contributor()
{
	cout<< "I have cleaned up yer mess" << endl;
}

ostream& operator << (ostream& out, const Contributor& InContributor)
{
	out << "The Contributor's information is: " << endl << endl;
	out << " Name: " << InContributor.Name << endl;
	out << " Donation: " << InContributor.Donation << endl;
	out << " Sex: ";
		switch (InContributor.Sex)
		{
			case male: out << " Male "; break;
			case female: out << " Female "; break;
			default: out << " No information available ";
		}
		out << endl;
		return out;

	out << " IDKey: " << InContributor.IDKey << endl;
}

istream& operator >> (istream& in, Contributor& InContributor)
{
	int sel=0;
	in.clear();
	in.ignore(in.rdbuf()->in_avail(),'\n');
	
	cout << "Please enter the name of the contributor: "<<endl;
	getline (in, InContributor.Name);

	cout << "Please enter the amount of the donation from the contributor: "<<endl;
	in >> InContributor.Donation;

	cout << "Please enter number of the sex of the contributor: "<<endl << endl;
		cout << "1 = The Contributor is male"<<endl;
		cout << "2 = The Contributor is female "<<endl;
	in >> sel;

	switch (sel)
	{
		case 1: InContributor.Sex = male; break;
		case 2: InContributor.Sex = female; break;
		default: InContributor.Sex = none; break;
	}

	cout << "Please enter the ID Key of the contributor: "<<endl;
	in >> InContributor.IDKey;

	return in;
}

Contributor& Contributor::operator = (const Contributor& rhs)
{
	this->Name=Name;
	this->Donation=Donation;
	this->Sex=Sex;
	this->IDKey=IDKey;
}
		
bool Contributor::operator <  (const Contributor& rhs)
{
	//if (this->Contributor() < rhs)            //these are obviously wrong
	//	return true;
	//else
	//	return false;
}

It was too late to edit my post, but now that the question is different, should this be a different thread?

I don't know if it's solved, but I got it to compile so if I have any other questions, I will post under a different thread. Thanks to those that had a look and Duoas.

When you overload stream operators such that they take two arguments, you have to declare them friend or they, like any other foreign object, will not be allowed to tinker with the object's internals.

class foo
  {
  private:
    int i;
    ...
  public:
    friend ostream& operator << ( ostream&, const foo& );
  };

ostream& operator << ( ostream& outs, const foo& f )
  {
  ostream << f.i << endl;  // or whatever
  }

Sorry about that. I should have noticed and warned you about it.
:$

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.