Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,211 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,273 people online right now. Registration is fast and FREE... Join Now!




Operator Overloading

 
Reply to this topicStart new topic

Operator Overloading

Outtey22
1 Jun, 2008 - 07:24 AM
Post #1

New D.I.C Head
*

Joined: 23 Sep, 2007
Posts: 27



Thanked: 1 times
My Contributions
Hi guys!

I'm having several issues with my code. My first problem is that I have no clue on how to use operator overloading. For this assignment, I'm supposed to overload+ so that it will give me the total score and average time. Any input you can give me about operator overloading would be awesome. I've looked at the tutorial on it but I'm still very confused.

My second problem is that I'm not sure on how to add the seconds up in a way that will increment the minutes when it goes over 60.

The third problem is that when I create an object of the RacingData class, the values are all garbage. I'm sure it has something to do with my constructor but I'm not sure what.

CODE

#include <iostream>  
#include <string>  
  
using namespace std;
  
int dpAvg;
int jgAvg;
int dpTotal;
int jgTotal;

class RacingData
{
      public:
             RacingData(string,int,int,int); //constructor with parameters
             RacingData(); // default constructor
             void printRacingData(); //displays race results
             int score, minutes, seconds;
             string racerName;
};

RacingData::RacingData(string racerName, int score, int minutes, int seconds)
{
                              racerName = racerName;
                              score = score;
                              minutes = minutes;
                              seconds = seconds;
}

RacingData::RacingData()
{
                        racerName = " ";
                        score = 0;
                        minutes = 0;
                        seconds = 0;
}

void RacingData::printRacingData()
{
     cout << racerName << "      " << score << "   " << minutes << ":" << seconds << "\n";
}

int main()
{
    RacingData dpRace1("Danica Patrick", 185, 11, 20);
    RacingData dpRace2("Danica Patrick", 103, 11, 30);
    RacingData dpRace3("Danica Patrick", 73, 12 ,40);
    RacingData jgRace1("Jeff Gordon", 155, 10, 10);
    RacingData jgRace2("Jeff Gordon", 127, 11, 15);
    RacingData jgRace3("Jeff Gordon", 34, 12, 35);
    cout << "Race Results\n";
    dpRace1.printRacingData();
    dpRace2.printRacingData();
    dpRace3.printRacingData();
    jgRace1.printRacingData();
    jgRace2.printRacingData();
    jgRace3.printRacingData();
    dpTotal = dpRace1.score + dpRace2.score + dpRace3.score;
    jgTotal = jgRace1.score + jgRace2.score + jgRace3.score;
    cout << "Total Scores:\n" << "Danica Patrick : " << dpTotal << "\n" << "Jeff Gordon : " << jgTotal << "\n";
    
    
    system("PAUSE");  
    return 0;  
}

User is offlineProfile CardPM
+Quote Post

joske
RE: Operator Overloading
1 Jun, 2008 - 08:19 AM
Post #2

D.I.C Head
**

Joined: 4 Sep, 2007
Posts: 158



Thanked: 12 times
My Contributions
To solve the garbage problem: give the parameters of the constructor other names than the data itself, for example:
cpp
RacingData::RacingData(string new_racerName, int new_score, int new_minutes, int new_seconds)
{
racerName = new_racerName;
score = new_score;
minutes = new_minutes;
seconds = new_seconds;
}


To solve the problems with the minutes/seconds: you can do a check:
cpp
if (seconds >= 60)
{
minutes += seconds / 60;
seconds = seconds % 60;
}


For the operator overloading, I have added it to your class. There the adjusted code:
cpp
#include <iostream>  
#include <string>

using namespace std;

int dpAvg;
int jgAvg;
int dpTotal;
int jgTotal;

class RacingData
{
public:
RacingData(string,int,int,int); //constructor with parameters
RacingData(); // default constructor
RacingData operator+(const RacingData& a); // overloaded operator +
void printRacingData(); //displays race results
int score, minutes, seconds;
string racerName;
};

RacingData::RacingData(string new_racerName, int new_score, int new_minutes, int new_seconds)
{
racerName = new_racerName;
score = new_score;
minutes = new_minutes;
seconds = new_seconds;
}

RacingData::RacingData()
{
racerName = " ";
score = 0;
minutes = 0;
seconds = 0;
}

void RacingData::printRacingData()
{
cout << racerName << " " << score << " " << minutes << ":" << seconds << "\n";
}

/**
Operator + for the class RacingData
*/
RacingData RacingData::operator+(const RacingData& a)
{
RacingData res = RacingData();

// copy name but only if both names are identical
if (racerName == a.racerName)
{
res.racerName = racerName;
}

// add up all data elements
res.score = score + a.score;
res.minutes = minutes + a.minutes;
res.seconds = seconds + a.seconds;

// reduce seconds to below 60
if (res.seconds >= 60)
{
res.minutes += res.seconds / 60;
res.seconds = res.seconds % 60;
}

return res;
}


int main()
{
RacingData dpRace1("Danica Patrick", 185, 11, 20);
RacingData dpRace2("Danica Patrick", 103, 11, 30);
RacingData dpRace3("Danica Patrick", 73, 12 ,40);
RacingData jgRace1("Jeff Gordon", 155, 10, 10);
RacingData jgRace2("Jeff Gordon", 127, 11, 15);
RacingData jgRace3("Jeff Gordon", 34, 12, 35);

cout << "Race Results\n";
dpRace1.printRacingData();
dpRace2.printRacingData();
dpRace3.printRacingData();
jgRace1.printRacingData();
jgRace2.printRacingData();
jgRace3.printRacingData();

cout << "Total Scores:\n";
RacingData dpTotal = dpRace1 + dpRace2 + dpRace3;
RacingData jgTotal = jgRace1 + jgRace2 + jgRace3;
dpTotal.printRacingData();
jgTotal.printRacingData();

system("PAUSE");
return 0;
}


This post has been edited by joske: 1 Jun, 2008 - 11:41 AM
User is offlineProfile CardPM
+Quote Post

Outtey22
RE: Operator Overloading
1 Jun, 2008 - 08:28 AM
Post #3

New D.I.C Head
*

Joined: 23 Sep, 2007
Posts: 27



Thanked: 1 times
My Contributions
So in order to make the time an average as opposed to the total, I would need to setup a counter and then make it divide the total by the counter correct?
User is offlineProfile CardPM
+Quote Post

joske
RE: Operator Overloading
1 Jun, 2008 - 10:35 AM
Post #4

D.I.C Head
**

Joined: 4 Sep, 2007
Posts: 158



Thanked: 12 times
My Contributions
To calculate the average, the easiest way is to first calculate the time in seconds, then divide it by 3, and then extract the number of minutes from it:
CODE
int seconds_sum = minutes * 60 + seconds;
int seconds_avg = seconds_sum / 3;
minutes_avg = seconds_avg / 60;  
seconds_avg = seconds_avg % 60;  

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Operator Overloading
1 Jun, 2008 - 11:16 AM
Post #5

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,248



Thanked: 221 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
I have one slight comment to make about the operator overloading provided by joske, do notice that is not actually part of the class. You have defined the operator as a general function rather than making it part of the class.

What this is saying is that any time the addition operator is encountered take the two operands, add them together and return a result. While this is right, it is not part of the class RacingData which is taking an object, calling its operator + method and passing in the second operand as its parameter.

If you are doing this as part of an assignment the teacher will make a comment about this. What they will be looking for is a function like...

cpp

RacingData operator+( RacingData &other );


... declared in the class itself and then a method defined with the signature...

cpp

RacingData RacingData::operator+( RacingData &other ) {

}


Where you will then take the passed in parameter and add its members to the current instance variables and return a brand new RacingData object.

It is a subtle difference but an important one. This makes the operator part of the object instead of a separate defined function. You would define it this way to take advantage of encapsulation and if you had the class defined in a separate file.

Hope that makes sense. Other than that, fantastic answer joske, I gave you a "Thanks that was helpful" click. smile.gif
User is online!Profile CardPM
+Quote Post

joske
RE: Operator Overloading
1 Jun, 2008 - 11:33 AM
Post #6

D.I.C Head
**

Joined: 4 Sep, 2007
Posts: 158



Thanked: 12 times
My Contributions
good point Martyr2, you're right!

I have adjusted my code in post #2.

This post has been edited by joske: 1 Jun, 2008 - 11:42 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 01:15PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month