Create a class `Rectangle` with data members `length` and `breadth`. Use a constructor to initialize values and include member functions to calculate and display area and perimeter.

SOLUTION....

Rectangle Class in C++ — Demo Page
#include <iostream>
using namespace std;

class Rectangle {
private:
    float length, breadth;

public:
    // Constructor to initialize values
    Rectangle(float l, float b) : length(l), breadth(b) {}

    // Function to calculate area
    float area() { return length * breadth; }

    // Function to calculate perimeter
    float perimeter() { return 2 * (length + breadth); }

    // Function to display details
    void display() {
        cout << "Rectangle Details:\n";
        cout << "Length: " << length << endl;
        cout << "Breadth: " << breadth << endl;
        cout << "Area: " << area() << endl;
        cout << "Perimeter: " << perimeter() << endl;
    }
};

int main() {
    float l, b;
    cout << "Enter length: ";
    cin >> l;
    cout << "Enter breadth: ";
    cin >> b;

    // Create object using constructor
    Rectangle rect(l, b);

    // Display rectangle details
    rect.display();

    return 0;
}

OUTPUT

Leave a Reply

Your email address will not be published. Required fields are marked *