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:
Object_name.function_name (arguments);
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.
For example:
A class and object are defined below:
class exforsys
{
int a, b;
public:
void sum(int,int);
} e1;
Then the member access is written as:
e1.sum(5,6);
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:
class exforsys
{
private:
int a;
public:
void sum(int)
{
& #46;...
& #46;...
}
};
void main()
{
exforsys e1,e2;
& #46;...
& #46;...
}
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.
class exforsys
{
int x; //Here access specifier is private by default
};
whereas
struct exforsys
{
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.
class exforsys
{
private:
int a;
public:
void getvalues(); // Only Member Function declaration is done
};
void exforsys :: getvalues() // Here Member Function is defined
{
& #46;...
& #46;...
}
void main()
{
exforsys e1,e2;
& #46;...
}
So the usage of scope resolution operator is as follows: