How To Pull An Attribute

Table of contents:

How To Pull An Attribute
How To Pull An Attribute

Video: How To Pull An Attribute

Video: How To Pull An Attribute
Video: How To Use Attributes In Minecraft 1.16 2024, December
Anonim

When working with objects and other instances of a class, accessing attributes is one of the usual operations for a programmer. Gaining access to certain data stored in objects can be difficult, given the security methods used for them. So, the attributes closed by the private and protected specifiers can be pulled out of a class instance only in a method belonging to an object of the same or a child (for protected) class.

How to pull an attribute
How to pull an attribute

Instructions

Step 1

Create an object or a pointer to an instance of the class whose attribute you need in your program. The standard construction for such an operation is CMyClass myObj1. Sometimes, to initialize an object, you need to pass certain parameters to the class constructor. In this case, the object creation record may look like this: CMyClass myObj1 (param1, param2, param3), where CMyClass is the class name, myObj1 is the name of the object being created, and all parameters requested by the constructor are listed in brackets. A pointer to a class instance is created as follows: CMyClass * pObj1 = new CMyClass (param1, param2, param3).

Step 2

The simplest way to call an attribute is to refer to it directly anywhere in the program. However, this is only possible for open data described in a class with the public modifier. Direct access through an object looks like this: myObj1.attr1, where attr1 is an attribute of this class. For a pointer, the call would be: pObj1–> attr1.

Step 3

If the attribute you need to access has the hidden status and is described in a class with the private modifier, then access to it is possible only from a method belonging to the same class. In this case, a method or function of a class must have the public status public. Add a new method to the class, where write the processing of the required attribute. In this case, parameters can be passed to the function, depending on the value of which certain actions are performed. The function itself can also return data, such as the content of an attribute. C ++ program code that implements both functions for working with a private attribute: class CMyClass {protected: int attr1; // public attribute: void funcA1 (int param) {attr1 = param; } int funcA2 () {return attr1; }};

Step 4

Thus, to set the private attribute attr1 to the value you need, call a method of the same class using the previously created object: myObj1.funcA1 (10) - a similar implicit operation sets the attr1 attribute to 10. When working with a pointer to an instance of a class, a similar operation will look like this: рObj1–> funcA (10). To retrieve the private attribute attr1 and find out the value stored in it, call another method of the class: int Res = myObj1.funcA2 (). In this case, the integer variable Res will be assigned the value of the hidden class variable.

Step 5

If you need to call an attribute with the protected status in all objects of child classes, refer to its variable directly. However, when working in functions of foreign classes, access to protected data will have to be obtained in the way described above.

Step 6

To call an attribute without creating an object, declare its variable in the class as static using the following construction: static int attr1. In this case, you can pull out the attribute anywhere in the program code by specifying the entry: CMyClass:: attr1.

Recommended: