Class Member Functions


The member functions are the interface between the class objects and the outside world. More than one member function of a class may have the same name as long as they have different signatures. As you should recall from a previous lecture, this is called overloading of functions and will be discussed in more detail below.

Two classes may have member functions with the same name and even with the same signature. Since these member functions are from different classes, this does not make a difference because a member function's name really consists of the visible function name as well as the name of the class. As you may recall from the example account.cpp discussed above, when the member functions were defined outside of the class, the remainder of the function's name (i.e. the class name) preceded the function's name and they were separated by the scope specifier operator: ::

Both of these concepts are examples of functions with the same name are what is called polymorphism. That this they have the same "form".

The question is then: How is data placed into an object's memory and how is it possible to access these data values?

Initialization and Access Member Functions

The attributes are given values by initializaing member functions. These functions require no special code. They must just permit the data to be loaded into the attributes. (It is recommended that an initializing member function's name begin with the word: set) This type of function is sometimes called the data member's mutation function because it "mutates" or changes the value contained in an object's data member.

The values of the attributes are accessed by access member functions. (It is recommended that an access member function's name begin with the word: get.)

For example look at the class TheDate defined below:

image from book

 class TheDate {    private:       int theMonth,           theDay,           theYear;    public:       void setDate(int inMonth,int inDay,int inYear)       {          theMonth = inMonth;          theDay = inDay;          theYear = inYear;       }       int getMonth()       {           return theMonth;       }       int getDay()       {           return theDay;       }       int getYear()       {           return theYear;       } }; 

image from book

As you can see from the class definition above, the member function setDate() initializes the attributes: theMonth, theDay and theYear of the calling object by assigning the values of the arguments to the object's attributes. In the example above, setDate() is an initializing member function because the data members could be initialized in the following manner:

image from book

 TheDate dob; dob.setDate(2,20,2006); 

image from book

The class: TheDate just considered above contains the methods: getMonth(), getDay() and getYear(). These methods are called access member functions because through them, it is possible to access or get the object's attributes' values. That is the value stored in an object's data member: theMonth may be accessed through the member function getMonth() in the following manner:

image from book

 cout << dob.getMonth(); 

image from book

In general, each attribute should have an access member function so that the value stored within an object's attributes may be accessed.

Since the reason for having data members is to place data in them, there must be at least one initializing member function for all data members or one for each data member. For example see clock.cpp and bank1.cpp. Which of the methods defined in these examples would initialize the data members of an object? That is, which are the initializing member functions? Which of the methods in these examples would provide access to the data in the attributes of an object? That is, which are access member functions? Copy each of these examples into your compiler, compile and run them.

Constructor Member Functions

When a class object is defined, memory is set aside, the memory is labeled and restrictions are placed on that memory. The setting aside of the memory for an object is done by member functions called constructor member functions. If the C++ programmer does not define one explicitly (an explicit constructor), the compiler will create an implicit constructor (the compiler creates the default constructor).

The default constructor has a void argument. Instead of being defined by the compiler, a default constructor may also be defined by the programmer and therefore it would be an explicit default constructor. A default constructor has no arguments so a constructor that has arguments is called a non-default explicit constructor. If a non-default constructor is defined, it is recommended that a default constructor also be explicitly defined as well. The reason for this is that an array of objects may need to be defined and in this case the default constructor would be required.

While the C++ compiler will create an implicit constructor if one is not explicitly defined, the programmer should always define at least one constructor explicitly so that the constructor will have the characteristics desired and one of these constructors should be the default constructor.

The constructor functions have special characteristic that distinguishes them from other member functions. A constructor:

  • has the same name as the class

  • has no return data type not even void

  • may have a void or a non void signature

  • should not be called explicitly (but can be)

  • is created by the compiler if none are explicitly defined

  • can be overloaded

  • must be defined or declared in the public access section

  • can not be a virtual function (to be discussed later)

Remember that each time an object is defined, it calls a constructor. Therefore C++ constructors must be public member functions in order to permit objects to be defined.

If an array of objects is going to be defined, then the class must have a default constructor whether supplied by the compiler or supplied by the programmer. For example the following definition:

image from book

 className objectArray[numberObjects]; 

image from book

would call a constructor for each object of the array. Since it would be impossible to supply arguments to a constructor for each object in the definition of the array, the default constructor is called for each element of the array.

For several examples of classes with explicit constructors see bank2.cpp, callcons.cpp and bank3.cpp. Copy each of these examples into your compiler, compile and run them.

Destructor Member Function

In addition to constructing the object when it is defined, it is necessary to destroy or reclaim the object's memory after it is no longer needed. To accomplish this objective, C++ provides for a type of method called the destructor member function. This function returns all memory used by the object and performs other housekeeping jobs. As with constructors, the compiler creates an implicit destructor if the programmer does not define one explicitly in the class definition (an explicit destructor).

It is recommended that the programmer define a destructor even when it is not necessary to create one. As you probably have noticed, there are several software products on the market that do not end properly when you exit them nor do they give back the memory when they end. Sometimes you will get an error that claims all of the system resources are used. To avoid this in your own programs, one way would be to create an explicit destructor for each class to reclaim the memory for each object that is defined this is especially true when the attributes may be pointers.

A destructor:

  • has the same name as the class preceded with ~

  • has no return data type not even void

  • has only a void signature

  • there is only one per class

  • is created by the compiler if one is not defined in the class definition

  • must be defined or declared in the public access section.

  • is called automatically when an object leaves scope

For an example of an explicit destructor, see consdesc.cpp. Copy this program to your compiler, compile and run it. Notice when the various objects are defined and destroyed.

Overloaded Member Functions

In the discussion previously, overloaded functions were defined as functions within the same scope that have the same name but have different signatures. Member functions are within the same scope namely the class' scope. Therefore if two different member functions have the same name but have different signatures, then they are called overloaded member functions.

By combining the non default constructor of bank2.cpp with the default constructor of bank3.cpp into one class, a class with two or more constructors can be defined and therefore overload the constructor's name. For example in bank4.cpp the class BankAccount is defined with both of the following constructors:

image from book

 BankAccount(void) {   amount = 0.00; } BankAccount(float startingAmount) {   amount = startingAmount; } 

image from book

When objects are defined, the definitions can use either one of these constructors. For example, when the objects: checkingAccount and savingsAccount were created in bank4.cpp, their definitions were done like the following:

image from book

 BankAccount checkingAccount; BankAccount savingsAccount(5000); 

image from book

In the first case the default constructor initialized checkingAccount to $0.00 while the second definition used the non default constructor to initialize savingsAccount to $5000.00. bank4.cpp is an example of using overloading because the constructor functions both have the same name yet they have different signatures. The signatures are different because the first constructor has a void argument and the second has an argument list consisting of one argument of type float.

Member functions may be defined either in the class definition or they may be declared in the class definition and defined outside. If they are defined outside of the class definition, they must be declared by using the functional prototype within the class definition. Whenever a member function is defined outside of the class definition, the member function's definition must have the function's name preceded by the class' name with the scope specifier operator (::) between the class' name and the name of the method. For example, see crclclss.cpp

Inline Member Functions

Those member functions (remember that they are also called methods) that are defined within the class definition will be expanded to inline member functions assuming that the compiler will carry out this request. Those member functions that are defined outside of the class definition are not inline unless the programmer specifically requests that they are to be inline. In the case the member functions defined outside of the class definition are to be inline then the keyword inline must precede the definition of the member functions. For example, see nlinclas.cpp

Member Functions with Default Values

It is also possible for a class to have member functions with arguments with default values. For example:

image from book

 class Ratio {    private:       int top, bottom;    public:       void setRatio(int t=1,int b=1)       { top = t;         bottom = b;       }       void showRatio()       {  cout << top << "/"               << bottom;       } }; 

image from book

Using setRatio(), it is possible to define objects of Ratio and initialize them as in following statements:

image from book

 Ratio aRatio; aRatio.setRatio(); 

image from book

or the following:

image from book

 Ratio bRatio; bRatio.setRatio(3,4); 

image from book

For examples with default values in the arguments, see defltcon.cpp, bankacnt.cpp, bank5.cpp, banktotl.cpp and bankttl2.cpp.




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net