Home C++ Introduction Decisions Loops Input/Output Functions Stack and Heap References Arrays Searching and Sorting Recursion Pointers Character and Strings Structures Classes Inheritance Exceptions Templatess STL Modern C++ Misc Books ----

Classes


Contents

Access Specifiers

When defining our class we can choose to make the data variables or member functions private. By default the access specifier is private.
File: c2.cpp
#include <string>
#include <iostream>

using namespace std ;

class Person
{
    string firstName  ;
    string lastName ;
    int age ;
    void print()
    {
        cout << "Name:" << firstName << " " << lastName
        << " Age: " << age << endl ;
    }
};

int main()
{
    Person personObj ;
    personObj.firstName = "Robert" ;
    return 0 ;
}
$ g++ c2.cpp
c2.cpp: In function ‘int main()’:
c2.cpp:21:19: error: ‘std::string Person::firstName’ is private within this context
   21 |         personObj.firstName = "Robert" ;
      |                   ^~~~~~~~~
c2.cpp:8:16: note: declared private here
    8 |         string firstName  ;
We did not specify any access specifiers ( default access specifier was private) and so we could not access the variable from outside. What "outside" means is that once we create an object of the class and then we try to access the variables. In the above case the statement:

personObj.firstName = "Robert" ;

caused the compiler error. Note even if the data variable is private it can always be access by any method of the class even if the method is private. Let us change the access specifier so that we get rid of the compiler error.

We did not specify any access specifiers ( default access specifier was private) and so we could not access the variable from outside. What "outside" means is that once we create an object of the class and then we try to access the variables. In the above case the statement:

personObj.firstName = "Robert" ;

caused the compiler error. Note even if the data variable is private it can always be access by any method of the class even if the method is private. Let us change the access specifier so that we get rid of the compiler error.
File: c3.cpp
#include <string>
#include <iostream>
using namespace std ;
class Person
{
    public:
    string firstName  ;
    string lastName ;
    int age ;
    void print()
    {
        cout << "Name:" << firstName << " " << lastName
        << " Age: " << age << endl ;
    }
};

int main()
{
    Person personObj ;
    personObj.firstName = "Robert" ;
    personObj.lastName = "Newman" ;
    personObj.age = 82 ;
    personObj.print() ;
    return 0 ;
}
$ g++ c3.cpp ; ./a.exe
Name:Robert Newman Age: 82
The "private" and "public" specifiers are hints to the compiler. Remember the CPU does not have a "public/private" scope concept. We can access the private variables by address manipulation.
File: c4.cpp
#include <string>
#include <iostream>
using namespace std ;

class Person
{
  private:
   int age ;
   string firstName  ;
   string lastName ;
  public:
   void setAge( int ageP )
   {
       age = ageP ;
    }
   int getAge()
   {
       return ( age )  ;
    }
   void setFirstName( string firstNameP )
    {
        firstName = firstNameP ;
    }
   string getFirstName()
    {
            return ( firstName )  ;
    }
  void setLastName( string lastNameP )
    {
        lastName = lastNameP ;
    }
  string  getLastName()
    {
            return ( lastName )  ;
    }
  void print()
    {
            cout << "Name:" << firstName << " " << lastName
            << " Age: " << age << endl ;
    }
};

int main()
{
    Person personObj ;
    personObj.setFirstName(  "Robert" )  ;
    personObj.setLastName( "Newman" ) ;
    personObj.setAge( 82)  ;
    personObj.print() ;
    //Get age directly by addresses
    Person*  ptr = &personObj ;
    int ageHack =     *(    (int*)ptr    )  ;
    cout << "ageHack =" << ageHack << endl ;
    return 0 ;
}
$ g++ c4.cpp ; ./a.exe
Name:Robert Newman Age: 82
ageHack =82

In the above we take advantage of the way a class object is stored in the ram.

                        RAM

                      -------------

         Person          age

                         firstName

                         lastName



  We take the address of the person object first.

      Person*  ptr = &personObj ;

Then the address is type cast to a pointer of type int.

 (int*)ptr

What we are saying is the the address points to an integer. Then we
obtain the integer at that address with the * .

int ageHack =     *(    (int*)ptr    )  ;

We have obtained a value for the age even though the variable
was declared as private.

Friend

Const functions

OO

Smart Pointer