Write a program that calculates the area and keeps a count that how many times it has
calculated the area.
Page 4
1. Create a class area which has data members of length, width and area (int type).
2. It also has a static int member count, initialize it with zero.
3. Overload constructor of class and calculate area in it, pass the values of length and width
from main.(calculate the area three times).4. Create a static member function in class which tells that how many times area has been
calculated. (This function will return the value of count when called)
5. Write function of display and display the area along with the value of count
#include <iostream>
class Area {
public:
    Area(int length, int width)
        : length_ (length)
        , width_(width)
    {
        Calculate();
    }
    void Calculate() {
        Area::count++;
        area_ = width_ * length_;
    }
    static int Count() {
        return Area::count;
    }
    void Display() const {
        std::cout << "Area: " << area_ << " Count: " << Area::Count() << "\n";
    }
private:
    static int count;
    int length_;
    int width_;
    int area_;
};
int Area::count = 0;
int main() {
    Area a1(10, 4);
    Area a2(20, 4);
    Area a3(30, 4);
    a1.Display();
    a2.Display();
    a3.Display();
    return 0;
}
Comments
Leave a comment