/* Filename: templat5.cpp Author: Br. David Carlson Date: August 2, 2003 This program sets up an OrderedPair class, parameterized by two types, and then uses it to create and print various ordered pairs. Note that the first and second components of a given ordered pair are not required to have the same type, thanks to the two generic types. Tested with: Microsoft Visual C++ 2019 */ #include #include using namespace std; // OrderedPair class parameterized by two types, TypeA and TypeB: template class OrderedPair { private: TypeA First; TypeB Second; public: OrderedPair(TypeA InitFirst, TypeB InitSecond); void SetFirst(TypeA NewFirst); void SetSecond(TypeB NewSecond); void GetFirst(TypeA & FirstValue) const; void GetSecond(TypeB & SecondValue) const; void Print(void) const; }; /* Given: InitFirst A data item of type TypeA. InitSecond A data item of type TypeB. Task: This is the constructor. It creates a new OrderedPair object containing InitFirst and InitSecond (in that order). Assumes: That assigment works for data of types TypeA and TypeB. Return: Nothing, though the implicit object is created. */ template OrderedPair::OrderedPair(TypeA InitFirst, TypeB InitSecond) { First = InitFirst; Second = InitSecond; } /* Given: Nothing (other than the implicit object). Task: To print the ordered pair represented by the implicit object. Assumes: That data of TypeA and TypeB can be output with <<. Return: Nothing. */ template void OrderedPair::Print(void) const { cout << '(' << First << ", " << Second << ')' << endl; } /* Given: NewFirst A data item of type TypeA. Task: To use NewFirst as the first component of the implicit object ordered pair. Assumes: That > works for data of type TypeA. Return: Nothing directly, but the implicit object is updated. */ template void OrderedPair::SetFirst(TypeA NewFirst) { First = NewFirst; } /* Given: NewSecond A data item of type TypeB. Task: To use NewSecond as the second component of the implicit object ordered pair. Assumes: That > works for data of type TypeB. Return: Nothing directly, but the implicit object is updated. */ template void OrderedPair::SetSecond(TypeB NewSecond) { Second = NewSecond; } /* Given: Nothing (other than the implicit object). Task: To look up the first component of this object. Assumes: That assignment works for data of type TypeA. Return: FirstValue The first component value. */ template void OrderedPair::GetFirst(TypeA & FirstValue) const { FirstValue = First; } /* Given: Nothing (other than the implicit object). Task: To look up the second component of this object. Assumes: That assignment works for data of type TypeB. Return: SecondValue The second component value. */ template void OrderedPair::GetSecond(TypeB & SecondValue) const { SecondValue = Second; } int main(void) { OrderedPair PairA(3.1416, 12); OrderedPair PairB(32, "Thirty-two"); OrderedPair PairC('e', 2.17828); OrderedPair PairD("", ""); OrderedPair Point(2.4, 3.57); PairA.Print(); PairB.Print(); PairC.Print(); PairD.Print(); Point.Print(); PairD.SetFirst("Name"); PairD.SetSecond("Juan Diego"); PairD.Print(); return 0; }