Something wrong about classes in my program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

class CRectangle {
    
  public:
	  int width, height;
    void set_values (int, int);
    int area () {return (width * height);}
    CRectangle duplicate (CRectangle);
};

void CRectangle::set_values (int a, int b) {
  width = a;
  height = b;
}

 CRectangle CRectangle::duplicate (CRectangle rectparam)
{
  CRectangle rectres;
  rectres.width = rectparam.width*2;
  rectres.height = rectparam.height*2;
  return (rectres);
}

int main () {
  CRectangle rect, rectb;
  rect.set_values (2,4);
  rectb.duplicate (rect);
  cout << rectb.area();
  system("PAUSE");
  return 0;
}


687194768Press any key to continue . . .


It's supposed to give me 32 not this number
It's your duplicate function. It's not actually performing anything on rectb. It just makes a new rect, assigns some dimensions and returns it. It's not actually doing anything with the returned value.

So rectb remains uninitialised.
Now I got it !! Self-study is a bit hard :)
when this line is executed:
 
cout << rectb.area();


rectres is already been destroyed...

and it seems like tome, it's a wrong way to return a value. rectb cannot receive the value returned...
Well Guys I go a way to do it !! Thanks
Topic archived. No new replies allowed.