Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 27th, 2006, 1:13 PM   #1
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
A class with operator overloading

I'm new to c++. I recently read a book on it. After reading the book i just wanted to get some practical experience. Now i'm trying to get "operator overloading" to work. I have a Car class and all i want to do is increment the Honda's speed by 1, (from the Car class). Doing "++Honda" (Honda is the object). However, Instead of increamenting by 1 it increments by approx ONE HUNDRED MILLION. What am i doing wrong??

#include <iostream>
#include <stdlib.h>

class Car
{
public:
    Car(); 
    ~Car();
    int GetSpeed() {return *itsSpeed;}
    void SetSpeed (int speed) { *itsSpeed = speed;}
    void Speeding();
    void Speed() { *itsSpeed = *itsSpeed + 1;}
    const Car& operator++ ();
private:
    int * itsSpeed;
    int * itsFuel;
};
    
Car::Car()
{
    itsSpeed = new int(10);
    itsFuel = new int(20);
}

Car::~Car()
{
    delete itsSpeed;
    delete itsFuel;
}

const Car& Car::operator++()
{
    * ++itsSpeed;
}

void Car::Speeding()
{
    if (* itsSpeed > (30))
    {
        std::cout << "You are over the speed limit, please slow down. Your going " << *itsSpeed << " KM/H \n";
    }
    else 
    {
        std::cout << "Your under the speed limit. Your at " << *itsSpeed << " KM/H at the moment.\n"; 
    }
}
int main()
{
    //int CarSpeed;
  
    Car * Honda = new Car;
    Honda->Speeding();    
    Honda->SetSpeed(45);
    Honda->Speeding();
    Honda->Speed();
    Honda->Speeding();
    ++Honda;
    Honda->Speeding();
    ++Honda;
    Honda->Speeding();
           
    delete Honda;
    system("Pause");
    return 0;
}
Eric the Red is offline   Reply With Quote
Old Feb 27th, 2006, 1:33 PM   #2
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 3 Kaja Fumei is on a distinguished road
With unary operators, they are evaluated right to left, not left to right, so in your code, the ++ operation is done to itsSpeed before *. This causes the pointer to be incremented, rather than the value stored at the pointer's address, which is what you meant to do.

Simple fix:
++*itsSpeed;
Kaja Fumei is offline   Reply With Quote
Old Feb 27th, 2006, 1:43 PM   #3
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Thanks for your help. Unfortunately, this doesn't seem to fix the problem. I tried

const Car& Car::operator++()
{
    ++ (* itsSpeed); 
}

// and 

const Car& Car::operator++()
{
    ++ * itsSpeed;
}

The result, still give me a value in the millions when incremented. Just try running the program and you'll see.
Eric the Red is offline   Reply With Quote
Old Feb 27th, 2006, 1:56 PM   #4
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 288
Rep Power: 4 Klarre is on a distinguished road
You also have to change on the Honda object.
Klarre is offline   Reply With Quote
Old Feb 27th, 2006, 2:02 PM   #5
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 3 Kaja Fumei is on a distinguished road
I see the problem now, I wasn't paying too much attention before. Overloaded operators can only be used on the the object "value". If you declare a pointer to an object, any operators you do on that pointer will be done to the pointer, not to the data. So you must declare the car on the stack or if you have a pointer to a Car (call the pointer variable Honda), you can use ++*Honda.

Method one:
    Car Honda;
    Honda.Speeding();    
    Honda.SetSpeed(45);
    Honda.Speeding();
    Honda.Speed();
    Honda.Speeding();
    ++Honda;
    Honda.Speeding();
    ++Honda;
    Honda.Speeding();

Method 2:
    Car * Honda2 = new Car;
    Honda2->Speeding();    
    Honda2->SetSpeed(45);
    Honda2->Speeding();
    Honda2->Speed();
    Honda2->Speeding();
    ++*Honda2;
    Honda2->Speeding();
    ++*Honda2;
    Honda2->Speeding();
    
    delete Honda2;

I ran your code on both of these and it worked fine.
Kaja Fumei is offline   Reply With Quote
Old Feb 27th, 2006, 2:02 PM   #6
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Yeah ur right.. and wow can these pointers can get tedius.

I'm using pointers in this class to get practice with it. However, is there really a point to be using pointers in this program?
Eric the Red is offline   Reply With Quote
Old Feb 27th, 2006, 2:05 PM   #7
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 3 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by Eric the Red
However, is there really a point to be using pointers in this program?
Well, there is always a point, but in this case no need. Since you exactly know how much memory you are using (basically a car object, and two integers) there is indeed no need for them.
It is good to practice them though, because practice makes perfect (it is said) .
Polyphemus_ is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:41 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC