C++ Program to calculate the area using classes

310 views

Using one class and one object.

#include<iostream> using namespace class Rectangle { int width, height; public void set_values (int,int); int area() {return width*height;} }; void Rectangle::set_values ( int x, int y) { width = x; height = y; } int main () { Rectangle rect; rect.set_values (3,4); cout<<"area: "<< rect.area(); return 0; }
Code language: C++ (cpp)

Output

<samp>area: 12</samp>
Code language: HTML, XML (xml)

Using one class ,two objects

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

Output

<samp>rect area: 12 </samp> <samp>rectb area: 30 </samp>
Code language: C++ (cpp)

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