Exforsys.com
 

Sponsored Links

 

C++ Tutorials

 
Home Tutorials C++
 

How to Access C++ Class Members

 
Category: C++
Comments (1)

How to Access C++ Class Members

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);


Where e1 is the object of class exforsys and sum() is the member function of the class.


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)
   {
      ………
      ………
   }
};

main()
{
   exforsys e1,e2;
   …………
   …………
}



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 :: void getvalues() // Here Member Function is defined
{
   ………………
   ………………
}


main()
{
   exforsys e1,e2;
   …………
}




So the usage of scope resolution operator is as follows:




Read Next: C++ Standard Input Output Stream



 

 

Comments


Matthias said:

  Isn't there an error in the scope resolution operator example:
void exforsys::getvalues()
should work correct
December 30, 2008, 4:40 pm

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape