by Dinesh Thakur Category: Classes in C++

Object Oriented Programming has a special feature called data abstraction. Data abstraction allows ignoring the details of how a data type is represented. While defining a class, both member data and member functions are described. However while using an object (that is an instance of a class) the built in data types and the members in the class are ignored. This is known as data abstraction. This can be seen from the above example.

Example

An example to show how public member data and public member functions are accessed outside the class.

 

#include <iostream.h>

#include<conio.h>

class base

{

       public: int sl,s2;

       public:

       void inp_val()

        {

               cout<<"input the values of sl and s2 ";

               cin>>sl>>s2;

        }

      void disp()

       {

              cout<<sl <<" "<<s2;

              cout<<"\n" ;

       }

};

void main()

{

       clrscr();

       base o1;

       o1.inp_val();

       o1.disp();

}

The first occurrence of the keyword public (or private) before a member data or a member function makes the compiler assume the types of accesses of all the members following this to be of the first occurrence type. However, if the types of accesses of member data and member functions are different, then the access type should be explicitly mentioned in each case.

Example

An example to show that private member data cannot be accessed outside the class if the member functions which involve this data are also private.

 

#include <iostream.h>

#include<conio.h>

class base

{

          private: int sl,s2;

          void inp_val()

          {

                    cout<<"input the values of s1 and s2 ";

                    cin>>sl>>s2;

           }

 

          void disp()

          {

                  cout<<sl <<" "<<s2;

                  cout<<"\n" ;

          }

};

void main()

{

        clrscr();

        base o1;

        o1.inp_val();

        o1.disp();

}

On the execution of the above program, following error messages will appear.

error messages

Error oop22.cpp 26 : 'base::inp-val()' is not accessible

error oop22.cpp 27: 'base::disp() ' is not accessible

Example

 

Modified version of Example in which member data are private but the member function which invoke the private data members are public. This will not cause any error as in example

#include <iostream.h>

#include<conio.h>

class base

{

          private: int sl,s2;

          public: void inp_val()

          {

                     cout<<"input the values of sl and s2 ";

                     cin>>sl>>s2;

           }

          void disp()

          {

                   cout<<sl <<" "<<s2<<"\n ";

          }

};

          void main()

          {

                 clrscr();

                 base o1;

                 o1.inp_val();

                 o1.disp();

          }



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content





Popular Article