Write a simple function template for predicate function isEqualTo that compares its two arguments of the same type with the equality operator (==) and returns true if they are equal and false if they are not equal. Use this function template in a program that calls isEqualTo on a variety of built-in types and user define types, Complex and Date (need to overload the equality operator (operator==) and extraction operator (operator<<)). Write a program with variety of inputs to test the functionalities of the program.
The following are classes, functions, and private data members needed in this program. You can add and create more classes, functions and private data members for this program.
Date class
There should have three private integer data members, month, day and year for the Date class.
It should have Date, operator<< and operator== functions in the class. The Date class has to do a validation on data.
Complex class
There should have two private double data members, real and imaginary for the Complex class.
It should have Complex, operator<< and operator== functions in the class.
CISP400V7A6.cpp
Implement an isEqualTo template function to test with different types.
The data type and data are list as follow:
Data type
Operant 1
Operant 2
Int
1
1
Int
2
4
Int
-1
1
Int
-1
-1
char
a
a
char
a
c
char
c
a
char
c
c
double
2.2
2.2
double
2.2
2.3
double
-2.2
2.2
double
-2.2
-2.2
Complex
(10, 5)
(10, 5)
Complex
(10, 5)
(10, 54)
Complex
(10,- 5)
(10, 5)
Complex
(-10, -5)
(-10, -5)
string
abcdefg
abcdefg
string
abcdefg
abcdefh
String
-abcdefg
abcdefg
string
-abcdefg
-abcdefg
Date
(2, 31, 2011)
(2, 31, 2011)
Date
(2, 13, 2011)
(2, 14, 2011)
Date
(-2, 13, 2011)
(2, 13, 2011)
Date
(-2, 13, 2011)
(-2, 13, 2011)
Display the data type they are testing in a group and use the isEqualTo function to thes the equality of the operants.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
here is the code:
#include <iostream>
using namespace std;
int main()
{
int a; // integers used for
int b; // testing equality
// test if two ints input by user are equal
cout << "Enter two integer values: ";
cin >> a >> b;
cout << a << " and " << b << " are " << ( isEqualTo( a, b ) ? "equal" : "not equal" ) << '\n';
char c; // chars used for
char d; // testing equality
// test if two chars input by user are equal
cout << "\nEnter two character values: ";
cin >> c >> d;
cout << c << " and " << d << " are " << ( isEqualTo( c, d ) ? "equal" : "not equal" ) << '\n';
double e; // double values used for
double f; // testing equality
// test if two doubles input by user are equal
cout << "\nEnter two double values: ";
cin >> e >> f;
cout << e << " and " << f << " are "
<< ( isEqualTo( e, f ) ? "equal" : "not equal") << '\n';
Complex g( 10, 5 ); // Complex objects used
Complex h( 10, 5 ); // for testing equality
// test if two Complex objects are equal
// uses overloaded << operator
cout << "\nThe class objects " << g << " and " << h << " are "
<< ( isEqualTo( g, h ) ? "equal" : "not equal" ) << '\n';
return 0;
} // end main
| |
the output should be like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
*** Integers Tests ***
Integers: 1 and 1 are equal
Integers: 2 and 4 are "NOT" equal
Integers: -1 and 1 are "NOT" equal
Integers: -1 and -1 are equal
*** Chars Tests ***
Characters: a and a are equal
Characters: a and c are "NOT" equal
Characters: a and a are equal
Characters: a and a are equal
*** Double Tests ***
Double numbers: 2.2 and 2.2 are equal
Double numbers: 2.2 and 2.3 are "NOT" equal
Double numbers: -2.2 and 2.2 are "NOT" equal
Double numbers: -2.2 and -2.2 are equal
*** Complex Tests ***
Class objects: 10 + 5i and 10 + 5i are equal
Class objects: 10 + 5i and 10 + 54i are "NOT" equal
Class objects: 10 - 5i and 10 + 5i are "NOT" equal
Class objects: 10 - 5i and 10 - 5i are equal
*** string Tests ***
String objects: abcdefg and abcdefg are equal
String objects: abcdefg and abcdefh are "NOT" equal
String objects: -abcdefg and abcdefg are "NOT" equal
String objects: -abcdefg and -abcdefg are equal
*** Date Tests ***
Date objects: February 1, 2011 and February 1, 2011 are equal
Date objects: February 13, 2011 and February 14, 2011 are "NOT" equal
Date objects: January 13, 2011 and February 13, 2011 are "NOT" equal
Date objects: January 13, 2011 and January 13, 2011 are equal
Press any key to continue . . .
| |