My assignment: Triplet Template Class Directions: Define a template class for a
ID: 3864618 • Letter: M
Question
My assignment:
Triplet Template Class Directions:
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.
Here is what I have so far:
Header.h
#include <iostream>
#include <string>
using namespace std;
template <class T>
class triplet
{
public:
T A, B, C;
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 = Arr[j];
Arr[j] = Arr[j + 1];
Arr[j + 1] = temp;
}
}
}
cout << " Data after sorting: ";
for (j = 0; j<n; j++)
{
cout << Arr[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 = Arr[j];
Arr[j] = Arr[j + 1];
Arr[j + 1] = temp;
}
}
}
cout << " Data after sorting: ";
for (j = 0; j<n; j++)
{
cout << Arr[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;
}
Am I on the right path?
Explanation / Answer
# include <iostream.h>
class Test
{
public:
int Arr[50];
void findminmax();
void sortasc ();
void sortdec();
}
{
}
{
}
{
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.