c++ - Initializing class variables in nested classes -


i'm new c++ classes, , have questions nested classes.

what know

  1. classes in c++ set of variables , functions set 'private' default.
  2. when create class, object created.
  3. i understand basic concepts of constructors.

i know can make 'classes' variable in 'class', known nested class, can't figure out how done exactly. if create nested class, create object itself, , objects class variables inside nested class?

i'm having problems initializing class variables in nested class using constructors.

for example

class point { int xpos; int ypos;  } 

let's created class containing 2 int variables.

class rectangle { point upleft; point lowright; } 

then, created rectangle class having 2 'point class' variables.

rectangle rec1; 

then, created object rec1.

how can initialize 2 xpos , 2ypos using constructors in rectangle class?

you need add constructor, here simple example:

class point { public:     point(int x, int y) : xpos(x), ypos(y) {} private:     int xpos;     int ypos;  };   class rectangle { public:     rectangle(int x1, int y1, int x2, int y2) : upleft(point(x1, y1)), lowright(point(x2, y2)) {} private:     point upleft;     point lowright; }; 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -