Q: find the area of the rectangle using 2 objects
This is my code with out the 2 objects normal code
#include<iostream>
using namespace std;
class rectangle
{
int q,w ;
public :
void set_value (int e, int r)
{
q=e;
w=r;
}
int area()
{
return q*w ;
}
};
int main()
{
int t,y,u;
char i;
rectangle rect;// one opject
cout << "Enter a length:\n";
cin>>t;
cout << "Enter a width:\n " ;
cin >> y;
rect.set_value(t,y);
u = rect.area();
cout<< "area of the rectangle ="<<u;
cin>>i;
return 0;
}
then here i tried to do it with 2 objects but the output is strange :
#include<iostream>
using namespace std;
class rectangle
{
int q,w ;
public :
void set_value (int e)
{
q=e;
}
void set_value2 (int r)
{
w=r;
}
int area()
{
return q*w ;
}
};
int main()
{
int t,y,u;
char i;
rectangle rect1,rect2;// creating 2 objects
cout << "Enter a length:\n";
cin>>t;
cout << "Enter a width:\n " ;
cin >> y;
rect1.set_value(t);//call by object1
rect2.set_value(y);// call by object2
u = rect1.area();// i used any object
cout<< "area of the rectangle ="<<u;
cin>>i;
return 0;
}