•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 426,514 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 2,115 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: 298 | Replies: 7 | Solved
![]() |
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Rep Power: 1
Solved Threads: 1
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;
}
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation:
Rep Power: 11
Solved Threads: 193
"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:
Hope this helps.
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.
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Rep Power: 1
Solved Threads: 1
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.
Oh, I should probably give my header also since I've gotten this far into it.
#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;
};
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation:
Rep Power: 11
Solved Threads: 193
Just a quick glance it looks OK.
The insertion operator prototype should list its second argument as const:
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.
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.
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Rep Power: 1
Solved Threads: 1
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?
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;
}•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation:
Rep Power: 11
Solved Threads: 193
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.
Sorry about that. I should have noticed and warned you about it.
C++ Syntax (Toggle Plain Text)
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 }
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Constructor Question? (C#)
- Virtual constructor (C)
- copy constructor problem (C++)
- How to call constructor for descendant of TObject ? (C++)
- Yet another initialise question (Java)
- queue program question (C++)
- A little Help (C++)
- Copy constructor in derived class (C)
Other Threads in the C++ Forum
- Previous Thread: Inheritance Hierarchy
- Next Thread: TXT File Loading Prog



Linear Mode