Overload operator

I need to overload this operator for my class Car..

It should compare each attribute for the object, and cout the things which are the same.

ex.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Clas car {
Public:
Car(int,int,int,int);
Int Engine;
Int maxSpeed;
int HorsePower;
};

car::car(int a, int b, int c)
{
engine = a;
maxSpeed = b;
Horsepower = c;


// in my main I would have ..

car Opel (2,2,2);
car Ferrari (99,99,2);
// and by doing opel == ferarri => it should say which car has the better engine,maxspeed,horsepower..

}


I am a bit lost on how to overload operator, and it would help me alot, if someone could tell how to do it.. I understand the concept of using and why it used, but how someone would do, that ....
Note that in C++, case matters.
In your example, "Class" and "Public" and "Int" are not recognized as keywords.
They must be written as class, public and int respectively.

Overloaded operators are basically functions. See this Wikipedia article for a helpful table:
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

So, to overload the equality operator for the car class, you can write something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// ...

bool operator == (car lhs, car rhs) // left hand side, right hand side
{
    // do the comparisons and "cout" what you want

    // and at the end...
    if (/* cars are equal */)
        return true;

    return false; // if cars are not equal
}

int main()
{
    car lhs(2, 2, 2), rhs(99, 99, 2);

    lhs == rhs; // operator== will be called

    // the above is a shorthand for
    operator==(lhs, rhs);
}


You can also write the operator== as a member function, but in general you shouldn't do that because it requires the lhs to be the class in question (in this case, car) and this can be a limitation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class car
{
    // ...

    bool operator == (car rhs) const
    {
        // lhs is implied and it's the current object
        // otherwise, same as in the previous example
    }
};

int main()
{
    car lhs(2, 2, 2), rhs(99, 99, 2);

    lhs == rhs; // car::operator== will be called

    // the above is a shorthand for
    lhs.operator==(rhs);
}
Last edited on
where and how would you place it .. how would you write it in the class.. i know it is a boolean operation, but how would it be for other operations...
Last edited on
where and how would you place it .. how would you write it in the class.. i know it is a boolean operation, but how would it be for other operations...

I would write it as a non-member overload (as in the first example).

As for other operations... what exactly do you mean?
Did you read the Wikipedia article for other operators besides operator== that you can overload?
i mean like + and - and so on..
i mean like + and - and so on..

Overloading those operators doesn't make much sense for a Car class.

Again I will give you the Wikipedia article, which contains the tables which show clearly how to overload any operator.
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

If you cannot use the information in the Wikipedia article, it may be a sign that you are not ready yet to do this, and must spend more time learning C++.
http://www.cplusplus.com/doc/tutorial/

A hint:

1
2
car operator + (car lhs, car rhs);
car operator - (car lhs, car rhs);

I found the operator overloading guide from StackOverflow to be helpful:
http://stackoverflow.com/questions/4421706
It's generally how you overload an operator i am confused..

Using my cas ex. i could morph 2 cars and get a better car or something like that..
Topic archived. No new replies allowed.