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;
}