Question

In: Computer Science

Write a simple function template for predicate function isEqualTo that compares its two arguments of the...

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;

}

Solutions

Expert Solution

#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 Solutions

Write a simple function template for predicate function isEqualTo that compares its two arguments of the...
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...
Write a simple function template for predicate function isEqualTo that compares its two arguments of the...
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...
Write a function that will receive two input arguments that is the height in inches (in.)...
Write a function that will receive two input arguments that is the height in inches (in.) and the weight in pounds (lb) of a person and return two output arguments that is the height in meters (m) and mass in kilograms (kg). Determine in SI units the height and mass of a 5 ft.15 in. person who weight 180 lb. Determine your own height and weight in SI units. Note: 1 meter = 39.3701 inch, 1 foot = 12 inches,...
In a program, write a function that accepts two arguments: a list, and a number n....
In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all of the numbers in the list that are greater than the number n. The program should ask for a list of numbers from the user as well as a value (a, b, c)--> inputs from user. After that, each number in that list should be compared to that value (a or b or...
JavaScript Write a function called "first" that takes in two arguments - the first is an...
JavaScript Write a function called "first" that takes in two arguments - the first is an argument called arr that is an array of numbers, the second is an optional number argument called num(hint: you'll need a default value - look back at the slides). If "num" was not passed in (since it's optional), the "first" function will return an array containing the first item of the array. If a value for "num" was given, the "first" function will return...
In this third part, write a function that accepts an array of integers and its size as arguments.
in C++In this third part, write a function that accepts an array of integers and its size as arguments. The function should create a new array that is of half size the argument array (round up the fraction if the size of the argument array is odd). The value of the first element (Element 0) of the new array should be the sum of the first two elements of the argument array (Element 0 and 1). Element 1 of the...
Write a function that accepts two arguments (say a and b) by value and one argument...
Write a function that accepts two arguments (say a and b) by value and one argument (say c) by reference. Then it calculates the multiplication of all the numbers between a and b (inclusive) and saves the results in c. The function does not return any value.
Write a C++ function template to add two inputs and return their result. Make exceptions for...
Write a C++ function template to add two inputs and return their result. Make exceptions for Characters (such that sum of two characters is a character associated with sum of their ASCII) and String (such that sum of two strings is their concatenation)
Write a user-defined MATLAB function, with two input and two output arguments that determines the height...
Write a user-defined MATLAB function, with two input and two output arguments that determines the height in centimeters (cm) and mass in kilograms (kg)of a person from his height in inches (in.) and weight in pounds (lb). (a) Determine in SI units the height and mass of a 5 ft.15 in. person who weight 180 lb. (b) Determine your own height and weight in SI units.
Using C++ Write a template function that accepts an integer parameter and returns its integer square...
Using C++ Write a template function that accepts an integer parameter and returns its integer square root. The function should return -1, if the argument passed is not integer. Demonstrate the function with a suitable driver program .
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT