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;
     }

Recommended Answers

All 14 Replies

I think ur new to Object Orientated Programming because ur logic is wrong
Try understand understand little bit about object 1st

for example

class rectangle 
{
int a.b;
public:
void set_value(int t) {a=t}
void set_value2(int r) {b=t}
int area(){return a*b}

}

int main()
{
rectangle r1,r2;
}

when compiler execute

rectangle r1,r2;

it will create four variable namely

r1.a
r1.b

r2.a
r2.b

and now try to understand ur program logic according to my example
1st u set the value for
r1.set_value(t) //call by object r1
causes r1.a=t
r2.set_value(y) //call by object r2
causes r2.a=y
then
u=r1.area();
causes u=r1.a*r1.b;

r1.b=? this the region not getting the correct value for this variable

I hope it will help to understand the logic of ur program......and now try to solve ur problem

Best Of Luck.

By chance is this assignment related to inheritance? If so, it is possible to define 2 classes that work together to produce a shape such as a rectangle then determine the area.

Originally Fbody By chance is this assignment related to inheritance? If so, it is possible to define 2 classes that work together to produce a shape such as a rectangle then determine the area

Sorry i Didn't understand ur Question sir

Sorry i Didn't understand ur Question sir

I was asking the OP if the assignment is part of a chapter on inheritance. I suspect that the language the OP used is not the correct language for the question.

Depending on the assignment's requirements, it is possible to solve a question like this using inheritance.

May be u right but he use just a single class so I don't think about inheritance.

I think ur new to Object Orientated Programming because ur logic is wrong
Try understand understand little bit about object 1st

for example

class rectangle 
{
int a.b;
public:
void set_value(int t) {a=t}
void set_value2(int r) {b=t}
int area(){return a*b}

}

int main()
{
rectangle r1,r2;
}

when compiler execute

rectangle r1,r2;

it will create four variable namely

r1.a
r1.b

r2.a
r2.b

and now try to understand ur program logic according to my example
1st u set the value for
r1.set_value(t) //call by object r1
causes r1.a=t
r2.set_value(y) //call by object r2
causes r2.a=y
then
u=r1.area();
causes u=r1.a*r1.b;

r1.b=? this the region not getting the correct value for this variable

I hope it will help to understand the logic of ur program......and now try to solve ur problem

Best Of Luck.

Are you saying there are no such way to do it without creating 2 classes?

because i understood that if the functions in the same class then

each object will get its data , so i read the second post from Fbody

and i tried it and it worked good , but there are no other way to do it ?

And yes this is my second day to learn the c++ i started yesterday , i posted

some problems here and the guys here helped me , you too , thanks

Here what i did!

#include<iostream>
using namespace std;
int q;
int w;
class rectangle1
{
      
       
       
      public : 
             void set_value (int e)
           
             
                { 
                            q=e;
                          
                }
              
                        
                        };
  class rectangle2: public rectangle1 
  {
        
        public:
               void set_value2 (int r)
                  {
                       w=r;
                       }    
                        int area()
              
                {
                        return q*w ;
                        }
                       };    
                      
 int main()
 {
     int t,y,u;
     char i;
     rectangle1 rect1;
     rectangle2 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_value2(y);// call by object2
     u = rect2.area();// i used any object
     
     
     cout<< "area of the rectangle ="<<u;
     cin>>i;
     return 0;
     }

May be u right but he use just a single class so I don't think about inheritance.

Yes i want it with one class .. is is possible?

@Rayanjaha
No I dont say that............
It is possible but why u crate two object just use single object.
and then call ur function like this

r1.set_value(width);
r1.set_value2(length);
AREA=r1.area();

I don't why u use two object.

@Rayanjaha
No I dont say that............
It is possible but why u crate two object just use single object.
and then call ur function like this

r1.set_value(width);
r1.set_value2(length);
AREA=r1.area();

I don't why u use two object.

This is My assigment on the class that i am taking it ,

its says
find the area of the rectangle using 2 objects .

the problem is that question didnt say with 1 class or 2

so if it is imposible with 1 , then i think using inheritance it is

the right answer coz we took inheritance at the same time :)

@Rayanjaha
No I dont say that............
It is possible but why u crate two object just use single object.
and then call ur function like this

r1.set_value(width);
r1.set_value2(length);
AREA=r1.area();

I don't why u use two object.

Oh you said possible , what should i use to do it?

If you want to solve this question without inheritance then u have to change ur area function some thing like this

int area(rectangle temp){return a*temp.a}

..........
r1.set_value(l);
r2.set_value(w);
..............

and u have to call ur function like this

AREA=r1.area(r2);

Well, you obviously have your language correct. I wonder if it means to use 2 objects to represent a single area. The total area would then be the composite/combination of the 2 areas.
i.e.

declare variable, total area
create object 1
create object 2
total area = obj1.area() + obj2.area();
display total area

Your objects could either be 2 rectangles or 2 triangles (the area of a triangle is 1/2 the area of an identically-specified rectangle;);)).

The other possibility is that the prof wants one object to hold the data about the rectangle and another object that can perform operations on it.

EX.

class Rect
{
   double len, wid;

   public:

   Rect(double l, double w) : len(l), wid(w) {}
   double getW() {return wid;}
   double getL() {return len;}
}

class Area
{
   static double getArea(const Rect &rect)
   {
      return rect.getW() * rect.getL();
   }
}

Or another possibility to this stupid problem :

struct WidthProperty{
 unsigned width;
};
struct HeightProperty{
 unsigned height;
};
class Rectangle{
 WidthProperty w;
 HeightProperty h;
public:
 unsigned area(){ return w.width * w.height; }
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.