Member in C++

What is Member in C++?

The member access specifiers public and private control access to a class’s data members and member functions. The default access mode for classes is private so all members after the class header and before the first member access specifier are private. After each member access specifier, the mode that was invoked by that member access specifier applies until the next member access specifier or until the terminating right brace(}) of the class definition. The member access specifiers public, private and protected may be repeated, but such usage is rare and can be confusing.

A class’s private members can be accessed only by a member function of that class. The public members of a class may be accessed by any function in the program that holds a handle on an object of that class

An attempt by a function, which is not a member of a particular class(or a friend of that class,) to access a private member of that class is a compiler error

How to Implement a Member Function?

To define a member function of a class template, you have to specify that it is a template, and you have to use the full type qualification of the class template. Thus, the implementation of the member function push() for Stack(T) looks like this:

template<typename T>
void Stack<T>::push(T const&elem)

What is a static member function of C++?

From a non-template class, one copy of a static data member is shared among all objects of the class and the static data member must be initialized at file scope

Each class-template specialization instantiated from a class template has its own copy of each static data member of the class template; all objects of that specialization share that one static data member. In addition, as with static data members of non-template classes, static data members of class-template specializations must be initialized at file scope. Each class-template specialization gets its own copy of the class template’s static member function

Translate »