Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following declaration: (8, 9) template <class type> class strange {

ID: 3822084 • Letter: C

Question

Consider the following declaration: (8, 9)

template <class type>

class strange

{

.

.

.

private:

type a;

type b;

};

a. Write a statement that declares sObj to be an object of type strange

such that the private member variables a and b are of type int.

b. Write a statement that shows the declaration in the class strange to


overload the operator == as a member function.

c. Assume that two objects of type strange are equal if their correspond-

ing member variables are equal. Write the definition of the function

operator== for the class strange, which is overloaded as a

member function.

Explanation / Answer

Consider the following declaration: (8, 9)

template <class type>
class strange
{
private:
   type a;
   type b;
};

a. Write a statement that declares sObj to be an object of type strange

Ans:    strange<int> sObj;

such that the private member variables a and b are of type int.

b. Write a statement that shows the declaration in the class strange to overload the operator == as a member function.

   Ans:   bool operator==(strange& rhs) const;

c. Assume that two objects of type strange are equal if their corresponding member variables are equal.
Write the definition of the function operator== for the class strange, which is overloaded as a member function.

template <class type>
bool strange<type>::operator==(strange& rhs) const{
  
   if(a == rhs.a && b == rhs.b)
       return true;
   else
       return false;  
}