In this C++ tutorial, you will learn how to access Class members, dot operator or class member access operator, difference between struct and class and scope resolution operator.
It is possible to access the class members after a class is defined and objects are created.
General syntax to access class member:
The dot ('. ') used above is called the dot operator or class member access operator. The dot operator is used to connect the object and the member function. This concept is similar to that of accessing structure members in C programming language. The private data of a class can be accessed only through the member function of that class.
A class and object are defined below:
int a, b;
Then the member access is written as:
A Where e1 is the object of class exforsys and sum() is the member function of the class.
A The programmer now understands declaration of a class, creation of an object and accessibility of members of a class.
It is also possible to declare more than one object within a class:
In these two objects e1 and e2 are declared of class exforsys.
By default, the access specifier for members of a class is private. The default access specifier for a structure is public. This is an important difference to recognize and understand in object-oriented C++ programming language.
int x; //Here access specifier is private by default
int x; //Here access specifier is public by default
It is not always the case that the member function declaration and definition takes place within the class itself. Sometimes, declaration of member function alone can occur within the class. The programmer may choose to place the definition outside the class. In a situation such as this, it is important to understand the identifying member function of a particular class. This is performed by using the operator :: this is called scope resolution operator.
So the usage of scope resolution operator is as follows: