1 #ifndef __FIGURES_H 2 #define __FIGURES_H 3 4 5 class Figure{ 6 int color; 7 public: 8 Figure(int color) : 9 color(color) 10 {} 11 12 virtual double place() const = 0; 13 14 struct Point{ 15 int x; 16 int y; 17 Point(int x, int y): 18 x(x), y(y) 19 {} 20 }; 21 }; 22 23 ... 24 #endif 

If the access specifier is not specified in the 5th line, then by default it means public: int color; ? It is not very clear what is happening at 8-10 and 14-20

If it is not difficult to paint or say how it is called and where you can read about it.

 Figure(int color) : color(color) {} 

and

  struct Point{ int x; int y; Point(int x, int y): x(x), y(y) {} }; 

    2 answers 2

    1. The default access for the class is private (and for the structure is public ). Thus, color declared as private .
    2. In lines 14-20, the Point structure is internal for the class Figure . It is in the public , so it is “visible” outside. This is only a declaration of the Point structure, but not a declaration of the field of type Point . (There is no such field in the structure.) You can use this type, for example, by declaring variables of this type: Figure::Point point; .
    3. Lines 8-10 show the implementation of the constructor, typical of C ++.

    Let's write down the lines:

     Figure(int color) : // Конструктор принимает один параметр. color(color) // Инициализация полей: для поля color вызывается конструктор // с аргументом, равным аргументу color из конструктора. По // стечению обстоятельств, их имена одинаковы. {} // Больше конструктор ничего не делает. // Это типично для конструкторов в C++: только инициализировать // поля и всё. 

    The constructor at Point executed in the same key, only it initializes 2 fields.

    In the constructor, you can also initialize the base classes in addition to the fields directly before the constructor body. But Figure / Point does not have a base class, so this part disappears. If there are base classes, and you have not registered an initializer, the constructor of the base class with no parameters will be called (well, or one that can be called without parameters).

    • Thank you, figured out. Only with the default access modifier is not clear, are you sure that color is declared private here? If I prescribe private: int color; the program starts to work differently when private was not written, but when writing public or protected everything works as it should. - arukasa
    • @arukasa: Strange. I quote :> A class has a default public accessibility for private inheritance and members. What is different? Not compiled? - VladD
    • He writes that he cannot turn to a private member, the matter is not in this piece of code, but in the whole program, just if the private is not registered, then he can turn to it. Or I do not quite understand, and I want to clarify in this case:> int color; == Private: int color; ? - arukasa
    • In this case, it should be the same as private: (with a small letter). Show a piece of code that stops compiling, eh? - VladD
    • > ideone.com/OXmWGy .h> ideone.com/6JEF7q .cpp 32-34> ideone.com/dnY4B7 .cpp - arukasa
    • Access rights for an object of the type declared using the class keyword - private and public - for the struct
    • Lines 7-10 - This is the declaration (and definition) of the object constructor of the class Figure with a parameter of type int (or any that can be given by int, since the explicit keyword is not specified). The constructor does nothing except initialization of the internal field color by the constructor parameter (of course, it is very crookedly done, but most compilers grab it).
    • This is a declaration of the internal type of the object inside the class Figure . All fields are public. The constructor takes two parameters of type int (or reducible to int) initializing the structure fields. The type Point is available outside the class Figure .