Create a C++ class called "Rectangle" with private attributes width and height, and public member functions to set and get these attributes. Include a function getArea() that calculates and returns the area of the rectangle.

Recommended Answers

All 4 Replies

You wouldn't expect to get ripped by getting someone else to go to the gym for you. You won't learn how to program by getting someone else to do your homework.

But if you are having a particular problem, please post your code and an explanation of the problem and perhaps we can offer some help.

You are probably getting marked as a student or paid to complete this task, we can thus not help you. You need to show us what you have done so far, where you are stuck or what kind of errors do you get.

Less games and more work :)

If you want to get answer without studying, you may ask chatGPT.

Here is the C++ code for a class called "Rectangle" with private attributes width and height, and public member functions to set and get these attributes.  Additionally, it has a function called getArea() that computes and returns the rectangle's area:

class Rectangle {
 private:
  int width;
  int height;

 public:
  Rectangle() {
    width = 0;
    height = 0;
  }

  void setWidth(int width) {
    this->width = width;
  }

  int getWidth() {
    return width;
  }

  void setHeight(int height) {
    this->height = height;
  }

  int getHeight() {
    return height;
  }

  int getArea() {
    return width * height;
  }
};

This class is well-defined and easy to use. It has private attributes for the width and height of the rectangle, and public member functions to set and get these attributes. It also has a function getArea() that calculates and returns the area of the rectangle.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.