C++ program to illustrate Constructor

//class Rectangle with private data members length and breadth
//read length and breadth in main()
//supply them in parameterized constructor to initialize

#include <iostream>
using namespace std;
class Rectangle
{
    private:
        int l,b;
    public:
        Rectangle(int x,int y)
//parameterized constructor
        {
            l=x;
            b=y;
        }
        int Calculate()
        {
            int area;
            area=l*b;
            return area;
        }
};
int main()
{
    int ln,br;
    cout<<"Enter length and breadth: ";
    cin>>ln>>br;
    Rectangle r(ln,br);
    cout<<"The area is "<<r.Calculate();
    return 0;
}

No comments:

Post a Comment