Introduction

 < Day Day Up > 



Functions provide a convenient way to package program behavior into manageable units with an eye towards reuse. However, as you will soon learn, writing effective and efficient functions takes more than simply breaking a large program into arbitrarily sized blocks of code.

In your studies of C++ and object-oriented programming you will be confronted by the terms function and member function, and wonder what is the difference between the two. The difference lies not in shape and form but rather in how each is employed.

When you think in terms of functions you are thinking in terms of program functional decomposition and the C- style way of writing programs. Chapter 3 provided a complete example of this approach to implementing a solution to a programming problem. In C-style programming the data structures required to implement a program are established and followed by the functions that manipulate those data structures. Although they are highly cohesive, which is a good thing, they are at the same time tightly coupled to the problem being solved via the file scope variables, which is a bad thing. It would be hard to reuse the functions appearing in chapter 3 in another program without rewriting them.

In C++ programming a member function is the term used to describe a function defined as part of a structure or class interface. A member function is a critical component of the data structure itself. There is a different type of thinking that goes into the creation of member functions. A member function is, by its very nature, tightly coupled to the member attributes of the class in which it appears because of the free access it has to those attributes. But now that is a good thing because when you think in object-oriented terms you stop thinking in functional decomposition terms, and think instead of a program’s objects and the messages they pass between each other. These messages are passed via member functions. This is discussed in detail in chapters 11 and 12.

As you study object-oriented programming in general you will encounter the term method used interchangeably with the term member function. (e.g., “Invoke a method on an object...”) The term method has its roots in the Smalltalk programming language. In C++ you declare and define functions, not methods, although it is safe to think of one being like the other.

Ultimately, the only significant difference between a function and a member function is the function exists on its own, whereas the member function is declared as being associated with a class of objects, and thus has free access to the internals of those objects.

In this chapter I intend to show you how to write functions. Everything you learn here will apply directly to writing member functions. This will require you to understand and master several key concepts regarding functions to include function declaration and definition, function argument passing, and function variable scoping rules. You will learn how to write functions that minimize their connection or coupling to the outside world while maximizing the cohesion of the statements that comprise the body of the function. I will also show you how to share the functions you write via function libraries.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

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