Exforsys

Home arrow Technical Training arrow C++ Tutorials

How to Access C++ Class Members

Author: Sripriya R     Published on: 21st Aug 2007    |   Last Updated on: 25th Jul 2011

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.

Ads

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:

Sample Code
  1. class exforsys
  2. {
  3.    int a, b;
  4.    public:
  5.    void sum(int,int);
  6. } e1;
Copyright exforsys.com


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:

Sample Code
  1. class exforsys
  2. {
  3.    private:
  4.    int a;
  5.    public:
  6.    void sum(int)
  7.    {
  8.      ....
  9.      ....
  10.    }
  11. };
  12.  
  13. void main()
  14. {
  15.    exforsys e1,e2;
  16.   ....
  17.   ....
  18. }
Copyright exforsys.com


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.

Sample Code
  1. class exforsys
  2. {
  3.      int x;          //Here access specifier is private by default
  4. };  
Copyright exforsys.com


whereas

Sample Code
  1. struct exforsys
  2. {
  3.    int x;   //Here access specifier is public by default
  4. };
Copyright exforsys.com


Ads

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.

Sample Code
  1. class exforsys
  2. {
  3.    private:
  4.    int a;
  5.    public:
  6.    void getvalues();               // Only Member Function declaration is done
  7. };
  8.  
  9. void exforsys :: getvalues() // Here Member Function is defined
  10. {
  11.   ....
  12.   ....
  13. }
  14.  
  15. void main()
  16. {
  17.    exforsys e1,e2;
  18.   ....
  19. }
Copyright exforsys.com


So the usage of scope resolution operator is as follows:



 
 

Comments