#include <iostream>
using namespace std;

class CRectangle {
    int x, y;
  public:
    void set_values (int,int);
    int area () {return (x*y);}
};

void CRectangle::set_values (int a, int b) {
  x = a;
  y = b;
}

int main () {
  CRectangle 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;
}

In the above code I am confused on the lines from rect.set_values to cout<<

After rect.set_values(3,4) is executed are the values 3 and 4 stored in the variables "x" and "y" ?

and then when rectb.setvalues(5,6) is executed are the variables "x" and "y" overwritten with "5" and "6"?

If that is true then how come when the cout<< 's are executed afterwards they each have a different value?


Thanks, if my question does not make sense, please tell me I will try to be more clear.


EDITED: NEVERMIND

I think I just understood it.
rectb and rect are different variables so basically the class that was defined is a general one which is a definition for each variable. So each variable has a different class thats "template" is the one shown at the top. So I guess the set_values stores the numbers in "x" and "y" which are different for each variable.


If thats right then I guess the thread can be closed, sorry about that

CRectangle rect, rectb;
  rect.set_values (3,4);
  rectb.set_values (5,6);
  cout << "rect area: " << rect.area() << endl;
  cout << "rectb area: " << rectb.area() << endl;

rect and rectb are 2 different variables that are of the same class-type.

to make the code more understandable it might be better for you to do something like this--

CRectangle rect; // a CRectangle reference-variable named rect
  CRectangle rectb; // a CRectangle reference-variable named rectb
  rect.set_values (3,4);
  rectb.set_values (5,6);
  cout << "rect area: " << rect.area() << endl;
  cout << "rectb area: " << rectb.area() << endl;

Ok thanks I think I understand my previous question but another question are the values "3" and "4" or "5" and "6" stored in any specific variable?

Like if I wanted to show the "3" and "4" can I write a line

cout <<specificvariable;

what would that specificvariable be?

Ok thanks I think I understand my previous question but another question are the values "3" and "4" or "5" and "6" stored in any specific variable?

Like if I wanted to show the "3" and "4" can I write a line

cout <<specificvariable;

what would that specificvariable be?

If you want to display the individual variables, you will have to understand that calling rect.x or rectb.x and rect.y and rectb.y are illegal calls since classes are initialized with private member variables until you provide public access members/functions.

If you want to get a specific variable, you'll need to provide a public "getter," like this--

class CRectangle {
    int x, y;
  public:
    void set_values (int,int);
    int area () {return (x*y);}
    int getX(){return x;};
    int getY(){return y;};
};

Now for rext and rectb you can call getX() and getY() with cout to display their respective x's and y's--

cout << rect.getX() << ", " << rect.getY() << endl;
cout << rectb.getX() << ", " << rectb.getY() << endl;

oh that makes sense!!!!!!

but can u just make the

int x and int y public instead?

then call rectb.x and rect.x etc.
?

>but can u just make the
>int x and int y public instead?
You can, but public data is typically a bad idea because it means you're giving full control over the values to everyone. Most of the time you don't want to do that, which is why public or protected member functions are used to give access without losing control.

alright cool, thanks a lot! :)

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.