
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is Dot Operator in C++
The dot operator (.) in C++ is a member access operator, which is used to access the members (variables and functions) of a class, structure, and union through an object. It allows you to access and manipulate the properties of an object's data members and member functions.
The dot (.) operator is used for various purposes. Here is the following list of its uses.
- Accessing Data Members
- Calling Member Functions
- Working with nested structures /Objects
- Chained Function Calls
- Accessing Static Members
Accessing Data Members
This operator is used to access, read, and modify the data members (variables) of a class or structure, which are defined inside a class or structure.
Syntax
object_name.data_member;
Example
#include <iostream> using namespace std; class Student { public: string name; int roll; }; int main() { Student s1; s1.name = "Aman"; s1.roll = 101; cout << "Name: " << s1.name << endl; cout << "Roll No: " << s1.roll << endl; return 0; }
Output
Name: Aman Roll No: 101
Explanation
In this code, we can see the dot (.) operator is used to access and manipulate the data members name and roll of the object s1 of class Student.
Here, in s1.name = "Aman", the dot operator accesses the name variable inside the s1 object and assigns it the value "Aman", similarly for roll. And then s1.name and s1.roll are used to print their values.
Calling Member Functions
Calling a member function is the process that calls and executes the function that is defined inside the class or struct.
Syntax
object_name.member_function_name(arguments);
Example
#include <iostream> using namespace std; class Student { public: string name; int roll; // Member function with parameters void setDetails(string n, int r) { name = n; roll = r; } // Member function to display the details void display() { cout << "Name: " << name << endl; cout << "Roll No: " << roll << endl; } }; int main() { Student s1; s1.setDetails("Aman", 101); s1.display(); return 0; }
Output
Name: Aman Roll No: 101
Explanation
In the given code, we can see that the dot (.) is used to access and call the member functions setDetails() and display() on the object s1 of the Student class.
Here, in s1.setDetails("Aman", 101), the dot operator calls the setDetails function for the s1 object, which sets the name and roll data members. And then s1.display() is used to display the stored values.
Nested structures /Objects
In C++, nested structures/objects are said to be structures that are within another structure, representing a complex data structure. Here, the dot operator (.) is used to access the data members or methods of both the outer and inner structures.
Syntax
outer_object.inner_object.member;
Example
#include <iostream> using namespace std; struct Address { string street; string city; }; struct Student { string name; int roll; Address addr; // Nested structure }; int main() { Student s1 = {"Aman", 101, {"123 Main St", "Bareilly"}}; cout << "Name: " << s1.name << endl; cout << "Roll No: " << s1.roll << endl; cout << "Street: " << s1.addr.street << endl; cout << "City: " << s1.addr.city << endl; return 0; }
Output
Name: Aman Roll No: 101 Street: 123 Main St City: Bareilly
Explanation
In the following code, there's a nested structure, Address, inside the structure Student. Here dot operator (.) is used to access both direct members of s1, that is, its name and roll with the member of the nested addr structure using s1.addr.street and s1.addr.city. This is how the dot operator enables access to inner members through the object.
Chained Function Calls
Chained function calls in C++ are said to be calling multiple functions on the same object in a single line using the dot operator. Here, each function returns a reference (*this) to the current object, which allows the next function to be called on that returned reference.
Syntax
object_name.function1().function2().function3();
Example
#include <iostream> using namespace std; class Student { public: string name; int roll; // Set name and return object Student& setName(string n) { name = n; return *this; } // Set roll and return object Student& setRoll(int r) { roll = r; return *this; } void display() { cout << "Name: " << name << endl; cout << "Roll No: " << roll << endl; } }; int main() { Student s1; s1.setName("Aman").setRoll(101).display(); return 0; }
Output
Name: Aman Roll No: 101
Explanation
In this code, the dot operator (.) is used to chain multiple function calls on the same object s1, in a single line.
In this, the function setName("Aman") sets the name and returns the current object using *this, which allows the next function setRoll(101) to be called using another dot operator. Similarly, setRoll also returns the object to call the display() function.
Thus, here the chaining is done as each function returns the object itself, and the dot operator connects each function call in the sequence.
Accessing Static Members
Static members are the members that belong to the class itself, not to any specific object. These are shared across all objects of that class.
This can be accessed in two ways: using the class name (ClassName::staticMember) or using an object with the dot operator (object.staticMember).
Syntax
object_name.static_member;
Example
#include <iostream> using namespace std; class Student { public: static int count; // Static member variable // Static member function static void incrementCount() { count++; } }; // Initialize static member variable int Student::count = 0; int main() { Student s1, s2; // Accessing and Incrementing static member using dot operator s1.incrementCount(); s2.incrementCount(); cout << "Student count: " << s1.count << endl; return 0; }
Output
Student count: 2
Explanation
In the following code, we can see how the static member variable count is shared and accessed through the objects s1 and s2 using the dot operator like s1.count, this is possible because static members are tied to the class itself, and using the dot operator on any object will still refers to the shared static variable for the entire class.