In: Computer Science
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.
Here is the code that works but I need it broken down into three files as asked above: Date, Complex, and main file.
#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;
}
friend ostream &operator<<( ostream &output, Date &D )
{
string months[] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
output << months[D.month-1] << " " << D.day << "," << " " << D.year;
return output;
}
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;
}
friend ostream &operator<<( ostream &output, Complex &C )
{
output << C.real << "+" << C.imaginary<<"i";
return output;
}
bool operator==(Complex C1)
{
return ((C1.real == real) && (C1.imaginary == imaginary));
}
Complex(double r,double i)
{
real = r;
imaginary = i;
}
};
int main()
{
cout << "*** Integers Tests *** " << endl;
cout <<"Integers: 1 and 1 are "<< (isEqualTo(1,1)?"":"NOT") << " equal Integers: " << endl;
cout <<"Integers: 2 and 4 are "<< (isEqualTo(2,4)?"":"NOT") << " equal Integers: " << endl;
cout <<"Integers: -1 and 1 are "<< (isEqualTo(-1,1)?"":"NOT") << " equal Integers: " << endl;
cout <<"Integers: -1 and -1 are "<< (isEqualTo(-1,-1)?"":"NOT") << " equal Integers: " << endl;
cout << endl;
cout << " *** Chars Tests *** " << endl;
cout <<"Characters: a and a are "<< (isEqualTo('a','a')?"":"NOT") << " equal Characters: " << endl;
cout <<"Characters: a and c are "<< (isEqualTo('a','c')?"":"NOT") << " equal Characters: " << endl;
cout <<"Characters: c and a are "<< (isEqualTo('c','a')?"":"NOT") << " equal Characters: " << endl;
cout <<"Characters: c and c are "<< (isEqualTo('c','c')?"":"NOT") << " equal Characters: " << endl;
cout << endl;
cout << " *** Double Tests *** " << endl;
cout <<"Doubles: 2.2 and 2.2 are "<< (isEqualTo(2.2,2.2)?"":"NOT") << " equal Doubles: " << endl;
cout <<"Doubles: 2.2 and 2.3 are "<< (isEqualTo(2.2,2.3)?"":"NOT") << " equal Doubles: " << endl;
cout <<"Doubles: -2.2 and 2.2 are "<< (isEqualTo(-2.2,2.2)?"":"NOT") << " equal Doubles: " << endl;
cout <<"Doubles: -2.2 and -2.2 are "<< (isEqualTo(-2.2,-2.2)?"":"NOT") << " equal Doubles: " << endl;
cout << endl;
cout <<"*** Complex Tests *** " <<endl;
Complex C1(10,5);
Complex C2(10,5);
cout <<"Class objects: "<< C1 << " and " << C2 <<" are "<< (isEqualTo(C1,C2)?"":"NOT")<< " equal "<< endl;
Complex C3(10,5);
Complex C4(10,54);
cout <<"Class objects: "<< C3 << " and " << C4 <<" are "<< (isEqualTo(C3,C4)?"":"NOT")<< " equal "<< endl;
Complex C5(10,-5);
cout <<"Class objects: "<< C5 << " and " << C1 <<" are "<< (isEqualTo(C5,C1)?"":"NOT")<< " equal "<< endl;
Complex C6(10,-5);
cout <<"Class objects: "<< C5 << " and " << C6 <<" are "<< (isEqualTo(C5,C6)?"":"NOT")<< " equal "<< endl;
cout << endl;
cout << "*** string Tests *** "<<endl;
cout << "String objects: abcdefg and abcdefg are " << (isEqualTo("abcdefg","abcdefg")?"":"NOT") <<" equal "<< endl;
cout << "String objects: abcdefg and abcdefh are " << (isEqualTo("abcdefg","abcdefh")?"":"NOT") << "equal "<< endl;
cout << "String objects: -abcdefg and abcdefg are " << (isEqualTo("-abcdefg","abcdefg")?"":"NOT") <<" equal "<< endl;
cout << "String objects: -abcdefg and -abcdefg are " << (isEqualTo("-abcdefg","-abcdefg")?"":"NOT") <<" equal "<< endl;
cout << endl;
cout <<"*** Date Tests *** " << endl;
Date D1(2,1,2011);
Date D2(2,1,2011);
cout <<"Date objects: " <<D1 << " and " << D2 << " are " <<(isEqualTo(D1,D2)?"":"NOT") << " equal " << endl;
Date D3(2,13,2011);
Date D4(2,14,2011);
cout <<"Date objects: " <<D3 << " and " << D4 << " are " <<(isEqualTo(D3,D4)?"":"NOT") << " equal " << endl;
Date D5(1,13,2011);
Date D6(2,13,2011);
cout <<"Date objects: " <<D5 << " and " << D6 << " are " <<(isEqualTo(D5,D6)?"":"NOT") << " equal " << endl;
Date D7(1,13,2011);
Date D8(1,13,2011);
cout <<"Date objects: " <<D7 << " and " << D8 << " are " <<(isEqualTo(D7,D8)?"":"NOT") << " equal " << endl;
//system("pause");
return 0;
}
***********main file************************
#include<iostream>
#include<string>
#include<Complex>
#include<Date>
using namespace std;
int main()
{
cout << "*** Integers Tests *** " << endl;
cout <<"Integers: 1 and 1 are "<< (isEqualTo(1,1)?"":"NOT") << " equal Integers: " << endl;
cout <<"Integers: 2 and 4 are "<< (isEqualTo(2,4)?"":"NOT") << " equal Integers: " << endl;
cout <<"Integers: -1 and 1 are "<< (isEqualTo(-1,1)?"":"NOT") << " equal Integers: " << endl;
cout <<"Integers: -1 and -1 are "<< (isEqualTo(-1,-1)?"":"NOT") << " equal Integers: " << endl;
cout << endl;
cout << " *** Chars Tests *** " << endl;
cout <<"Characters: a and a are "<< (isEqualTo('a','a')?"":"NOT") << " equal Characters: " << endl;
cout <<"Characters: a and c are "<< (isEqualTo('a','c')?"":"NOT") << " equal Characters: " << endl;
cout <<"Characters: c and a are "<< (isEqualTo('c','a')?"":"NOT") << " equal Characters: " << endl;
cout <<"Characters: c and c are "<< (isEqualTo('c','c')?"":"NOT") << " equal Characters: " << endl;
cout << endl;
cout << " *** Double Tests *** " << endl;
cout <<"Doubles: 2.2 and 2.2 are "<< (isEqualTo(2.2,2.2)?"":"NOT") << " equal Doubles: " << endl;
cout <<"Doubles: 2.2 and 2.3 are "<< (isEqualTo(2.2,2.3)?"":"NOT") << " equal Doubles: " << endl;
cout <<"Doubles: -2.2 and 2.2 are "<< (isEqualTo(-2.2,2.2)?"":"NOT") << " equal Doubles: " << endl;
cout <<"Doubles: -2.2 and -2.2 are "<< (isEqualTo(-2.2,-2.2)?"":"NOT") << " equal Doubles: " << endl;
cout << endl;
cout <<"*** Complex Tests *** " <<endl;
Complex C1(10,5);
Complex C2(10,5);
cout <<"Class objects: "<< C1 << " and " << C2 <<" are "<< (isEqualTo(C1,C2)?"":"NOT")<< " equal "<< endl;
Complex C3(10,5);
Complex C4(10,54);
cout <<"Class objects: "<< C3 << " and " << C4 <<" are "<< (isEqualTo(C3,C4)?"":"NOT")<< " equal "<< endl;
Complex C5(10,-5);
cout <<"Class objects: "<< C5 << " and " << C1 <<" are "<< (isEqualTo(C5,C1)?"":"NOT")<< " equal "<< endl;
Complex C6(10,-5);
cout <<"Class objects: "<< C5 << " and " << C6 <<" are "<< (isEqualTo(C5,C6)?"":"NOT")<< " equal "<< endl;
cout << endl;
cout << "*** string Tests *** "<<endl;
cout << "String objects: abcdefg and abcdefg are " << (isEqualTo("abcdefg","abcdefg")?"":"NOT") <<" equal "<< endl;
cout << "String objects: abcdefg and abcdefh are " << (isEqualTo("abcdefg","abcdefh")?"":"NOT") << "equal "<< endl;
cout << "String objects: -abcdefg and abcdefg are " << (isEqualTo("-abcdefg","abcdefg")?"":"NOT") <<" equal "<< endl;
cout << "String objects: -abcdefg and -abcdefg are " << (isEqualTo("-abcdefg","-abcdefg")?"":"NOT") <<" equal "<< endl;
cout << endl;
cout <<"*** Date Tests *** " << endl;
Date D1(2,1,2011);
Date D2(2,1,2011);
cout <<"Date objects: " <<D1 << " and " << D2 << " are " <<(isEqualTo(D1,D2)?"":"NOT") << " equal " << endl;
Date D3(2,13,2011);
Date D4(2,14,2011);
cout <<"Date objects: " <<D3 << " and " << D4 << " are " <<(isEqualTo(D3,D4)?"":"NOT") << " equal " << endl;
Date D5(1,13,2011);
Date D6(2,13,2011);
cout <<"Date objects: " <<D5 << " and " << D6 << " are " <<(isEqualTo(D5,D6)?"":"NOT") << " equal " << endl;
Date D7(1,13,2011);
Date D8(1,13,2011);
cout <<"Date objects: " <<D7 << " and " << D8 << " are " <<(isEqualTo(D7,D8)?"":"NOT") << " equal " << endl;
//system("pause");
return 0;
}
*********************************************************
******************complex*****************************
#include<iostream>
#include<string>
using namespace std;
class Complex
{
private:
double real;
double imaginary;
public:
friend istream &operator>>( istream &input, Complex &C )
{
input >> C.real >> C.imaginary;
return input;
}
friend ostream &operator<<( ostream &output, Complex &C )
{
output << C.real << "+" << C.imaginary<<"i";
return output;
}
bool operator==(Complex C1)
{
return ((C1.real == real) && (C1.imaginary == imaginary));
}
Complex(double r,double i)
{
real = r;
imaginary = i;
}
};
******************************************************
******************date*******************************
#include<iostream>
#include<string>
using namespace std;
class Complex
{
private:
double real;
double imaginary;
public:
friend istream &operator>>( istream &input, Complex &C )
{
input >> C.real >> C.imaginary;
return input;
}
friend ostream &operator<<( ostream &output, Complex &C )
{
output << C.real << "+" << C.imaginary<<"i";
return output;
}
bool operator==(Complex C1)
{
return ((C1.real == real) && (C1.imaginary == imaginary));
}
Complex(double r,double i)
{
real = r;
imaginary = i;
}
};
**************************************************