#Previous question #Index of C++ Questions #Next question

Structure default access modifier

Structure in C++ has been taken from C programming language and C does not support any OOP concept. All member variables and member functions are thus accessible from outside world.

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

Public members are the class 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

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

Most popular topics (must visit)

Useful links for students