C++ Programming Templates Create a class template Pair that houses 2 items of da
ID: 3889535 • Letter: C
Question
C++ Programming Templates
Create a class template Pair that houses 2 items of datatype T.
Whatever datatype stored needs to implement the < and > operators.
There should be two private data attributes called item1 and item2.
There should be the following public methods.
A 2 argument constructor.
A method getMax that returns the larger of the two stored items.
A method getMin that returns the smaller of the two stored items.
A method setItems that takes in two constant items by reference and sets item1 and item2 to them.
A method getItem1 that returns item1.
A method getItem2 that returns item2.
Make a appropriate methods const so they don't alter item1 or item2 if not needed.
You should have a Pair.h and Pair.cpp file.
Create a pairTest.cpp file with a main() that creates two pairs, one that houses ints an one that houses strings.
Explanation / Answer
Given below are the needed files. If you are compiling in an IDE, make sure you don't compile Pair.cpp as it is included already in Pair.h. Otherwise you will get duplicate definition error. If not sure , compile in command line using the command
g++ pairTest.cpp
./a.out
Hope the answer helps. If it does, please don't forget to rate the answer. Thank you very much.
Pair.h
#ifndef Pair_h
#define Pair_h
template <typename T>
class Pair
{
private :
T item1;
T item2;
public :
Pair(T item_1, T item_2);
T getMax() const;
T getMin() const;
void setItems(T item_1, T item_2);
T getItem1() const;
T getItem2() const;
};
#include "Pair.cpp"
#endif /* Pair_h */
Pair.cpp
#include "Pair.h"
template <typename T>
Pair<T>::Pair(T item_1, T item_2)
{
this->item1 = item_1;
this->item2 = item_2;
}
template <typename T>
T Pair<T>::Pair::getMax() const
{
if(item1 > item2)
return item1;
else
return item2;
}
template <typename T>
T Pair<T>::getMin() const
{
if(item1 < item2)
return item1;
else
return item2;
}
template <typename T>
void Pair<T>::setItems(T item_1, T item_2)
{
this->item1 = item_1;
this->item2 = item_2;
}
template <typename T>
T Pair<T>::getItem1() const
{
return item1;
}
template <typename T>
T Pair<T>::getItem2() const
{
return item2;
}
pairTest.cpp
#include "Pair.h"
#include <iostream>
using namespace std;
int main()
{
Pair<int> numbers(5,9);
Pair<string> fruits("grapes", "banana");
cout << "numbers.item1 = " << numbers.getItem1() << endl;
cout << "numbers.item2 = " << numbers.getItem2() << endl;
cout << "max of numbers = " << numbers.getMax() <<endl;
cout << "min of numbers = " << numbers.getMin() <<endl << endl;
cout << "fruits.item1 = " << fruits.getItem1() << endl;
cout << "fruits.item1 = " << fruits.getItem2() << endl;
cout << "max of fruits = " << fruits.getMax() <<endl;
cout << "min of fruits = " << fruits.getMin() <<endl;
}
output
numbers.item1 = 5
numbers.item2 = 9
max of numbers = 9
min of numbers = 5
fruits.item1 = grapes
fruits.item1 = banana
max of fruits = grapes
min of fruits = banana
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.