Question

In: Computer Science

For some reason I am having a hard time getting this program to compile properly. Could...

For some reason I am having a hard time getting this program to compile properly. Could you help me debug it? Write the prototypes and functions to overload the given operators in the code

//main.cpp

//This program shows how to use the class rectangleType.
#include <iostream>
#include "rectangleType.h"
using namespace std;
int main() {
    rectangleType rectangle1(23, 45);                     //Line 1
    rectangleType rectangle2(12, 10);                     //Line 2
    rectangleType rectangle3;                             //Line 3
    rectangleType rectangle4;                             //Line 4

    cout << "Line 5: rectangle1: ";                       //Line 5
    rectangle1.print();                                   //Line 6
    cout << endl;                                         //Line 7

    cout << "Line 8: rectangle2: ";                       //Line 8
    rectangle2.print();                                   //Line 9
    cout << endl;                                         //Line 10

    rectangle3 = rectangle1 + rectangle2;                 //Line 11
    cout << "Line 12: rectangle3: ";                      //Line 12
    rectangle3.print();                                   //Line 13
    cout << endl;                                         //Line 14

    rectangle4 = rectangle1 * rectangle2;                 //Line 15
    cout << "Line 16: rectangle4: ";                      //Line 16
    //print rectangl through << stream extraction operator; //Line 17
     cout << rectangle4;
     cout << endl;                                         //Line 18

    if (rectangle1 == rectangle2)                         //Line 19
        cout << "Line 20: rectangle1 and "
             << "rectangle2 are equal." << endl;          //Line 20
    else                                                  //Line 21
        cout << "Line 22: rectangle1 and "
             << "rectangle2 are not equal."
             << endl;                                      //Line 22
    return 0;
}

//rectangleType.h

#include <iostream>
using namespace std;

class rectangleType {

//Overload the stream insertion and extraction operators

public:
    void setDimension(double l, double w);
    double getLength() const;
    double getWidth() const;
    double area() const;
    double perimeter() const;
    void print() const;
//Overload the operator +

//Overload the operator *

//Overload the operator ==

//Overload the operator !=
    rectangleType();
    rectangleType(double l, double w);
private:
    double length;
    double width;
};

//rectangeType.cpp

#include "rectangleType.h"

void rectangleType::setDimension(double l, double w)
{
    if (l >= 0)
        length = l;
    else
        length = 0;
    if (w >= 0)
        width = w;
    else
        width = 0;
}
double rectangleType::getLength() const
{
    return length;
}
double rectangleType::getWidth()const
{
    return width;
}
double rectangleType::area() const
{
    return length * width;
}
double rectangleType::perimeter() const
{
    return 2 * (length + width);
}
void rectangleType::print() const
{
    cout << "Length = " << length
         << "; Width = " << width;
}
rectangleType::rectangleType(double l, double w)
{
    setDimension(l, w);
}

rectangleType::rectangleType()
{
    length = 0;
    width = 0;
}


rectangleType rectangleType::operator+ (const rectangleType& rectangle) const
{
    //to-do:Task 1
}

rectangleType rectangleType::operator* (const rectangleType& rectangle) const
{
    //to do: Task 2
}

bool rectangleType::operator== (const rectangleType& rectangle) const
{
    //to-do: Task 3
}

bool rectangleType::operator!= (const rectangleType& rectangle) const
{
   //to-do: Task 4
}

ostream& operator<< (ostream& osObject, const rectangleType& rectangle)
{
    //to-do: Task 5
}

Solutions

Expert Solution

Program Code [C++]

rectangleType.h

#include <iostream>
using namespace std;

class rectangleType {

//Overload the stream insertion and extraction operators

friend ostream& operator<<(ostream& osObject, const rectangleType& rectangle);

public:
void setDimension(double l, double w);
double getLength() const;
double getWidth() const;
double area() const;
double perimeter() const;
void print() const;

   // Writing prototypes for overloading given operators in statement
  
   //Overload the operator +

   rectangleType operator+(const rectangleType& rectangle) const;
  
   //Overload the operator *

   rectangleType operator*(const rectangleType& rectangle) const;

   //Overload the operator ==

   bool operator==(const rectangleType& rectangle) const;

   //Overload the operator !=

   bool operator!=(const rectangleType& rectangle) const;

rectangleType();
rectangleType(double l, double w);

private:
double length;
double width;
};

rectangleType.cpp

#include "rectangleType.h"

void rectangleType::setDimension(double l, double w)
{
if (l >= 0)
length = l;
else
length = 0;
if (w >= 0)
width = w;
else
width = 0;
}
double rectangleType::getLength() const
{
return length;
}
double rectangleType::getWidth()const
{
return width;
}
double rectangleType::area() const
{
return length * width;
}
double rectangleType::perimeter() const
{
return 2 * (length + width);
}
void rectangleType::print() const
{
cout << "Length = " << length
<< "; Width = " << width;
}
rectangleType::rectangleType(double l, double w)
{
setDimension(l, w);
}

rectangleType::rectangleType()
{
length = 0;
width = 0;
}


rectangleType rectangleType::operator+ (const rectangleType& rectangle) const
{
   rectangleType rec;
   rec.length = this->length + rectangle.length;
   rec.width = this->width + rectangle.width;
  
   return rec;
}

rectangleType rectangleType::operator* (const rectangleType& rectangle) const
{
   rectangleType rec;
   rec.length = this->length * rectangle.length;
   rec.width = this->width * rectangle.width;

   return rec;
}

bool rectangleType::operator== (const rectangleType& rectangle) const
{
   return ((this->length == rectangle.length) && (this->width == rectangle.width));
}

bool rectangleType::operator!= (const rectangleType& rectangle) const
{
return ((this->length != rectangle.length) || (this->width != rectangle.width));
}

ostream& operator<< (ostream& osObject, const rectangleType& rectangle)
{
   osObject << "Length = " << rectangle.getLength() << "; Width = " << rectangle.getWidth();
}

main.cpp

//This program shows how to use the class rectangleType.
#include <iostream>
#include "rectangleType.h"
using namespace std;
int main() {
rectangleType rectangle1(23, 45); //Line 1
rectangleType rectangle2(12, 10); //Line 2
rectangleType rectangle3; //Line 3
rectangleType rectangle4; //Line 4

cout << "Line 5: rectangle1: "; //Line 5
rectangle1.print(); //Line 6
cout << endl; //Line 7

cout << "Line 8: rectangle2: "; //Line 8
rectangle2.print(); //Line 9
cout << endl; //Line 10

rectangle3 = rectangle1 + rectangle2; //Line 11
cout << "Line 12: rectangle3: "; //Line 12
rectangle3.print(); //Line 13
cout << endl; //Line 14

rectangle4 = rectangle1 * rectangle2; //Line 15
cout << "Line 16: rectangle4: "; //Line 16
//print rectangl through << stream extraction operator; //Line 17
cout << rectangle4;
cout << endl; //Line 18

if (rectangle1 == rectangle2) //Line 19
cout << "Line 20: rectangle1 and "
<< "rectangle2 are equal." << endl; //Line 20
else //Line 21
cout << "Line 22: rectangle1 and "
<< "rectangle2 are not equal."
<< endl; //Line 22
return 0;
}

Sample Output:-

----------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!


Related Solutions

I am having a hard time getting started on how to do this assignment. I would...
I am having a hard time getting started on how to do this assignment. I would like some ideas on how to start the MEMO. and maybe some ideas on what you would include. But I don't necessarily need the assignment completed for me. just need ideas!!! One routine negative message in the form of an internal memo. 400-500 word message. Single-spaced, with 1-inch margins on all sides. Objective: To announce organizational restructuring, one routine negative message addressed to employees....
I am having a hard time getting my an output after putting in the second set...
I am having a hard time getting my an output after putting in the second set of functions and I was hoping to be able to have the results from the functions be rounded to 2 decimal places. <html> <head> <title>Length Conversion</title> <script language='JavaScript' type='text/JavaScript'> <!-- function validate(type) { if(document.my_form.textinput.value=='') { alert('Fill the Input box before submitting'); return false; }else{ if(type=="to_feet"){ var res=3.2808*document.my_form.textinput.value; var unit=" feet"; }else{ var res=0.3048*document.my_form.textinput.value; var unit=" meter"; } document.getElementById("result").innerHTML=res.toFixed(2) + unit; return false; } }...
I am having trouble with my assignment and getting compile errors on the following code. The...
I am having trouble with my assignment and getting compile errors on the following code. The instructions are in the initial comments. /* Chapter 5, Exercise 2 -Write a class "Plumbers" that handles emergency plumbing calls. -The company handles natural floods and burst pipes. -If the customer selects a flood, the program must prompt the user to determine the amount of damage for pricing. -Flood charging is based on the numbers of damaged rooms. 1 room costs $300.00, 2 rooms...
I am taking a Data Structures and Program Design class. BUT, I am having trouble getting...
I am taking a Data Structures and Program Design class. BUT, I am having trouble getting access to the vectors so that I can compare the guesses with the random numbers. I’ve been working on this for several days and don't feel any closer to a solution and could really use some help as I really don't know how to get this to work. Thank you! The assignment is: Design an ADT for a one-person guessing game that chooses 4...
I understand the answer to this I am just having a hard time creating a graph...
I understand the answer to this I am just having a hard time creating a graph for it. Bill the butcher is upset because the government plans to tax beef $.10 a pound. "I hate paying taxes," he says. "Because of this, I'm raising all my beef prices by $.10 a pound. The consumers will bear this burden, not me." Do you see anything wrong with this way of thinking? Explain. Draw a graph describing your answer and attach it...
I have figured out this assignment, but I am having a hard time figuring out the...
I have figured out this assignment, but I am having a hard time figuring out the extra credit, Thanks! Creating a Rectangle class We have created a representation of a rectangle in C++ and extended it to create multiple representation's of rectangles. Classes can simplify that process considerably. In this assignment, your task is to convert the representation of a rectangle we created earlier in the semester to a Rectangle class. The Rectangle class shall consist of the following: The...
I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
can someone explain to me what osmolality is.? i am having a hard time understanding it
can someone explain to me what osmolality is.? i am having a hard time understanding it
I am having a hard time understanding these two questions. If someone can explain that would...
I am having a hard time understanding these two questions. If someone can explain that would be great. 1) Explain the steps that enables body to metabolize fat to ATP? 2) Write out the amount of ATP, NADH, FADH2 produced in each step of cellular metabolism of a glucose.
I am having a hard time writing these SQL queries. Please specify the following queries in...
I am having a hard time writing these SQL queries. Please specify the following queries in SQL on the database schema shown in the figure below. STUDENT Name StudentNumber Class Major Smith 17 1 CS Brown 8 2 CS Kathy 15 1 EE COURSE CourseName CourseNumber CreditHours Department Intro to Computer Science CSE110 4 CS Data Structures CSE205 4 CS Discrete Mathematics MAT240 3 MATH Databases CSE380 3 CS Analog Circuits EE260 3 EE SECTION SectionIdentifier CourseNumber Semester Year Instructor...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT