Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Overview of Member Functions
In this articleMember functions are
either static or nonstatic. The behavior of static member functions differs from other member functions because static member functions have no implicit If a member function is defined inside a class declaration, it is treated as an inline function, and there is no need to qualify
the function name with its class name. Although functions defined inside class declarations are already treated as inline functions, you can use the An example of declaring a function within a class declaration follows:
If a member function's definition is outside the class declaration, it is treated as an inline function only if it is explicitly declared as The following example is identical to the previous declaration of class
Note Although member functions can be defined either inside a class declaration or separately, no member functions can be added to a class after the class is defined. Classes containing member functions can have many declarations, but the member functions themselves must have only one definition in a program. Multiple definitions cause an error message at link time. If a class contains inline function definitions, the function definitions must be identical to observe this "one definition" rule. FeedbackSubmit and view feedback for Additional resourcesAdditional resourcesIn this articleA class is a collection of C++ statements that specify the state (data members) and implementations of behavior (member functions) that a particular type of object may have. Think of a class as a "blueprint" that objects may be created from. So a class is not an object, but a description of an object. When we define a class in C++, we are defining a new data type. An object is simply a variable of a class type. To define a new class in C++, we write a class definition. For example, here is a class definition for a new class named
A class definition starts with the keyword The class definitions contains variable declarations for the data members of the class and prototypes (declarations) for the member functions of the class. Definitions for the member functions of the class are normally placed outside of the class definition. C++ also has the Access ModifiersAccess modifiers are keywords that modify the accessibility of members of a class. C++ uses three access modifiers:
In general, data members that are not constants should usually have private access. Constants and member function prototypes should usually have public access. A constant or member function that is only used within the class (say a sort member function that is only called by another member function of the class) may be made private instead. The ConstructorsA constructor is a special type of member function that is called to create a new object of the class. The usual job of a constructor is to initialize the data members of a new object to valid values. The name of a constructor is always the same as the name of the class. A constructor has no return type and cannot be constant. The constructor may have 0 to many parameters passed to it. Through function overloading, a C++ class may have multiple constructors as long as their parameters are different. For example, if one constructor takes zero parameters and another constructor takes two, the compiler will be able to tell them apart. If you do not code any constructors for your class, the compiler will generate a "default" constructor without any parameters. The compiler-supplied constructor does nothing (effectively it has an empty function body). If you write any constructors for your class, the compiler will not supply this default constructor. The code in a constructor definition typically sets the data members of the new object to some valid default value or to the values of the parameters passed into the constructor. This may be done using assignment statements, a function like Here are a couple of examples:
If your constructor accepts parameters, do not give them the same names as data members of the class unless you are prepared to use the Accessor and Mutator Member FunctionsIf the data members of a class are private, how can code outside the class access or modify them? Since private members are accessible to member functions of the class, we can write a member function to get the value of a data member (an accessor member function) and/or to set a new value for the data member (a mutator member function). The mutator member function may incorporate some code to ensure that the data member can only be set to a valid value. For example:
Since accessor member functions do not modify any data members of the object that called the member function, they should be coded as constant member functions. If an accessor member function returns a reference or pointer to a data member, the return data type should usually be coded as a reference or pointer to a constant variable. Failure to do so could allow the code that called the accessor method to use the reference or pointer that was returned to modify the data member directly, even though it is private. Parameters passed to mutator member function using a reference or pointer should usually be coded as a reference or pointer to a constant variable. The mutator member function code has no need to change the original variable in the calling routine using the reference or pointer and should not be allowed to do so. As the following code example illustrates, we cannot directly access private data members of a class, but we can get the value of a data member or modify the value of a data member by calling a public accessor or mutator member function.
Other Member FunctionsWe can write other member functions for the class as needed. For example, we might write a function to print the data members of an object in a neatly formatted fashion. If a member function does not modify any data members of the object that called it, the function should be coded as a constant member function. C++11 Initialization Option for Data MembersC++11 added the ability to initialize the non-static data members of a class at the time you declare them using a "brace-or-equal" syntax. This is very convenient, and can eliminate most or all of the code from your default constructor. Here are a few examples of the kind of initializations you can do in a class definition:
Even if you initalize all of your data members in your class definition, you may still need to write a default constructor (although the body of the constructor may be entirely empty). If you write at least one constructor, the compiler will not generate a default constructor definition for your class. If you then try to create an object in a way that would call the default constructor, you will get a syntax error. There are two solutions to this issue:
The second solution is used in the code example below. Full Code ExamplePutting this all together, here's the full code for the
Download the full program text and makefile Which type of member function may only be called from a function that is a member of the same class quizlet?A private member function may only be called from a function that is a member of the same class.
When a member function is defined outside of the class declaration?Member functions and static members can be defined outside their class declaration if they have already been declared, but not defined, in the class member list. Nonstatic data members are defined when an object of their class is created.
What is the keyword for a function outside of a class that can access the members within a class?In a class definition, use the friend keyword and the name of a non-member function or other class to grant it access to the private and protected members of your class.
Which type of function is not a member of a class but has access to the private members of the class?In C++, a function that has access to a class's private, protected, and public members is referred to as a friend function. Within the class's body, the friend keyword is used to declare the friend function.
|