In: Computer Science
Below is the code for the isSmaller() member function:
bool Location::isSmaller(const Location & r) const
{
// ORDER OF COMPARISONS ==> 1st country; 2nd state; 3rd city
int a = strcmp(country,r.country); // need
strcmp
// country and r.country are arrays and in C++ the
expression country < r.country will compare address, not
data
// the data must be compared cell by corresponding cell in
the 2 arrays, which takes place in the strcmp()
function
// There are three possible return cases:
// - a negative number, country is alphabetically smaller
than r.country (appears earlier in a Dictionary)
// - 0 value, the two C++ strings are
identical
// - a positive value, the first parameter is
alphabetically larger than the second parameter
if ( a < 0 ) return true;
else if ( a > 0 ) return false;
// at this point in the logic, the countries are the same
int b = strcmp(state,r.state); // need strcmp
if ( b < 0 ) return true;
else if ( b > 0 ) return false;
// at this point in the logic, the countries are the same
// and the states are the same
return ( city < r.city ); // < works for string objects
}
1. In location.h prototype a member function,
isEqual, to check for equality, i.e. all 3
corresponding fields must be equal
2. In location.cpp write the code for the isEqual member
function.
Note: arrays of char are compared using strcmp which string objects
are compared using the normal operators: <, ==, >
3. In lecture4driver.cpp test your isEqual function, once to get
true and once to get false.
Submit as 3 separate files ( no zip files): header file, implementation file, and driver file
1)
location.h
#include <string.h>
#define MAX 20
using namespace std;
class Location{
char country[MAX];
char state[MAX];
string city;
public:
Location(char[],char[],string);
bool isSmaller(const Location &)const;
bool isEqual(const Location &)const;
};
2) location.cpp
#include <iostream>
#include <string.h>
#include "location.h"
using namespace std;
Location::Location(char a[],char b[],string c){
strcpy(country,a);
strcpy(state,b);
city=c;
}
bool Location::isEqual(const Location & r)
const
{
// ORDER OF COMPARISONS ==> 1st country; 2nd state; 3rd
city
int a = strcmp(country,r.country); // need strcmp
// country and r.country are arrays and in C++ the expression
country < r.country will compare address, not data
// the data must be compared cell by corresponding cell in the 2
arrays, which takes place in the strcmp() function
// There are three possible return cases:
// - a negative number, country is alphabetically smaller than
r.country (appears earlier in a Dictionary)
// - 0 value, the two C++ strings are identical
// - a positive value, the first parameter is alphabetically larger
than the second parameter
if ( a < 0 || a > 0) return false; /// countries are not
equal
// at this point in the logic, the countries are the
same
int b = strcmp(state,r.state); // need strcmp
if ( b < 0 || b > 0) return false; /// states are not
equal
// at this point in the logic, the countries are the
same
// and the states are the same
return ( city == r.city ); // < works for string objects if city
name same then returns true
}
3) lecture4driver.cpp
#include <iostream>
#include <string.h>
using namespace std;
#include "location.h"
int main()
{
char a[]="INDIA";
char b[]="GUJARAT";
string c="PATAN";
Location A(a,b,c);
Location B(a,b,"PATAN");
if(B.isEqual(A)){
cout<<"\n\nBoth location A and B are equal ";
}
else{
cout<<"\n\nlocation A and B are NOT equal ";
}
Location C(a,b,"Surat");
Location D(a,b,"Rajkot");
///only cities differ in C and D
if(C.isEqual(D)){
cout<<"\n\nBoth location C and D are equal ";
}
else{
cout<<"\n\nlocation C and D are NOT equal ";
}
///example 3
///here states differ from one another
Location E(a,b,"Surat");
Location F(a,"Goa","Surat");
if(E.isEqual(F)){
cout<<"\n\nBoth location E and F are equal ";
}
else{
cout<<"\n\nlocation E and F are NOT equal \n";
}
return 0;
}