146k views
5 votes
given a variable named p that refers to a product object, which of the following statements accesses the price attribute of the product object

User Christina
by
8.2k points

1 Answer

4 votes

To access the price attribute of the product object which is referred by the variable p, you can utilize the dot notation. Here are the steps:

1. You need to first confirm that there is indeed a 'price' attribute available for the product object. Usually, this comes from the design of the product class.

2. In order to access or reference the 'price' attribute from the 'product' object, you would use the variable 'p' which is acting as the reference to the product object.

3. You implement the dot notation, which is simply a period '.', to link the object with its attribute.

4. After placing the '.', you simply write the attribute which you wish to access. In this case, it's 'price'.

5. Combining this all together, the correct statement that accesses the price attribute of the product object is "p.price".

This statement will retrieve the value stored in the 'price' attribute of the 'product' object that 'p' is pointing to. This value can then be used or manipulated according to the needs of your program.

User Harley
by
8.6k points