CS210 Lab: Operator Overloading Postlab Answers


Postlab Answers:

There are five questions to this postlab exercise.
  1. Take the code from Section 2 of this lab and add additional code to and overload the "+" operator using arrays.
    Hint: the prototype will look like the following:
    int * operator+(int array1[MAXARRAY], int array2[MAXARRAY])
    
    What happens when you compile this code?

    The code for the overloaded operator is the following:

    int * operator+(int array1[MAXARRAY], int array2[MAXARRAY])
    {
    	int array3[MAXARRAY];
    	for (i=0; i<MAXARRAY; i++)
    		array3[i]=array1[i] + array2[i];
    	return array3;
    }
    
    You will get the following compiler error:
    'operator +' must have at least one formal parameter of class type
    This is because you may only define overloaded operators for operands of class type or enumeration type.

  2. In section 4, we introduced the concept of friend. If we wanted to, we could rewrite the code for operator+ so that it doesn't need to access the value array directly. How can we do that? Specify the new code.

    Use getvalue and setvalue

    myarray operator+(myarray array1, myarray array2)
    {
    	myarray temparray;
    	for (int i=0; i<MAXARRAY; i++)
    		temparray.setvalue(i, (array1.getvalue(i) + array2.getvalue(i)));
    	return temparray;
    }
    

  3. Given the following code segment, rewrite it to include the this pointer.
    class pwr 
    {
    	double b;
    	int e;
    	double val;
      public:
    	pwr(double base, int exp);
    	double get_pwr() {return this->val;}
    };
    pwr::pwr(double base, int exp)
    {
    	this->b=base;
    	this->e=exp;
    	this->val=1;
    	if (exp==0) return;
    	for( ; exp>0; exp--) 
    		this->val=this->val * this->b;
    }
    

  4. Why do overloaded operators which are defined as member functions only have one parameter, whereas those defined outside of the class have two parameters?

    Because, member functions are implicitly passed a this pointer. This gives them access to the data members of the object that invoked the member function.

    By contrast, functions defined outside of the class, do not have a this pointer, and therefore need two objects explicitly passed to them.

  5. In the lab exercise, you overloaded the << operator. Why would you use os instead of cout in the body of function?

    This is because your overloaded << operator will also work for file output. For instance, if you were sending everything to os, you should be able to include the following lines of code in your program and have it work:

    
    #include <fstream> 
    .
    .(definition of class myarray)
    .
    .(definition of ostream& operator<<(ostream& os, myarray array))
    .
    int main()
    {
    	myarray array1, array2, array3;
    
    	
            ofstream outData;                       // declares output stream
    
            // binds program variable outData to file "Data.Out"
            outData.open("Data.Out");
    	
    	//INITIALIZE ARRAYS
    	.
    	.
    	//ADD ARRAYS
    	.
    	.
    	
    	outdata << array1 << array2 << array3;
    	
    
    This prints the three arrays to the ostream which, in this case, is to the "Data.Out" file.

Back to Exercise click here
Back to Operator Overloading Lab click here

CS Dept Home Page
CS Dept Class Files
CS210 Class Files

Copyright: Department of Computer Science, University of Regina.