Answer to Question #319760 in C++ for Daiki

Question #319760

Write a class rectangle and store the length and width of the rectangle. Write a member function called increment that will increment to length and width. Also write the function to find the area of a rectangle and then write the accessor function that will display the length, width and area of the rectangle. Demonstrate the use of the object in the main function.

class rectangle

{

private:

int length, width, area;

public:

}


1
Expert's answer
2022-03-28T13:52:25-0400
#include <iostream>
using namespace std;


class rectangle
{
private:
    int length, width, area;


public:
    rectangle(int l, int w);
    void calculate_area();
    void increment(int dl=1, int dw=1);
    int get_length();
    int get_width();
    int get_area();
};


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


void rectangle::calculate_area() {
    area = length * width;
}
void rectangle::increment(int dl, int dw) {
    length += dl;
    width += dw;
    calculate_area();
}


int rectangle::get_length() {
    return length;
}


int rectangle::get_width() {
    return width;
}


int rectangle::get_area() {
    return area;
}


int main() {
    rectangle r(3, 4);
    cout << "Rectangle " << r.get_length() << "*" << r.get_width()
         << " has area " << r.get_area() << endl;


    r.increment();
    cout << endl << "After incremation" << endl;
    cout << "Rectangle " << r.get_length() << "*" << r.get_width()
         << " has area " << r.get_area() << endl;
    
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog