C++ Program to implements Constructor

260 views

Here, we are initializing the parameter of rectangle to calculate area using constructor.

// class constructor #include <iostream> using namespace std; class Rectangle { int width, height; public: Rectangle (int, int); int area () { return (width * height); } }; Rectangle::Rectangle (int a, int b) { width = a; height = b; } int main () { Rectangle rect (3, 4); Rectangle rectb (5, 6); cout << "rect area: " << rect.area () << endl; cout << "rectb area: " << rectb.area () << endl; return 0; }
Code language: C++ (cpp)

Output

rect area: 12 rectb area: 30

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Enable Notifications    OK No thanks