Consider the following C++ template class. The class Vector cannot be instantiat
ID: 3766551 • Letter: C
Question
Consider the following C++ template class.
The class Vector cannot be instantiated for any arbitrary type. For example, consider the following instantiation for a wrapper integer class.
Explain why the second implementation fails. What must be added to that class so this program will compile? Suppose this program were written in Java. Explain how Java allows the constraints on a generic type parameter to be specified and how they would be specified in this case Java does have one limitation, however. Although wrapper classes can be used to instantiate generic type parameters, primitive types cannot. Explain why.
Explanation / Answer
Output of the First Implementation is 1
#include <iostream>
using namespace std;
template <typename T, int length>
class Vector
{
public:
Vector(T values[length])
{
for (int i = 0; i < length; i++)
list[i] = values[i];
}
friend bool operator<(const Vector<T, length>& left, const Vector<T, length>& right)
{
bool result = true;
for (int i = 0; i < length; i++)
result &= left.list[i] < right.list[i];
return result;
}
private:
T list[length];
};
int main()
{
int first[] = {1, 2}, second[] = {2, 3};
Vector<int, 2> vector1(first), vector2(second);
cout << (vector1 < vector2) << endl;
return 0;
}
The second implementation fails because the operands of friend bool operator are Vector objects. When the operator is used in main method, the type of arguments is indicated as integer
#include <vector>
#include <iostream>
using namespace std;
template <typename T, int length>
class Vector
{
public:
Vector(T values[length])
{
for (int i = 0; i < length; i++)
list[i] = values[i];
}
friend bool operator<(const Vector<T, length>& left, const Vector<T, length>& right)
{
bool result = true;
for (int i = 0; i < length; i++)
result &= left.list[i] < right.list[i];
return result;
}
private:
T list[length];
};
class Int
{
public:
Int(int i = 0) {this->i = i;}
private:
int i;
};
int main()
{
Int first[] = {Int(1), Int(2)}, second[] = {Int(2), Int(3)};
Vector<Int, 2> vector1(first), vector2(second);
cout << (vector1 < vector2) << endl;
return 0;
}
The second implementation fails because the operands of friend bool operator are Vector objects. When the operator is used in main method, the type of arguments is indicated as integer
binomial comparison operating program in Java by using Vector class, generics, and compareTo method
/*
CHEGG EXPERT
*/
GenericTypeTest.java
import java.util.Vector;
public class GenericTypeTest
{
public static void main(String[] args)
{
int resultinteger=0;
// Instantiates a generic type of Vector object
Vector<String> strings = new Vector<String>();
strings.add("dfr");//oth elements
System.out.println("strings"+strings);
strings.add("asd");//1st element
System.out.println("strings"+strings);
resultinteger = strings.elementAt(0).compareTo(strings.elementAt(1));
System.out.println(ifleftissmall(resultinteger));
// Instantiates a generic type of Vector object
Vector<Integer> integers = new Vector<Integer>();
integers.add(1);
integers.add(2);
resultinteger = integers.elementAt(0).compareTo(integers.elementAt(1));
System.out.println("resultinteger:"+resultinteger);
System.out.println(ifleftissmall(resultinteger));
}
// compareTo method which returns a boolean value
static boolean ifleftissmall(int resultinteger)
{
boolean result = true;
if (resultinteger < 0)
result = true;
else if (resultinteger > 0)
result = false;
return result;
}
}
Output
strings[dfr]
strings[dfr, asd]
false
resultinteger:-1
true
BUILD SUCCESSFUL (total time: 0 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.