Help, Overloading Operators

Help, Overloading Operators

Post by Neccross on Thu Oct 07, 2010 10:03 pm
([msg=47210]see Help, Overloading Operators[/msg])

I was having trouble adding multiple parameters, for example,
Code: Select all
int main()
{
    RacingData car1, car2, car3, car4, car5, car6;
    RacingData danikaTotalData, jeffTotalData, winner;
    car1.setData("Danika Patrick", 185, 11, 20);
    car2.setData("Danika Patrick", 103, 11, 30);
    car3.setData("Danika Patrick", 73, 12, 40);
    car4.setData("Jeff Gordon", 155, 10, 10);
    car5.setData("Jeff Gordon", 127, 11, 15);
    car6.setData("Jeff Gordon", 34, 12, 35);

    // Add the data

    danikaTotalData = car1 + car2 + car3;
    jeffTotalData = car4 + car5 + car6;


Creating a class called RacingData for the overloaded operator I ended up with something like,
Code: Select all
RacingData RacingData::operator+(const RacingData& RaceSum)const
{
    string theName;
    int otherScore, otherMinutes, otherSeconds;
    RacingData tempVal;

    RaceSum.getData(theName, otherScore, otherMinutes, otherSeconds);
    tempVal.setData(name, score + otherScore, minutes + otherMinutes, seconds + otherSeconds);
    tempVal.getData(theName, otherScore, otherMinutes, otherSeconds);
    if (otherSeconds > 59)
    {
        otherMinutes= otherMinutes + (otherSeconds /60);
        otherSeconds = otherSeconds % 60;
        tempVal.setData(name, otherScore, otherMinutes, otherSeconds);
    }
    return tempVal;


I keep getting numbers that are incorrect and Im assuming it has to do with the way I wrote the overloaded operator, but its my first time doing it so I got confused? Can i get some insight on this.
Neccross
New User
New User
 
Posts: 21
Joined: Tue Jun 03, 2008 3:34 pm
Location: California
Blog: View Blog (0)


Re: Help, Overloading Operators

Post by fashizzlepop on Thu Oct 07, 2010 11:52 pm
([msg=47213]see Re: Help, Overloading Operators[/msg])

Is this C#?

This is a wild guess, but I would think you need...
Code: Select all
    // Add the data

    danikaTotalData = car1.setData + car2.setData + car3.setData;
    jeffTotalData = car4.setData + car5.setData + car6.setData;


Again, wild guess.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2303
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: Help, Overloading Operators

Post by Neccross on Fri Oct 08, 2010 12:19 am
([msg=47219]see Re: Help, Overloading Operators[/msg])

fashizzlepop wrote:Is this C#?

This is a wild guess, but I would think you need...
Code: Select all
    // Add the data

    danikaTotalData = car1.setData + car2.setData + car3.setData;
    jeffTotalData = car4.setData + car5.setData + car6.setData;


Again, wild guess.


Umm its C++ the setData goes in the overload operator function since I'm using classes, which are separate from the main code. Otherwise, it wont know what car1 + car2 means since there is no function explaining it, thanks though. (Well at least from what i understood, feel free to correct me if I'm wrong.)
Neccross
New User
New User
 
Posts: 21
Joined: Tue Jun 03, 2008 3:34 pm
Location: California
Blog: View Blog (0)


Re: Help, Overloading Operators

Post by fashizzlepop on Fri Oct 08, 2010 12:28 am
([msg=47223]see Re: Help, Overloading Operators[/msg])

Like I said, it was a wild guess. It' looked very clean and I usually only see that in C# around here.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2303
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: Help, Overloading Operators

Post by Assassian360 on Fri Oct 08, 2010 1:19 am
([msg=47226]see Re: Help, Overloading Operators[/msg])

fashizzlepop wrote:Is this C#?

This is a wild guess, but I would think you need...
Code: Select all
    // Add the data

    danikaTotalData = car1.setData + car2.setData + car3.setData;
    jeffTotalData = car4.setData + car5.setData + car6.setData;


Again, wild guess.


Firstly, it would make more sense to "getData" rather than have "setData" there fashizzlepop.
I couldn't see from a glance what was actually wrong, but you are doing it a very convoluted way with getting and setting the data. Normally for operator overloading like this you can use what are called "friend"s. The friend keyword will make it that the method itself is allowed to access the private instance variables of the object. If you look into the friend keyword, you will be able to simplify your code a bit by being able to have:

tempVal.score = score += RaceSum.score;
tempVal.minutes = minutes + RaceSum.minutes;
tempVal.seconds = seconds + RaceSum.minutes;


Better than this though would be to use two parameters for the addition operator overloading though. So for example:

Code: Select all
class RacingData{
  // all your stuff
public:
      friend RacingData operator=(const RacingData& d1, const RacingData& d2);
}


Then you can have (leaving out RacingData:: in the method below is deliberate!):

Code: Select all
RacingData operator+(const RacingData& d1, const RacingData& d2)
{
    RacingData result;

    result.name = d1.name;
    result.score = d1.score + d2.score;
    result.minutes = d1.minutes + d2.minutes;
    result.seconds = d1.seconds + d2.seconds;
   if (result.seconds > 59)
    {
        result.minutes= result.minutes + (result.seconds /60);
        result.seconds = result.seconds % 60;
    }
    return result;
}


As a side note, I would recommend using either individual set/get methods or method overloading, if you are wanting to get/set a single or two variables, your get and set of the entire set of data every time was overkill.

Hope this helps. Let us know if your code still isn't working.
But if it isn't please provide some examples of what it is doing.
Assassian360
Poster
Poster
 
Posts: 135
Joined: Sat Jun 26, 2010 1:37 am
Blog: View Blog (0)


Re: Help, Overloading Operators

Post by Neccross on Fri Oct 08, 2010 1:59 am
([msg=47227]see Re: Help, Overloading Operators[/msg])

Assassian360,

Hey, thanks, that worked perfectly for me. I guess I was aiming more towards not having to use friend, or to be honest I didn't think about it since I'm not sure if we had to use friend or not. Hmm... I get the correct values and it's a lot cleaner but I'm going to have to look more into it. The only example I had to create my own operator overload was this...

Main Code
Code: Select all
// OpOverloadDemo.cpp : Defines the entry point for the console application.
#include "Simple.h"
#include <iostream>

using namespace std;

int main()
{
   
    Simple one(5);
    Simple two(7);
    Simple three;
   
    three = one + two;
   
    cout << "Instance one - the print method produces: " ;
    one.Print();
    cout << endl;
    cout << "Instance two - the print method produces: " ;
    two.Print();
    cout << endl;
    cout << "Instance three - the print method produces: " ;
    three.Print();
    cout << endl;


    system("pause");
    return 0;
}


Header File
Code: Select all
#pragma once

class Simple
{
private:
    int simpleInt;

public:
    Simple(void);
    Simple(int theSimpleInt);
    ~Simple(void);
    int getSimpleInt(void) const;
    void setSimpleInt(int theSimpleInt);
    void Print(void)const;
    Simple operator+(const Simple&)const; //Overload the operator +
};


Implementation of Header
Code: Select all
#include "Simple.h"
#include <iostream>

using namespace std;

Simple::Simple(void)
: simpleInt(0)
{
}

Simple::Simple(int theSimpleInt)
{
    simpleInt = theSimpleInt;
}
Simple::~Simple(void)
{
}



int Simple::getSimpleInt(void) const
{
    return simpleInt;
}

void Simple::setSimpleInt(int theSimpleInt)
{
    simpleInt = theSimpleInt;
}

void Simple::Print(void) const
{
    cout << simpleInt;
}

Simple Simple::operator+ (const Simple& theSimple)const
{
    Simple tempSimple;
    tempSimple.simpleInt = simpleInt + theSimple.simpleInt;
    return tempSimple;
}


Is there something closer towards what I was doing? Just for the sake of getting to know the alternative. I'm still trying to play around with the code I posted for this thread. Based on what I saw maybe it does have to do with the private members and not correctly being able to access them, although it wouldn't make sense that I can still display the proper data with the rest of the code with the exception of the operator overload I wrote. Gah, I'm probably missing something, of course assuming I don't use friend. Can I get some more feedback on this?
Neccross
New User
New User
 
Posts: 21
Joined: Tue Jun 03, 2008 3:34 pm
Location: California
Blog: View Blog (0)


Re: Help, Overloading Operators

Post by Assassian360 on Fri Oct 08, 2010 5:48 am
([msg=47233]see Re: Help, Overloading Operators[/msg])

I don't think you can overload the + operator without the friend keyword and have it work. I may be wrong. The examples you will find online demonstrate it with the use of the friend keyword, which indicates generally that it is a good way to go. An example site if you did want to read some more is: http://www.ee.ed.ac.uk/~sasg/Computers/Prog/Cpp/cpp6.html

Code: Select all
int getSimpleInt(void) const;
    void setSimpleInt(int theSimpleInt);


Using things like that are perfectly fine if you don't want to have a proper operator overload. Encapsulation of classes is typically taught to use these types of get and set methods. So you could potentially have something like:

Code: Select all
newobject.setVariable(object1.getVariable() + object2.getVariable());


Or something along those lines. So you could have a method like add(d1, d2) and have it return the result. And inside it have all the setting. What you had before in terms of this was on the right sort of track I think, but the getting/setting of everything in one go was a bit unneeded.
Assassian360
Poster
Poster
 
Posts: 135
Joined: Sat Jun 26, 2010 1:37 am
Blog: View Blog (0)


Re: Help, Overloading Operators

Post by tgoe on Sun Oct 10, 2010 10:48 am
([msg=47305]see Re: Help, Overloading Operators[/msg])

Is this just a homework assignment? This is structured awkwardly IMO to make operator overloading seem awesome. Your "top level" class should be Driver not RacingData which is what I think Assassian360 is getting at.
User avatar
tgoe
Contributor
Contributor
 
Posts: 661
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: Help, Overloading Operators

Post by Neccross on Sun Oct 10, 2010 12:33 pm
([msg=47308]see Re: Help, Overloading Operators[/msg])

tgoe wrote:Is this just a homework assignment? This is structured awkwardly IMO to make operator overloading seem awesome. Your "top level" class should be Driver not RacingData which is what I think Assassian360 is getting at.


Yeah it's an assignment, but its just not working for me. I've tried asking the professor how to go about it, and he told me that for this assignment I don't need use friend, and insists that the code I have is correct so I'm insanely confused at the moment. Everything else works the way it's supposed to except for the operator overload. =/
Neccross
New User
New User
 
Posts: 21
Joined: Tue Jun 03, 2008 3:34 pm
Location: California
Blog: View Blog (0)


Re: Help, Overloading Operators

Post by Assassian360 on Sun Oct 10, 2010 6:43 pm
([msg=47324]see Re: Help, Overloading Operators[/msg])

Neccross wrote:
tgoe wrote:Is this just a homework assignment? This is structured awkwardly IMO to make operator overloading seem awesome. Your "top level" class should be Driver not RacingData which is what I think Assassian360 is getting at.


Yeah it's an assignment, but its just not working for me. I've tried asking the professor how to go about it, and he told me that for this assignment I don't need use friend, and insists that the code I have is correct so I'm insanely confused at the moment. Everything else works the way it's supposed to except for the operator overload. =/


You could try returning to your original operator overloading and try something like:
Code: Select all
result.setVarName(this.getVarName() + other.getVarName());


Where VarName would be the variable you are working with, and other is the other data you are combining with.
Assassian360
Poster
Poster
 
Posts: 135
Joined: Sat Jun 26, 2010 1:37 am
Blog: View Blog (0)


Next

Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests