Question
VisualStudio C++ Make sure it runs and add comments to answer the questions(No specific format for expected output) Lab Manual - Templates Objective Implement template
VisualStudio C++ Make sure it runs and add comments to answer the questions(No specific format for expected output)
Lab Manual - Templates
Objective
- Implement template functions
- Implement template classes
- Implement templates with multiple types
Exercise 1:
As a last exercise you have to change (augment) your code so that the following main runs without errors and gives the expected output. Here Pair refers to the class you created earlier, where each element in the Pair has the same type.
int main ()
{
Pair y (2.23,3.45);
Sequence ,5> myPairs;
myPairs.setmember (0,y);
cout << myPairs.getmember(0) << ' ';
}
Notice that for this code to work you have to overload << operator for the Pair class template. Also the pair class template will need a default constructor. The friend function in this case is declared in a class template using the following syntax:
template
class Pair{
template friend ostream & operator << (ostream& out,const Pair& p);
}; Code is given Below:-
#include using namespace std;
//Answer a)
//GetMaxand GetMin fnction definition
//1) Answer of(a) and (b)
// function Temlate for GetMax template T GetMax(T x, T y) { return (x > y) ? x : y; }
// function Temlate for GetMin template P GetMin(P x, P y) { return (x < y) ? x : y; }
int main()
{ int i = 5, j = 6, k;
long l = 10, m = 5, n;
k = GetMax(i, j);
n = GetMin(l, m);
cout << k << endl;
cout << n << endl;
return 0; }
//Answer b)
//Yes the file will run without error
//2) (a) After Removing and from the above code program run peacefully
// function Temlate for GetMax template < typename T > T GetMax(T x, T y) { return (x > y) ? x : y; }
// function Temlate for GetMin template < typename P > P GetMin(P x, P y) { return (x < y) ? x : y; }
int main() {
int i = 5, j = 6, k;
long l = 10, m = 5, n;
k = GetMax(i, j);
n = GetMin(l, m);
cout << k << endl;
cout << n << endl;
return 0;
}
//2) (b)
// function Temlate for GetMax template < typename T, typename R > T GetMax(T x, R y) { return (x > y) ? x : y; }
// function Temlate for GetMin template < typename P, typename Q > P GetMin(P x, Q y) { return (x < y) ? x : y; }
int main()
{
char i = 'Z';
int j = 6, k;
long l = 10, m = 5, n;
k = GetMax(i, m);
n = GetMin(j, l);
cout << k << endl;
cout << n << endl;
return 0;
}
//Answer c)
//We can't call a funtion like k=GetMax k (i,);
//3) After Removing and from the above code program run peacefully
// function Temlate for GetMax template < typename T, typename R > T GetMax(T x, R y) { return (x > y) ? x : y; }
// function Temlate for GetMin template < typename P, typename Q > P GetMin(P x, Q y) { return (x < y) ? x : y; } int main()
{
char i = 'Z';
int j = 6, k;
long l = 10, m = 5, n;
k = GetMax(i, m);
n = GetMin(j, l);
cout << k << endl;
cout << n << endl;
return 0;
}
//Answer e)
//It wont work as syntax is incorrect
//Exercise 2
//template creation
template class Pair {
//private variable declaration
private:
T a[2];
public:
Pair(T x, T y) {
a[0] = x;
a[1] = y;
}
public:
T GetMax() {
return(a[0] > a[1]) ? a[0] : a[1];
}
public:
T GetMin();
};
templateT Pair::GetMin()
{
return(a[0] < a[1]) ? a[0] : a[1];
}
int main()
{
Pair myobject(1.012, 1.01234);
cout << myobject.GetMax() << endl;
cout << myobject.GetMin();
return 0;
}
//Exercise 3
template class Container { T data; public: Container(T data) { this->data = data; }
T increase() { return ++data; } };
template<> class Container { char data; public: Container(char data) { this->data = data; }
char uppercase() { if (data >= 'a' && data <= 'z') { data = 'A' + (data - 'a'); } return data; } };
int main() { Container myint(7); Container mychar('j'); cout << myint.increase() << endl; cout << mychar.uppercase() << endl; return 0; }
//Exercise 4:-
// Create a class template template class Sequence { // Array used to store value of type T T memblock[N];
public:
// Used to sets the member at position x in the memblock with value void setmember(int x, T value) { // Set the member at position x in the memblock with value memblock[x] = value; }
// Used to returns the value at index x T getmember(int x) { // Returns the value at index x return memblock[x]; } };
// Main function for test int main() { Sequence myints; Sequence myfloats; myints.setmember(0, 100); myfloats.setmember(3, 3.1416); cout << myints.getmember(0) << ' '; cout << myfloats.getmember(3) << ' '; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started