You get a bonus - 1 coin for daily activity. Now you have 1 coin

1.2. Availability of class components

Lecture



In the examples of classes considered earlier, the components of classes are publicly available. At any point in the program where the class definition is “visible”, you can access the components of the class object. Thus, the basic principle of data abstraction is not fulfilled - encapsulation (concealment) of data inside the object. To change the visibility of components in a class definition, you can use access specifiers: public, private, protected .

Public (public) components are available in any part of the program. They can use any function both inside the class and outside it. Access from the outside is through the object name:

object_name.class_name;

link_on_object.name_name_class;

pointer_to_object-> member_name of class;

Private components are localized in the class and are not accessible from the outside. They can be used by member functions of a given class and functions by “friends of the class in which they are described.

Protected components are available inside the class and in derived classes. Protected components are needed only when building a class hierarchy. They are also used like private members, but can additionally be used by member functions and “friends” of classes derived from the class described.

You can change the status of access to the components of a class by using the keyword class in the class definition. In this case, all components of the class by default are their own.

Example 1.2.1

class complex

{

double re, im; // private by default

public:

double real () {return re;}

double imag () {return im;}

void set (double x, double y) {re = x; im = y;}

};

 

created: 2015-12-20
updated: 2021-03-13
132388



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

C ++ (C plus plus)

Terms: C ++ (C plus plus)