Directions (C++ Programming) Define a template class for a generic triplet. The
ID: 3680024 • Letter: D
Question
Directions (C++ Programming)
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.
Explanation / Answer
Here is the code for the question. Please don't forget to rate the answer if it helped .Thank you very much.
Triplet.h
#ifndef TRIPLET_H
#define TRIPLET_H
#include<iostream>
#include <fstream>
using namespace std;
template <typename T>
class Triplet
{
private:
T data[3];
public:
//default constructor
Triplet()
{
data[0]=T();
data[1]=T();
data[2]=T();
}
//explicit constructor with 3 arguments
Triplet(T d1, T d2, T d3)
{
data[0] = d1;
data[1] = d2;
data[2] = d3;
}
//3 accessors
T get_data1()
{
return data[0];
}
T get_data2()
{
return data[1];
}
T get_data3()
{
return data[2];
}
//mutator function
void set_data(T d1, T d2, T d3)
{
data[0] = d1;
data[1] = d2;
data[2] = d3;
}
//finds and returns the maximum in the triplet array
T find_max()
{
if(data[0] > data[1])
{
if(data[0] > data[2])
return data[0];
else
return data[2];
}
else
{
if(data[1] > data[2])
return data[1];
else
return data[2];
}
}
//finds and returns the minimum in the triplet array
T find_min()
{
if(data[0] < data[1])
{
if(data[0] < data[2])
return data[0];
else
return data[2];
}
else
{
if(data[1] < data[2])
return data[1];
else
return data[2];
}
}
//sorts the arry in ascending order using selection sort
void sort_ascending()
{
int minIdx;
for(int i=0; i <3;i ++)
{
minIdx = i;
for(int j=i+1; j<3; j++)
{
if(data[j] < data[minIdx])
minIdx = j;
}
if(minIdx != i)
{
T temp = data[i];
data[i] = data[minIdx];
data[minIdx] = temp;
}
}
}
//sorts the arry in descending order using selection sort
void sort_descending()
{
int maxIdx;
for(int i=0; i <3;i ++)
{
maxIdx = i;
for(int j=i+1; j<3; j++)
{
if(data[j] > data[maxIdx])
maxIdx = j;
}
if(maxIdx != i)
{
T temp = data[i];
data[i] = data[maxIdx];
data[maxIdx] = temp;
}
}
}
//overloaded == operator to compare two triplets
bool operator ==(const Triplet<T> other)
{
return data[0] == other.data[0] && data[1] == other.data[1] && data[2] == other.data[2];
}
friend ostream& operator <<(ostream &out, const Triplet<T>& tri)
{
out<< tri.data[0] << " " << tri.data[1] << " " << tri.data[2] ;
return out;
}
};
#endif
triplet-driver.cpp
#include <iostream>
#include "Triplet.h"
using namespace std;
int main()
{
Triplet<string> words("What","Hello","Please") , words2("hi", "Everybody","!");
cout<<"1st item in words is "<<words.get_data1()<<endl;
cout<<"2nd item in words is "<<words.get_data2()<<endl;
cout<<"3rd item in words is "<<words.get_data3()<<endl;
cout<<"max word is "<<words.find_max()<<endl;
cout<<"min word is "<<words.find_min()<<endl;
words.sort_ascending();
cout<<"words sorted in ascending order is "<<words<<endl;
words.sort_descending();
cout<<"words sorted in descending order is "<<words<<endl;
cout<<"words2 = "<<words2<<endl;
if(words == words2)
cout<<"words is equal to words2"<<endl;
else
cout<<"words is not equal to words2"<<endl;
cout<<"---------------------"<<endl;
Triplet<int> nums1, nums2(5,3,8);
nums1.set_data(5,3,8);
cout<<"1st item in nums1 is "<<nums1.get_data1()<<endl;
cout<<"2nd item in nums1 is "<<nums1.get_data2()<<endl;
cout<<"3rd item in nums1 is "<<nums1.get_data3()<<endl;
cout<<"max num is "<<nums1.find_max()<<endl;
cout<<"min num is "<<nums1.find_min()<<endl;
if(nums1 == nums2)
cout<<"nums1 is equal to nums2"<<endl;
else
cout<<"nums1 is not equal to nums2"<<endl;
nums1.sort_ascending();
cout<<"nums1 sorted in ascending order is "<<nums1<<endl;
nums1.sort_descending();
cout<<"nums1 sorted in descending order is "<<nums1<<endl;
cout<<"nums2 = "<<nums2<<endl;
}
output
1st item in words is What
2nd item in words is Hello
3rd item in words is Please
max word is What
min word is Hello
words sorted in ascending order is Hello Please What
words sorted in descending order is What Please Hello
words2 = hi Everybody !
words is not equal to words2
---------------------
1st item in nums1 is 5
2nd item in nums1 is 3
3rd item in nums1 is 8
max num is 8
min num is 3
nums1 is equal to nums2
nums1 sorted in ascending order is 3 5 8
nums1 sorted in descending order is 8 5 3
nums2 = 5 3 8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.