Triplet Template Class

My question:
Define a template class for a generic triplet. The private data member for the triplet is a
generic array with three elements. The triplet ADT has the following functions:
 default constructor
 explicit constructor: initialize the data member using parameters
 three accessors (three get functions) which will return the value of each individual
element of the array data member
 one mutator (set function) which will assign values to the data member using
paramenters
 find_max function which will find the maximum value of the array and return it
 find_min function which will find the minimum value of the array and return it
 sort_ascending function which will sort the array data member into ascending order
 sort_descending function which will sort the array data member into descending order
 an overloaded comparison operator == which will compare if two triplets are exactly
the same
 an overloaded output operator to output items in the triplet.
Write a driver to test your class. It should test each function for
o a triplet of strings
o a triplet of integers
For credit: Show your code and execute the test program

My code so far:

Header.h

#include <iostream>
#include <string>
using namespace std;
template <class T>
class triplet
{
public:
T A, B, C;
int Array[50];
triplet()
//Purpose: default constructor
//Precondition: array
//Postcondition: array is declared as empty
{
T a = 0;
T b = 0;
T c = 0;
};
triplet(T a, T b, T c)
//Purpose: explicit constructor
//Precondition: array
//Postcondition: initalizes the data member with parameters
{
A = a;
B = b;
C = c;
};
void get_A()
//Purpose: returns A value
//Precondition: A value
//Postcondition: returns A to array
{
return A;
};
void get_B()
//Purpose: returns B value
//Precondition: B value
//Postcondition: returns B to array
{
return B;
};
void get_C()
//Purpose: returns C value
//Precondition: C value
//Postcondition: returns C to array
{
return C;
};
void set(T a, T b, T c)
//Purpose: assigns values to the data member
//Precondition: data members
//Postcondition: values are assigned to the data member
{
A = a;
B = b;
C = c;
};
T find_max()
//Purpose: finds maximum number
//Precondition: filled array
//Postcondition: returns max number
{
return max(A, max(B, C));
};
T find_min()
//Purpose: finds minimum number
//Precondition: filled array
//Postcondition: returns min number
{
return min(A, min(B, C));
};
void sort_ascending()
//Purpose: sorts the array in ascending order
//Precondition: filled array
//Postcondition: returns array in ascending order
{
for (i = 0; i <= n; i++)
{
for (j = 0; j <= n - i; j++)
{
if (a[j]>a[j + 1])
{
temp = Array[j];
Array[j] = Array[j + 1];
Array[j + 1] = temp;
}
}
}
cout << "\nData after sorting: ";
for (j = 0; j<n; j++)
{
cout << Array[j];
}
};
void sort_descending()
//Purpose: sorts array in descending order
//Precondition: filled array
//Postcondition: returns array in descending order
{
for (i = 0; i <= n; i++)
{
for (j = 0; j <= n - i; j++)
{
if (a[j]<a[j + 1])
{
temp = Array[j];
Array[j] = Array[j + 1];
Array[j + 1] = temp;
}
}
}
cout << "\nData after sorting: ";
for (j = 0; j<n; j++)
{
cout << Array[j];
}
};
bool operator==(const triplet&t)
//Purpose: compares two triplets to see if they're the same
//Precondition: filled array
//Postcondition: returns true or false
{
if (t.a == this->a&&t.b == this->b&&t.c == this->c)
return true;
return false;
};
ostream&operator<<(ostream&out, const triplet<T>* list)
//Purpose: overloaded output operator
//Precondition: filled array
//Postcondition: outputs items in the triplet
{
output << "A B C are: " << list.A << " " << list.B << " " << list.C << endl;
return output;
}
};

Main.cpp
#include "Header.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
triplet<int> one(12, 9, 16);
cout << one << endl;
cout << "Maximum is: " << one.find_max() << endl;
cout << "Minimum is: " << one.find_min() << endl;
triplet<string> two("hello", "hi", "bye");
cout << two << endl;
return 0;
}

I am getting many errors. What all did I do wrong or need to add?
The private data member for the triplet is a generic array with three elements.

which should be :
1
2
3
T m_array[2];
//or if you can use std::array
std::array<T, 2> m_array;

instead of T A, B, C; that you have now
and what is the purpose of this: int Array[50];

couple other points:
(a) use code-tags
(b)
I am getting many errors
- describe what these errors are
Last edited on
I fixed the array to what you said and adjusted my code accordingly. I have a hard time with arrays; I find them confusing at times. Thank you for showing me! I altered everything else and got the program to work!
I have a hard time with arrays; I find them confusing at times.
you're welcome and as for arrays, unless it's a homework requirement, try and use std::vector or other standard library containers as much as possible:
http://en.cppreference.com/w/cpp/container
The private data member for the triplet is a generic array with three elements.

which should be :
1
2
3
T m_array[2];
//or if you can use std::array
std::array<T, 2> m_array;


Note that each 2 in in those definitions should be a 3.
thanks cire, one of these days I'll learn to count!
Topic archived. No new replies allowed.