Question

In: Computer Science

Using the program below, overload the Multiply operator to the Rectangle class… this will multiple the...

  1. Using the program below, overload the Multiply operator to the Rectangle class… this will multiple the widths for the 2 Rectangles and multiple the lengths of the two rectangles (similar to how the “+” operator added the widths and lengths of the 2 rectangles). Overload this operator using the non-member method. Please copy-paste code into MS Word document and then show some screenshots of you running and testing this method using your IDE.

  1. Using the program below, overload the Divide operator to the Rectangle class… this will divide the widths for the 2 Rectangles and divide the lengths of the two rectangles (similar to how the “+” operator added the widths and lengths of the 2 rectangles). Overload this operator using the class member method. Please copy-paste code into MS Word document and then show some screenshots of you running and testing this method using your IDE.


// Overloaded operator is a Rectangle member function

#include <iostream > 
using namespace std ; 
/************ Declaration Section ******************/ 
class Rectangle{ 
  private : 
    int width , length ; 
 
  public : 
    Rectangle (); 
    Rectangle ( int w, int l ); 
    void printArea (); 
    void displayInfo (); 
    Rectangle operator +(Rectangle& r ); 
}; 

/************ Implementation Section ******************/ 
Rectangle :: Rectangle () { width = 1, length = 2; } 
Rectangle :: Rectangle ( int w, int l ) { width = w, length = l ; } 

void Rectangle :: printArea () { 
 cout <<"Area of "<< this <<" = "<< width*length << endl ; 
} 

void Rectangle :: displayInfo () { 
 cout << "I am a " << width <<"x"<< length <<" rectangle " 
      << "with id " << this << endl ; 
} 

Rectangle Rectangle :: operator +( Rectangle& r ) { 
  Rectangle temp ; 
//  cout << "This " << this->width << "  " << this->length << endl;;
//  cout << "argument r: "<< r.width << "  " << r.length << endl;
  temp.width = this->width + r.width ; 
  temp.length = this->length + r.length ; 
  
  return temp; 
} 

/***************** Main Section *********************/ 
int main (){ 
  Rectangle r1 , r2 (5 ,7) , r3 ; 
  r1 . displayInfo (); 
  r2 . displayInfo (); 
  r3 . displayInfo (); 

//  r1 = r1 + r2;
//  r1.displayInfo();

  r3=r1+r2 ; 
  r3 . displayInfo (); 
 
  r1 . printArea (); 
  r2 . printArea (); 
  r3 . printArea (); 
  
  return 0; 
}

Solutions

Expert Solution

/*C++ program that overloads the (*) operator as non-member function and (/) as a member function of the Rectangle class.*/

//Demonstrate the class in the main function

//Rectangle.cpp

#include <iostream >
using namespace std ;
//Rectangle class
class Rectangle
{
private :
   int width , length ;

public :
   Rectangle ();
   Rectangle ( int w, int l );
   void printArea ();
   void displayInfo ();
   Rectangle operator +(Rectangle& r );
   //non-member friend function
   friend Rectangle operator + (Rectangle &r1, const Rectangle& r2);

   //non-member friend function
   friend Rectangle operator * (Rectangle &r1, const Rectangle& r2);
   //member friend function
   Rectangle operator / (Rectangle &r);


}; //end of the class ,Rectangle

//non-member friend function to overload addition (+) operator
Rectangle operator + (Rectangle &r1, const Rectangle& r2)
{
   Rectangle add;
   add.length=r1.length+r2.length;
   add.width=r1.width+r2.width;
   return add;
}

//non-member friend function to overload multiply (*) operator
Rectangle operator * (Rectangle &r1, const Rectangle& r2)
{
   Rectangle multiply;
   multiply.length=r1.length*r2.length;
   multiply.width=r1.width*r2.width;
   return multiply;
}


//member friend function to overload division (/) operator
Rectangle Rectangle ::operator / (Rectangle &r)
{
   Rectangle temp;
   temp.length=length/r.length;
   temp.width=width/r.width;
   return temp;
}

/************ Implementation Section ******************/
Rectangle :: Rectangle ()
{
   width = 1, length = 2;
}

Rectangle :: Rectangle ( int w, int l )
{
   width = w, length = l ;
}

void Rectangle :: printArea ()
{
   cout <<"Area of "<< this <<" = "<< width*length << endl ;
}

void Rectangle :: displayInfo ()
{
   cout << "I am a " << width <<"x"<< length <<" rectangle "
       << "with id " << this << endl ;
}

Rectangle Rectangle :: operator +( Rectangle& r )
{
   Rectangle temp ;
   temp.width = this->width + r.width ;
   temp.length = this->length + r.length ;

   return temp;
}

/***************** Main Section *********************/
int main ()
{


   Rectangle r1(2,4) , r2 (2 ,4) , r3 ;
   r1 . displayInfo ();
   r2 . displayInfo ();
   r3 . displayInfo ();

   cout<<"Overloading mutltiply(*) non- member operator "<<endl;
   r3 = r1 * r2;
   r3.displayInfo();

  
   cout<<"Overloading division(/) member operator "<<endl;
   r3=r1/r2;
   r3.displayInfo();

   cin.get();
   cin.ignore();
   return 0;
}

Sample output screenshot:


Related Solutions

In class we discussed how to overload the + operator to enable objects of type Fraction to be added together using the + operator.
2.(a) Fraction Operators Objective:In class we discussed how to overload the + operator to enable objects of type Fraction to be added together using the + operator. Extend the Fraction class definition so that the -, * and / operators are supported. Write a main function that demonstrates usage of all of these operators.(b)More Custom Types Objective:Define a set of classes that represent a Schedule and Course in the context of a university student who has a schedule with a...
Complete the following: Extend the newString class (attached) to include the following: Overload the operator +...
Complete the following: Extend the newString class (attached) to include the following: Overload the operator + to perform string concatenation. Overload the operator += to work as shown here: s1 = "Hello " s2 = "there" s1 += s2 // Should assign "Hello there" to s1 Add a function length to return the length of the string. Write a test program. //myString.h (header file) //Header file myString.h    #ifndef H_myString #define H_myString #include <iostream> using namespace std; class newString {...
C++ Class involving Set intersection The goal is to overload the function: void Bag::operator/=(const Bag& a_bag)...
C++ Class involving Set intersection The goal is to overload the function: void Bag::operator/=(const Bag& a_bag) // Bag<int> bag1 = 1,2,3,4 //Bag<int> bag2 = 2,5,6,7 bag1+=bag2; bag1.display() should return 2 //Since type is void, you should not be returning any array but change the bag itself. #include <iostream> #include <string> #include <vector> using namespace std; template<class T> class Bag { public: Bag(); int getCurrentSize() const; bool isEmpty() const; bool add(const T& new_entry); bool remove(const T& an_entry); /** @post item_count_ ==...
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle....
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area of...
C++ Class involving union. The goal is to overload the function: void Bag<T>::operator+=(const Bag<T>& a_bag) //...
C++ Class involving union. The goal is to overload the function: void Bag<T>::operator+=(const Bag<T>& a_bag) // The union of two sets A and B is the set of elements which are in A, in B, or in both A and B. For instance, Bag<int> bag1 = 1,2,3 and Bag<int> bag2 = 3,4,5 then bag1+=bag2 should return 1,2,3,4,5. //Since type is void, it should not return an array. #include <iostream> #include <string> #include <vector> using namespace std; template<class T> class Bag...
c++ E2b: Design a class named Rectangle to represent a rectangle. The class contains:
using c++E2b: Design a class named Rectangle to represent a rectangle. The class contains:(1) Two double data members named width and height which specifies the width and height of the rectangle .(2) A no-arg constructor that creates a rectangle with width 1 and height 1.(3) A constructor that creates a rectangle with the specified width and height .(4) A function named getArea() that returns the area of this rectangle .(5) A function named getPerimeter() that returns the perimeter of this...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
C++ Program Overload the following operators to work with the Rational class and add test cases...
C++ Program Overload the following operators to work with the Rational class and add test cases in the driver program. Make sure your driver program now tests each of these symbols for your Rational Class. + – * / == != < <= > >= << (stream insertion operator, use the toString function) >> (stream extraction operator) Make sure you test all 12 operators Example Run (Bold is input), everything else is a print statement using the overloaded operators Enter...
I want to indent this c++ program #include<iostream> using namespace std; class Rectangle{ private: double width;...
I want to indent this c++ program #include<iostream> using namespace std; class Rectangle{ private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; double getPerimeter() const; bool isSquare() const; }; void Rectangle::setWidth(double w){ width = w; } void Rectangle::setLength(double l){ length = l; } double Rectangle::getWidth() const{ return width; } double Rectangle::getLength() const{ return length; } // Added Method definitions double Rectangle::getArea() const{ return (width * length); } double Rectangle::getPerimeter() const{...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT