Class declaration offset - name between closing bracket and semicolon

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

In this example, what does “rect” mean after the closing parenthesis and between two commas in this class definition? I am having trouble finding clear explanations. Also: whatever that is, can you do this for structures too?

+5
source share
4 answers

rect is the name of the variable (the object in this case).

It is exactly as if he said:

  int rect;

except intthere is a definition of a new type called CRectangle. Typically, class types are declared separately and then used as

  CRectangle rect;

, , , .

, structs:

  struct SRectangle { int x, y; } rect;

, :

  struct { int x, y; } rect;

" " ( ).

+10

CRectangle, .

+2

. :

class CRectangle
{
    // ...
};

CRectangle rect;

++ class a struct, struct ( )

+1

The only difference between structures and classes is that structures have public default inheritance, and access and classes use private as the default value for both.

+1
source

All Articles