Class default access modifier

In a class, the default access modifier for class member and member functions is private.

Private members are the class members (data members and member functions) that are hidden from the outside world. The private members implement the concept of OOP called data hiding.

Class default access (private)

class 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;
}

Compilation

The above program can not compile since all default access modifier are private.

$ g++ class.cpp
class.cpp:19:13: error: calling a private constructor of class 'student'
  s1 = new  student();
            ^
class.cpp:4:4: note: implicitly declared private here
   student();
   ^
class.cpp:20:7: error: 'm_Member1' is a private member of 'student'
  s1->m_Member1 = 0;
      ^
class.cpp:3:7: note: implicitly declared private here
  int m_Member1;
      ^
class.cpp:21:10: error: calling a private destructor of class 'student'
  delete s1;
         ^
class.cpp:5:4: note: implicitly declared private here
   ~student();
   ^
3 errors generated.
$

Class default access override with "public"

Having "public" access can pass the compilation.

class 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;
}

See also: What is the default access modifier for structure 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.

#