Question

In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we...

In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features:

a. Create a base class named rectangleType with following private data members:

  • length with double type
  • width with double type

b. rectangleType class should contain the following functions:

  • • 2 constructors (one for default and another to initialize the two private data members mentioned above)
  • • get methods (getLength() and getWidth()) for each private data members
  • • setDimension() method with two arguments: length and width) to set the private data members
  • • functions to overload the operators +, -, *, ++, -- for manipulation of the private data members.
  • • functions to overload the operators ==, !=, <=, <, >=, > for comparison of different rectangleType objects
  • • overload stream insertion and extraction operators in order to print information out onto the screen

c. Create a derived class named boxType which inherits the functionality from based class, rectangleType, but has its own data member and functions to represent a box.

  • height with double type
  • • 2 constructors (one for default and another to initialize the three private data members of the base class and the derived class)
  • • get method (getHeight) for the derived class’s data member
  • • overload the setDimension() method to set the data members of the base class, rectangleType, as well as its own data member (hint: setDimension(double l, double w, double h) where l=length, w=width, and h=height)
  • • override the area() method as necessary to work with the derived class
  • • add a new method called volume() to return the volume of the box (using base class’s private data members, length & width, with derived class’s private data member, height)
  • • functions to overload the operators +, -, *, ++, --, ==, !=, <=, <, >=, > for the derived class

d. Create a caller (where main() is located) file. In this file, create 2 boxType objects and execute all the overloaded operators mentioned above. Use the code below:

int main()

{

boxType box1(12, 9, 6);

boxType box2(8, 7, 5);

boxType box3, box4, box5;

cout << "box1: " << box1 << endl;

cout << "box2: " << box2 << endl;

box3 = box1 + box2;

cout << "box3: " << box3 << endl;

box4 = box1 - box2;

cout << "box4: " << box4 << endl;

box5 = box1 * box2;

cout << "box5: " << box5 << endl;

if (box1 > box2)

cout << "Volume of box1 is greater than the volume of box2." << endl;

else

cout << "Volume of box1 is less than or equal to the volume of box2." << endl;

box1++;

cout << "After increment the length, width, and height of "

<< "box1 by one unit, \nbox1: "

<< box1 << endl;

box3 = ++box2;

cout << "New dimension of box2: " << box2 << endl;

cout << "New dimension of box3: " << box3 << endl;

return 0;

}

Format your output with two decimal places.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
using namespace std;
class rectangleType
{
private :
double width;
double length;
public :
rectangleType()
{
this->width=0;
this->length=0;
}
rectangleType(double width,double length)
{
this->width=width;
this->length=length;
}
double getWidth()
{
return width;
}
double getLength()
{
return length;
}
void setWidth(double width)
{
this->width=width;
}
void setLength(double length)
{
this->length=length;
}

void setDimension(double length,double width)
{
this->length=length;  
this->width=width;
   }
rectangleType operator+ (rectangleType & r)
{
double wid=width+r.width;
double ht=length+r.length;
rectangleType rect(wid,ht);
return rect;
}
rectangleType operator- (rectangleType & r)
{
rectangleType rect;
double wid=width-r.width;
double ht=length-r.length;
if(wid<0 || ht<0)
{
cout<<"** Dimensions will not be negative **"<<endl;
}
else
{
rect.setWidth(wid);
rect.setLength(ht);
}
  
return rect;
}
rectangleType operator* (rectangleType & r)
{
double wid=width*r.width;
double ht=length*r.length;
rectangleType rect(wid,ht);
return rect;
}
bool operator== (rectangleType & r)
{
if( area()==r.area())
return true;
else
return false;
}
bool operator!= (rectangleType & r)
{
if(area()!=r.area())
return true;
else
return false;
}
  
bool operator<= (rectangleType & r)
{
if(area()<=r.area())
return true;
else
return false;
}
bool operator>= (rectangleType & r)
{
if(area()>=r.area())
return true;
else
return false;
}
bool operator< (rectangleType & r)
{
if(area()<r.area())
return true;
else
return false;
}
bool operator> (rectangleType & r)
{
if(area()>r.area())
return true;
else
return false;
}
  
  
  
  
friend ostream & operator << (std::ostream & dout, const rectangleType & right)
{
dout<<"Width = "<<right.width<<", length = "<<right.length<<endl;
return dout;
}
friend istream & operator >> (std::istream & in, rectangleType & right)
{
cout<<"Enter Width :";
in>>right.width;
cout<<"Enter length :";
in>>right.length;
return in;
}
virtual double area()
{
return width*length;
}
  
// Prefix operator(increment)
rectangleType operator++()
{
double l=++length;
double w=++width;

rectangleType rt(l,w);
return rt;
}

//Operator postfix (increment)   
rectangleType operator++(int)
{
rectangleType old(*this);
rectangleType rect(old.length++,old.width++);
return rect;
}

// Prefix operator(decrement)
rectangleType operator--()
{
double l=--length;
double w=--width;

rectangleType rt(l,w);
return rt;
}

//Operator postfix (decrement)   
rectangleType operator--(int)
{
rectangleType old(*this);
rectangleType rect(old.length--,old.width--);
return rect;
}

};
  
class boxType : public rectangleType
{
private :
   double height;
  
public :  
boxType():rectangleType()
{
    this->height=0;
   }
   boxType(double length,double width,double height):rectangleType(length,width)
{
    this->height=height;
   }
   double getHeight()
   {
      return height;
   }
   void setHeight(double ht)
   {
      this->height=ht;
   }
   void setDimension(double length,double width,double height)
{
rectangleType::setDimension(length,width);
this->height=height;
   }
     
double volume()
{
return getLength()*getWidth()*getHeight();
}
  
double area()
{
return 2*(getLength()*getWidth()+getWidth()*getHeight()+getHeight()*getLength());
}
  
boxType operator+ (boxType & r)
{
double wid=getWidth()+r.getLength();
double len=getLength()+r.getWidth();
double ht=getHeight()+r.height;
  
boxType bt(len,wid,ht);
return bt;
}
boxType operator- (boxType & r)
{
boxType bt;
double wid=getWidth()-r.getWidth();
double len=getLength()-r.getLength();
double ht=getHeight()-r.height;
if(wid<0 || ht<0 || len<0)
{
cout<<"** Dimensions will not be negative **"<<endl;
}
else
{
bt.setWidth(wid);
bt.setLength(len);
bt.setHeight(ht);
}
  
return bt;
}
boxType operator* (boxType & r)
{
double wid=getWidth()*r.getWidth();
double len=getLength()*r.getLength();
double ht=getHeight()*r.height;
  
boxType bt(len,wid,ht);
return bt;
}
bool operator== (boxType & r)
{
if( area()==r.area())
return true;
else
return false;
}
bool operator!= (boxType & r)
{
if( area()!=r.area())
return true;
else
return false;
}
  
bool operator<= (boxType & r)
{
if(area()<=r.area())
return true;
else
return false;
}
bool operator>= (boxType & r)
{
if(area()>=r.area())
return true;
else
return false;
}
bool operator< (boxType & r)
{
if(area()<r.area())
return true;
else
return false;
}
bool operator> (boxType & r)
{
if(area()>r.area())
return true;
else
return false;
}
  
  
  
  
friend ostream & operator << (std::ostream & dout, boxType & right)
{
dout<<"Length = "<<right.getLength()<<", Width = "<<right.getWidth()<<", Height = "<<right.height<<endl;
return dout;
}
friend istream & operator >> (std::istream & in, boxType & right)
{
   double l,w,h;
   cout<<"Enter length :";
in>>l;
cout<<"Enter Width :";
in>>w;
cout<<"Enter Height :";
in>>h;
right.setLength(l);
right.setWidth(w);
right.setHeight(h);
  
return in;
}

// Prefix operator(increment)
boxType operator++()
{
double l=getLength();
double w=getWidth();
double h=getHeight();
  

boxType bt(++l,++w,++h);
return bt;
}

//Operator postfix (increment)   
rectangleType operator++(int)
{
boxType old(*this);
double l=old.getLength();
double w=old.getWidth();
double h=height;
boxType bt(l++,w++,h++);
return bt;
}

// Prefix operator(decrement)
boxType operator--()
{
double l=getLength();
double w=getWidth();
double h=height;
  

boxType bt(--l,--w,--h);
return bt;
}

//Operator postfix (decrement)   
boxType operator--(int)
{
boxType old(*this);
double l=old.getLength();
double w=old.getWidth();
double h=height;
boxType bt(l--,w--,h--);
return bt;
}
  
     
};
  
int main()
{
       //setting the precision to two decimal places
   std::cout << std::setprecision(2) << std::fixed;
     
boxType box1(12, 9, 6);

boxType box2(8, 7, 5);

boxType box3, box4, box5;

cout << "box1: " << box1 << endl;

cout << "box2: " << box2 << endl;

box3 = box1 + box2;

cout << "box3: " << box3 << endl;

box4 = box1 - box2;

cout << "box4: " << box4 << endl;

box5 = box1 * box2;

cout << "box5: " << box5 << endl;

if (box1 > box2)

cout << "Volume of box1 is greater than the volume of box2." << endl;

else

cout << "Volume of box1 is less than or equal to the volume of box2." << endl;

box1++;

cout << "After increment the length, width, and height of "

<< "box1 by one unit, \nbox1: "

<< box1 << endl;

box3 = ++box2;

cout << "New dimension of box2: " << box2 << endl;

cout << "New dimension of box3: " << box3 << endl;
return 0;
}


===============================================

Output:

C:\Program Files (x86)\Dev-Cpp\MinGW64\bin\rectangleTypeBoxType.exe box1: Length = 9.00, Width 12.00, Height = 6.00 box2: Len

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleTyp...

    This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

  • Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default...

    Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default constructor -- that calls the default super class constructor and sets the default height to 0- Overloaded constructor with three parameters (length, width and height) – that calls the two parameterized super class constructor passing in length and width passed and sets the height to passed height.- setDimension method with three parameters (length, width, height) – call the super class setDimension and pass in...

  • Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: --------------------...

    Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: ------------------------------------------------------------------------------------------------------------------------------------------ main.cpp #include #include "rectangleType.h" using namespace std; // part e int main() { rectangleType rectangle1(10, 5); rectangleType rectangle2(8, 7); rectangleType rectangle3; rectangleType rectangle4; cout << "rectangle1: " << rectangle1 << endl; cout << "rectangle2: " << rectangle2 << endl; rectangle3 = rectangle1 + rectangle2;    cout << "rectangle3: " << rectangle3 << endl; rectangle4 = rectangle1 * rectangle2;    cout << "rectangle4: " << rectangle4 << endl; if (rectangle1 > rectangle2) cout << "Area...

  • Question 3: Q3 (Polymorphism & virtual function) a) Consider the class, derived class, and the virtual functions as shown in Display for Q3. Create a derived class (of Sale) called 'Mailorder...

    Question 3: Q3 (Polymorphism & virtual function) a) Consider the class, derived class, and the virtual functions as shown in Display for Q3. Create a derived class (of Sale) called 'MailorderSale' having one private member variable called 'shipping charge'. It should have constructor function and virtual function 'bill' (this function adds shipping charge to the price). Define all these functions (no need of default constructors). b) In the main function, define one object of DiscountSale with (11,10) initial values and...

  • (Classes and Objects) Design a class named Box. The class should have the following private members:...

    (Classes and Objects) Design a class named Box. The class should have the following private members: width - A double member variablethat holds the width of the box. length – A double member variable for holding the length of the box. height – A double member variable of type double that holds the height of the box. In addition, provide the following member functions with full (inline) implementation. a) A default constructor that sets all member variables to 0. b)...

  • Long Answer 2: (Classes and Objects) Design a class named Box. The class should have the...

    Long Answer 2: (Classes and Objects) Design a class named Box. The class should have the following private members: width - A double member variablethat holds the width of the box. length – A double member variable for holding the length of the box. height – A double member variable of type double that holds the height of the box. In addition, provide the following member functions with full (inline) implementation. a) A default constructor that sets all member variables...

  • c++ Part 1 Consider using the following Card class as a base class to implement a...

    c++ Part 1 Consider using the following Card class as a base class to implement a hierarchy of related classes: class Card { public: Card(); Card (string n) ; virtual bool is_expired() const; virtual void print () const; private: string name; Card:: Card() name = ""; Card: :Card (string n) { name = n; Card::is_expired() return false; } Write definitions for each of the following derived classes. Derived Class Data IDcard ID number CallingCard Card number, PIN Driverlicense Expiration date...

  • Define an example of inheritance with a base class that has 2 attributes, 2 derived classes...

    Define an example of inheritance with a base class that has 2 attributes, 2 derived classes that each have 2 attributes. Implement this in C++. Create a driver program that creates instances of both of the derived classes and exercises the classes’ member functionsl. Student answer should include the following: 1 base class with 2 attributes, 2 constructors (default and with parameters), get/set methods for each attribute, and display methods 2 derived classes with 2 attributes, 2 constructors (default and...

  • I need help solving this basic C++ question. I need help fixing my code.   Question: Design...

    I need help solving this basic C++ question. I need help fixing my code.   Question: Design a class called a box that represents a box. Box classes have variables such as the length of the box, width, and height. 1. Member variables shall be dedicated members. 2. Define the creator of the Box Class. The creator may receive all of the data and may not receive any. 3. Add accessor and creator 4. Add an empty function, which indicates whether...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT