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:
}
#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;
}
Comments
Leave a comment