C++ program to demonstrate Constructor

//class Rectangle with private data members length and breadth
//use object of the class to calculate area of triangle

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

No comments:

Post a Comment