cpp//6 1- This function returns the largest integer in an array: int max(int a[]
ID: 3820070 • Letter: C
Question
cpp//6
1- This function returns the largest integer in an array:
int max(int a[],int size) {
int theMax = a[0];
int i;
for (i = 1; i < size; i++) {
if (theMax < a[i]) {
theMax = a[i];
}
}
return theMax;
}
Convert this function to a template so it can find the largest of any object in an array. Document what features the object must have to be able to work with the template.
2- Name the four different types of polymphism and name one example for each type.
Explanation / Answer
[1] we can distinguish two kinds of equality.
1. Object reference equality: when two object references point to the same object.
2. Object value equality: when two separate objects happen to have the same values/state
We can Use compareTo method
if(object1.compareTo(object2) < 0)
{
max = object2
}
[2] There are FOUR kinds of polymorphism
1. Overloading (Same function names, different parameter types. This includes operator overloading and is done at compile time)
2. Parametric polymorphism (These are like templates in C++) Compile time
3. Subtype polymorphism (if a function has a parameter with a subtype, for example Car->Honda, f(Car), then function f will accept f(Honda) as well.) Runtime
4. Parameter coercion (This is an implicit type conversion. For example, a function might require a double/real/float, but will accept an int and will implicitly upcast the parameter) Compile time
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.