need help, im not that smart on templates

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 . . .



1
2
template <class T>
bool isEqualTo(const T&, const T&);
you can at least figure out the body.
yes i can but what goes further, im just starting to learn on templates
this is what i got :
this is for only int if you guys help me and help set the first one and the the results for first int, like show below, ill be able to do all the the rest

THE OUTPUT:
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 . . .


AND THERE IS I HAVE GOT:
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
# include <iostream>
using namespace std;
class largeintegers;
{
public:
largeintegers();
void Input();
bool operator==( largeintegers);
private:
int integer[ 10 ];
int len;
};
void largeintegers::Input()
{string in;
int i,j,k;
cout<<"Enter a number("<<10<<" digits max):";
cin>>in;
for(i=0;in[i]!='\0';i++);
len=i;
k=0;
for(j=i-1;j>=0;j--)
integer[j]=in[k++]-48;
}
largeintegers::largeintegers( )
{
for ( int i = 0; i <10; i++ )
integer[ i ] = 0;
len=9;
}
bool largeintegers::operator==( largeintegers op2 )
{int i;
if(len!=op2.len)
return false;
for(i=len-1;i>=0;i--)
if(integer[i]!=op2.integer[i])
return false;

return true;
}
template <class Type>
bool isEqualTo(Type a ,Type b)
{if(a==b)
return true;
return false;
}

int main ()
{int a=1,b=2,f=2;
double c=3,d=4,e=4;
largeintegers n1,n2;
cout<<"a=1,b=2: "<<isEqualTo(a,b)<<endl;
cout<<"b=2,f=2: "<<isEqualTo(b,f)<<endl;
cout<<"c=3,d=4: "<<isEqualTo(c,d)<<endl;
cout<<"d=4,e=4: "<<isEqualTo(d,e)<<endl;

n1.Input();
n2.Input();
cout<<"n1,n2: "<<isEqualTo(n1,n2)<<endl;
system("pause");
return 0;
}
Last edited on
Topic archived. No new replies allowed.