How to access Members of an Object in C++

After creating an object, we can access the data and functions using the dot operator as shown below:

object-name.variable-name; (or)

object-name.function-name(params-list);

 

Based on our student class example, we can access the get_details() function as shown below:

std1.get_details();

std2.get_details();

 

The complete program which demonstrates creating class, creating objects, and accessing object members is as follows:

 

In the above program class name is Student and object names are std1 and std2.

Nested Functions

 

A member function can call another member function directly without any need of dot operator. To call another member function, we can simply write the function name followed by parameter values enclosed in parentheses. An example for calling another member function is as follows:

 

In the above program, the function show_details() contains nested call to area() and peri() functions.

Take your time to comment on this article.

Leave a comment