Member Functions in C++
A class declaration defines a new class type and lists its members: member functions and data members.
Why Programs Group Data With Operations
Many programs need to represent a thing that has both data and operations on that data. A bank account has a balance, and it also needs operations that deposit and withdraw money. A list of numbers has values stored in it, and it also needs operations that report how many values it holds. C++ lets you bundle the data and the operations together in one unit called a class.
A class declaration is the C++ statement that defines a new class type. It gives the type a name and lists everything that belongs to it. Once a class type is defined, you can work with values of that type in the same way you work with built-in types such as int and double. Each value of a class type is called an object.
This article uses one running example throughout: a class named Vector. Vector is a small container that holds a sequence of double values. Its declaration shows every part a class declaration usually contains, so reading it carefully is the fastest way to learn the pattern. You will trace what each line tells you, then use the same procedure on a class of your own.
Reading the Vector Declaration
Every class declaration follows the same overall pattern. The keyword class is followed by the name of the class, and the body is enclosed in braces. The body is divided into labeled sections by the words public and private, each followed by a colon. The declaration ends with a closing brace and a semicolon. Every member declaration inside the body ends with its own semicolon.
The source also includes a free function named sqrt for comparison. It takes a double and returns a double. Its declaration has a return type, a name, and a parameter list, and it ends with a semicolon, just like the member function declarations inside Vector. The difference is where the declaration sits: a free function stands on its own, while a member function is declared inside the body of a class.
Here is a sketch with the same shape as the Vector declaration. It is an original illustration written for this article, not the source code itself. Notice the labels, the three declarations under public, and the two declarations under private.
- class Vector names the new class type
- public: opens the section whose members code outside the class may use
Vector(int n), operator[], and size are declarations with parameter lists, so they are member functions- private: opens the section that outside code cannot use
- double* elem and int sz are plain variable declarations, so they are data members
The anatomy strip above shows the parts in order. The class name Vector appears once, right after the keyword class. Everything else inside the braces is either an access label or a member. Read top to bottom, each access label starts a new section, and the members listed beneath it belong to that section until the next label appears.
What Public and Private Control
The access specifiers public and private control who can use the members that follow them. Public members are accessible from anywhere, and private members are accessible only within the class. This is the mechanism that decides which parts of a class form its outside-facing interface and which parts are hidden inside it.
An access specifier applies to every member after it, until the next specifier appears. The Vector declaration does not need a separate label on each line. The public: label covers the constructor, operator[], and size, and the private: label covers elem and sz. The access specifier is a property of the section, not of an individual declaration.
The public functions in Vector form its interface. They are the route through which code outside the class can reach the data the class holds. The private data members elem and sz hold the stored values, and they stay hidden behind that interface.
Predicting Outside Access Before Checking
What do you think happens?
Given the Vector declaration, which members can code outside the class use: the constructor, operator[], size, elem, and sz?
Reveal answer
Only the constructor, operator[], and size.
These three sit under the public: label, so they are accessible from anywhere. elem and sz sit under the private: label, so they are accessible only within the class.
Member Functions Versus Data Members
Member functions and data members are both declared inside the class body, but they play different roles. A member function is a function that is part of the class. Its job is to access and manipulate data members, and it can perform operations on class objects. A data member is a variable that is part of the class. Its job is to store data within a class object.
The two kinds of member depend on each other. The data members hold the state of an object, and the member functions are the operations that work on that state. The source notes that data members can be accessed and manipulated using member functions, which is exactly the pattern visible in Vector: the data members are private, while the member functions are public.
| Member | Kind | Access in Vector | Role |
|---|---|---|---|
| Vector(...) | Member function (constructor) | public | Part of the interface through which outside code works with a Vector |
| operator[] | Member function | public | Part of the interface; operates on the stored values |
| size | Member function | public | Part of the interface; reports on the stored values |
| elem | Data member | private | Stores where the doubles live |
| sz | Data member | private | Stores how many doubles there are |
Each member of Vector, classified by kind, access section, and role
Tracing Which Members Are Usable
The source gives a procedure you can use on any class declaration. Follow the steps in order and record what you find at each one.
- 1Find the access labels first. Note every public: and private: label and the order in which they appear.
- 2Assign each member to the section that was in effect when it was declared. A label covers every member after it until the next label.
- 3Sort each member by whether it is followed by a parameter list. Declarations with a parameter list are member functions. Plain variable declarations are data members.
- 4Read the access of each member. Members under public are usable from outside the class; members under private are not.
Classifying the members of a new class
A class declaration has a public: label followed by a declaration with a parameter list, a private: label followed by a plain variable declaration, and then a second plain variable declaration still under private. Which members can outside code use, and which are member functions?
- 1Find the labels
There is one public: label and one private: label. Everything after public: is public until private: appears.
- 2Sort the public member
The first declaration under public: has a parameter list, so it is a member function. Because it is public, outside code can use it.
- 3Sort the private members
The two plain variable declarations under private: are data members. Both are private, so outside code cannot use them.
One public member function is usable from outside the class. Two private data members are not.
Array Storage Behind the Vector Data Members
The source includes a comment on the elem data member: elem points to an array of sz doubles. The two data members work together. elem records where the stored doubles live, and sz records how many there are. Neither member alone describes the whole collection. A class with both data members can describe a sequence of values, and that is why Vector is a sensible running example.
The key idea for this article is the split of responsibilities. The array of doubles is the stored data, which lives in private data members. The public member functions are what outside code uses to work with that data. Outside code never needs to know how elem and sz are arranged, because the interface hides them.
Mistakes Beginners Make With Access Labels
Common mistake Treating the access label as applying only to the next line
What it looks likeprivate: followed by two declarations, where the reader assumes only the first is private.Why it failsThe access specifier applies to every member after it, until the next specifier appears.
Do this insteadRead the section from the label down to the next label, and treat every member in that span as belonging to the label.
Common mistake Deciding member kind by the name instead of the declaration shape
What it looks likeAssuming size is a data member because it sounds like a value.
Why it failsDeclarations with a parameter list are member functions. Plain variable declarations are data members.
Do this insteadLook for the parentheses that mark a parameter list. Their presence makes the member a member function.
Common mistake Assuming a private member is unusable everywhere in the program
What it looks likeConcluding that elem can never be touched in any part of the program.
Why it failsPrivate members are accessible only within the class, so the rule is about where code sits relative to the class.
Do this insteadAsk whether the code using the member is inside the class. Outside code must go through the public interface.
Common mistake Confusing a free function declaration like sqrt with a member function declaration. The free function stands alone, while a member function sits inside the class body.
Practice Reading a Class Declaration
Take the sketch of Vector from earlier and change it so that size is declared under private: instead of public:. Then answer two questions: which members can code outside the class now use, and which members are member functions?
Hints
- Start by listing the access labels and the members under each one.
- A declaration with parentheses is a member function, regardless of its access.
- After the change, only
Vector(...)and operator[] remain under public.
Key Takeaways
- A class declaration defines a new class type, gives it a name, and lists its members: member functions and data members.
- The access specifiers public and private control who can use the members that follow them. Public members are accessible from anywhere, and private members only within the class.
- Member functions access and manipulate data members, while data members store data within a class object.
- To predict outside access, find the access labels first, then sort each member by whether it has a parameter list, and read its access from the section it sits in.