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. Studentis a structure containing two data members:nameandage.Student student;creates an object of typeStudent.student.nameaccesses thenamemember of the object.student.ageaccesses theagemember 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.