User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,918 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,204 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 254 | Replies: 7 | Solved
Reply
Join Date: Dec 2007
Posts: 218
Reputation: henpecked1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Constructor question

  #1  
Jul 19th, 2008
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;
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,840
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 189
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Constructor question

  #2  
Jul 19th, 2008
"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.
Reply With Quote  
Join Date: Dec 2007
Posts: 218
Reputation: henpecked1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Constructor question

  #3  
Jul 19th, 2008
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;
};
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,840
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 189
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Constructor question

  #4  
Jul 19th, 2008
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.
Last edited by Duoas : Jul 19th, 2008 at 7:05 pm.
Reply With Quote  
Join Date: Dec 2007
Posts: 218
Reputation: henpecked1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Constructor question

  #5  
Jul 20th, 2008
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;
}
Reply With Quote  
Join Date: Dec 2007
Posts: 218
Reputation: henpecked1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Constructor question

  #6  
Jul 20th, 2008
It was too late to edit my post, but now that the question is different, should this be a different thread?
Reply With Quote  
Join Date: Dec 2007
Posts: 218
Reputation: henpecked1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Constructor question

  #7  
Jul 20th, 2008
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.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,840
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 189
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Constructor question

  #8  
Jul 20th, 2008
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.
  1. class foo
  2. {
  3. private:
  4. int i;
  5. ...
  6. public:
  7. friend ostream& operator << ( ostream&, const foo& );
  8. };
  9.  
  10. ostream& operator << ( ostream& outs, const foo& f )
  11. {
  12. ostream << f.i << endl; // or whatever
  13. }
Sorry about that. I should have noticed and warned you about it.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:08 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC