• Home
  • 3.23 Member Access Operator (.)

3.23 Member Access Operator (.)

View Categories

3.23 Member Access Operator (.)

< 1 min read

Table of Contents

The member access operator (.) is used to access the public data members and member functions of an object. It connects an object with its members and is one of the most frequently used operators in object-oriented programming.

The operator is applicable only to objects, not pointers. When an object belongs to a class or structure, the dot operator provides access to its members.

Source Code #

#include <iostream>

struct Student
{
    std::string name;
    int age;
};

int main()
{
    Student student;

    student.name = "Alice";
    student.age = 20;

    std::cout << "Name: " << student.name << '\n';
    std::cout << "Age : " << student.age << '\n';

    return 0;
}

Output #

Name: Alice
Age : 20

Explanation #

  • The member access operator (.) accesses the members of an object.
  • Student is a structure containing two data members: name and age.
  • Student student; creates an object of type Student.
  • student.name accesses the name member of the object.
  • student.age accesses the age member of the object.
  • The dot operator can be used to read from or write to public data members.
  • The member access operator is applicable only to objects and references, not pointers.
  • return 0; terminates the program successfully.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *