Write a simple function template for predicate function isEqualTo that compares
ID: 3546559 • Letter: W
Question
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.
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)
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
template<class type>
bool isEqualTo(type T1, type T2)
{
return (T1==T2);
}
class Date
{
private:
int month;
int day;
int year;
public:
friend istream &operator>>( istream &input, Date &D )
{
input >> D.month >> D.day >> D.year;
return input;
}
bool operator==(Date D1)
{
return ( (D1.month==month) && (D1.day==day) && (D1.year==year));
}
Date(int month=1,int day=1,int year=1)
{
this->month = month;
this->day = day;
this->year = year;
}
};
class Complex
{
private:
double real;
double imaginary;
public:
friend istream &operator>>( istream &input, Complex &C )
{
input >> C.real >> C.imaginary;
return input;
}
bool operator==(Complex C1)
{
return ((C1.real == real) && (C1.imaginary == imaginary));
}
Complex(double r,double i)
{
real = r;
imaginary = i;
}
};
int main()
{
cout << "are 1 and 1 are equal ? " << (isEqualTo(1,1)?"yes":"no") << endl;
cout << "are 2 and 4 are equal ? " << (isEqualTo(2,4)?"yes":"no") << endl;
cout << "are -1 and 1 are equal ? " << (isEqualTo(-1,1)?"yes":"no") << endl;
cout << "are a and a are equal ? " << (isEqualTo('a','a')?"yes":"no") << endl;
cout << "are a and c are equal ? " << (isEqualTo('a','c')?"yes":"no") << endl;
cout << "are c and a are equal ? " << (isEqualTo('c','a')?"yes":"no") << endl;
cout << "are c and c are equal ? " << (isEqualTo('c','c')?"yes":"no") << endl;
cout << "are 2.2 and 2.2 are equal ? " << (isEqualTo(2.2,2.2)?"yes":"no") << endl;
cout << "are 2.2 and 2.3 are equal ? " << (isEqualTo(2.2,2.3)?"yes":"no") << endl;
cout << "are -2.2 and 2.2 are equal ? " << (isEqualTo(-2.2,2.2)?"yes":"no") << endl;
cout << "are -2.2 and -2.2 are equal ? " << (isEqualTo(-2.2,-2.2)?"yes":"no") << endl;
cout << "are Complex(10,5) and Complex(10,5) are equal ? " << (isEqualTo(Complex(10,5),Complex(10,5))?"yes":"no") << endl;
cout << "are Complex(10,5) and Complex(10,54) are equal ? " << (isEqualTo(Complex(10,5),Complex(10,54))?"yes":"no") << endl;
cout << "are Complex(10,-5) and Complex(10,5) are equal ? " << (isEqualTo(Complex(10,-5),Complex(10,5))?"yes":"no") << endl;
cout << "are Complex(-10,-5) and Complex(-10,-5) are equal ? " << (isEqualTo(Complex(-10,-5),Complex(-10,-5))?"yes":"no") << endl;
cout << "are abcdefg and abcdefg are equal ? " << (isEqualTo("abcdefg","abcdefg")?"yes":"no") << endl;
cout << "are abcdefg and abcdefh are equal ? " << (isEqualTo("abcdefg","abcdefh")?"yes":"no") << endl;
cout << "are -abcdefg and abcdefg are equal ? " << (isEqualTo("-abcdefg","abcdefg")?"yes":"no") << endl;
cout << "are -abcdefg and -abcdefg are equal ? " << (isEqualTo("-abcdefg","-abcdefg")?"yes":"no") << endl;
cout << "are Date(2,13,2011) and Date(2,13,2011) are equal ? " << (isEqualTo(Date(2,13,2011),Date(2,13,2011))?"yes":"no") << endl;
cout << "are Date(2,13,2011) and Date(2,14,2011) are equal ? " << (isEqualTo(Date(2,13,2011),Date(2,14,2011))?"yes":"no") << endl;
cout << "are Date(2,13,2011) and Date(2,14,2011) are equal ? " << (isEqualTo(Date(2,13,2011),Date(2,14,2011))?"yes":"no") << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.