Solved

# C ++ structure  and bool isEqual

Posted on 2006-05-04
860 Views
I build a cartype structure with four elements that are typically different between car types. These four elements are the color, how many doors it has, how many passengers it can carry, and whether it is a Toyota or not. My question is is there a diffefrence method to use rather bool isEqual with  else if ...

#include <iostream>
#include <string>

using namespace std;

struct cartype
{
bool bolToyota; // is the car a Toyota
string strColor; // what color the car is
int intDoors; // how many doors the car has
int intPassengers; // how many passengers the car can carry
};

bool isEqual(cartype, cartype); // prototype of compare function

int main(void)
{
cartype CarA;
cartype CarB;

CarA.bolToyota = true;
CarB.bolToyota = false;
CarA.intDoors = 4;
CarB.intDoors = 2;
CarA.intPassengers = 4;
CarB.intPassengers = 5;
CarA.strColor = "Red";
CarB.strColor = "Blue";

if (! isEqual(CarA, CarB))
{
cout << "The cars are different" << endl;
}
else
{
cout << "The cars are the same" << endl;
}

return 0;
}

bool isEqual(cartype c1, cartype c2)
{
bool b = true ; // default check is true
// check each structure type, exit on first difference

if (c1.intDoors != c2.intDoors)
{
b = false;
}
else if (c1.bolToyota != c2.bolToyota)
{
b = false;
}
else if (c1.intPassengers != c2.intPassengers)
{
b = false;
}
else if ( c1.strColor.compare(c2.strColor) != 0)
{
b = false;
}
return 0;
}
0
Question by:mhho

LVL 11

Expert Comment

Hi,
What do you mean by "diffefrence method"?
0

LVL 86

Assisted Solution

You could just use

bool isEqual(cartype c1, cartype c2)
{
bool b = true ; // default check is true
// check each structure type, exit on first difference

if (c1.intDoors != c2.intDoors)
{
return false;
}
if (c1.bolToyota != c2.bolToyota)
{
return false;
}
if (c1.intPassengers != c2.intPassengers)
{
return false;
}
if ( c1.strColor.compare(c2.strColor) != 0)
{
return false;
}
return true;
}
0

LVL 17

Expert Comment

You can define operator==() and operator!=().

e.g.
--------8<--------
bool operator==(const cartype& lhs,const cartype& rhs)
{
return isEqual(lhs,rhs);
}

bool operator!=(const cartype& lhs,const cartype& rhs)
{
return !operator==(lhs,rhs);
}
--------8<--------

That way you can compare cars thus:

if (cara == carb) { /* */ }
if (cara != carb) { /* */ }

Note the use of const reference, which would be good style for your isEqual function too:

bool isEqual(const cartype& c1, const cartype& c2) {
// ...
}
0

Author Comment

Note the use of const reference, which would be good style for your isEqual function too:
#include <iostream>
#include <string>

using namespace std;

struct cartype
{
bool bolSportcar; //  the car is sport car
string strColor; // what color the car is
int intDoors; // how many doors
int intPassengers; // how many passengers
};
bool isEqual(const cartype& c1, const cartype& c2); // prototype of compare function

int main(void)
{
cartype CarA;
cartype CarB;

CarA.bolSportcar = true;
CarB.bolSportcar = false;
CarA.intDoors = 2;
CarB.intDoors = 4;
CarA.intPassengers = 2;
CarB.intPassengers = 5;
CarA.strColor = "Red";
CarB.strColor = "Blue";

if (! isEqual(CarA, CarB))
{
cout << "It not the same car" << endl;
}
else
{
cout << "The cars are the same" << endl;
}

return 0;
}

bool operator==(const cartype& lhs,const cartype& rhs)
{
return isEqual(lhs,rhs);
}

0

LVL 17

Expert Comment

This compiles OK:
--------8<--------
#include <iostream>
#include <string>

using namespace std;

struct cartype
{
bool bolToyota; // is the car a Toyota
string strColor; // what color the car is
int intDoors; // how many doors the car has
int intPassengers; // how many passengers the car can carry
};

bool isEqual(cartype, cartype); // prototype of compare function

inline bool operator==(const cartype& lhs,const cartype& rhs)
{
return isEqual(lhs,rhs);
}

inline bool operator!=(const cartype& lhs,const cartype& rhs)
{
return !operator==(lhs,rhs);
}

int main(void)
{
cartype CarA;
cartype CarB;

CarA.bolToyota = true;
CarB.bolToyota = false;
CarA.intDoors = 4;
CarB.intDoors = 2;
CarA.intPassengers = 4;
CarB.intPassengers = 5;
CarA.strColor = "Red";
CarB.strColor = "Blue";

if (CarA != CarB)
{
cout << "The cars are different" << endl;
}
else
{
cout << "The cars are the same" << endl;
}

return 0;
}

bool isEqual(cartype c1, cartype c2)
{
bool b = true ; // default check is true
// check each structure type, exit on first difference

if (c1.intDoors != c2.intDoors)
{
b = false;
}
else if (c1.bolToyota != c2.bolToyota)
{
b = false;
}
else if (c1.intPassengers != c2.intPassengers)
{
b = false;
}
else if ( c1.strColor.compare(c2.strColor) != 0)
{
b = false;
}
return 0;
}
--------8<--------
0

LVL 17

Accepted Solution

...but better with

bool isEqual(const cartype&, const cartype&);

--------8<--------
#include <iostream>
#include <string>

using namespace std;

struct cartype
{
bool bolToyota; // is the car a Toyota
string strColor; // what color the car is
int intDoors; // how many doors the car has
int intPassengers; // how many passengers the car can carry
};

bool isEqual(const cartype&, const cartype&); // prototype of compare function

inline bool operator==(const cartype& lhs,const cartype& rhs)
{
return isEqual(lhs,rhs);
}

inline bool operator!=(const cartype& lhs,const cartype& rhs)
{
return !operator==(lhs,rhs);
}

int main(void)
{
cartype CarA;
cartype CarB;

CarA.bolToyota = true;
CarB.bolToyota = false;
CarA.intDoors = 4;
CarB.intDoors = 2;
CarA.intPassengers = 4;
CarB.intPassengers = 5;
CarA.strColor = "Red";
CarB.strColor = "Blue";

if (CarA != CarB)
{
cout << "The cars are different" << endl;
}
else
{
cout << "The cars are the same" << endl;
}

return 0;
}

bool isEqual(const cartype& c1, const cartype& c2)
{
bool b = true ; // default check is true
// check each structure type, exit on first difference

if (c1.intDoors != c2.intDoors)
{
b = false;
}
else if (c1.bolToyota != c2.bolToyota)
{
b = false;
}
else if (c1.intPassengers != c2.intPassengers)
{
b = false;
}
else if ( c1.strColor.compare(c2.strColor) != 0)
{
b = false;
}
return 0;
}
--------8<--------
0

Author Comment

is there a way you could  reduce he number of return statement inside the function to one?
0

LVL 17

Expert Comment

You'll get short-circuit evaluation with...

bool isEqual(const cartype& c1, const cartype& c2)
{
return c1.intDoors == c2.intDoors
&& c1.bolToyota == c2.bolToyota
&& c1.intPassengers == c2.intPassengers
&& c1.strColor.compare(c2.strColor) == 0
;
}

.. i.e. at run time it immediately returns false if doors don't match, without checking Toyota etc.
0

Author Comment

thanks !
0

## Featured Post

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.