Advertisement

C++ Program to find the area of a rectangle using parameterized constructor


In this program, You will learn how to find the area of a rectangle using a parameterized constructor in C++.


class Test{
   //statement
};

int main(){
  //statement;
}

Example: How to find the area of a rectangle using a parameterized constructor in C++

#include<iostream>
using namespace std;

class Test {
public:
    int l, w, area;

    Test(int length, int width) {
        l = length;
        w = width;
    }

    void findArea() {
        area = l * w;
    }

    void display() {
        cout << "Area of rectangle is:" << area;
    }
};

int main() {

    int length, width;

    cout << "Enter length of rectangle:";
    cin >> length;
    cout << "Enter width of rectangle:";
    cin>>width;

    Test obj(length, width);

    obj.findArea();
    obj.display();

    return 0;
}

Output:

Enter length of rectangle:12
Enter width of rectangle:2
Area of rectangle is:24