Structure default access modifier

Structure in C++ has been taken from C programming language and C does not support any object oriented programming concept. Encapsulation and datahidings are not a part of structures in C programming. Thus all member variables and member functions are thus accessible from outside world.

In a C++ structure type, the default access modifier for structure member and member functions is "public". We need not to provide the keyword "public" in the class definition but that is default taken by the compiler.

Public members are the structure members (data members and member functions) that are open to the outside world. The public function members implement the concept of interfaces in object oriented programming.

Structure default access without "public"

struct student
{
  int m_Member1;
   student();
   ~student();
};
student::student()
{
}
student::~student()
{
}

int main( int argc, char * argv[])
{
  student * s1 ;
  s1 = new  student();
  s1->m_Member1 = 0;
  delete s1;
}

Structure default access with "public"

struct student
{
public : 
  int m_Member1;
   student();
   ~student();
};
student::student()
{
}
student::~student()
{
}

int main( int argc, char * argv[])
{
  student * s1 ;
  s1 = new  student();
  s1->m_Member1 = 0;
  delete s1;
}

Compilation

The above two programs can compile fine since having "public" keyword or not is same for the compiler.

$ g++ struct.cpp

C++ class default access modifier

When it comes for the C++ class the story is different. C++ programming fully supports data hiding and object oriented programming. So the member variables and functions are hidden inside the class. Know more about the default access modifier of C++ class see also: What is the default access modifier for class members and member functions in c++?

About our authors: Team EQA

You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.

#