Summary

 < Day Day Up > 



A class declaration introduces a new data type. A class type object is different from a fundamental data type object because of the expanded role it can play in a software system.

There are four special functions: default constructor, copy constructor, copy assignment operator, and destructor. The default constructor is a constructor that has either no parameters, or all parameters have default values so it can be called with no arguments. The copy constructor is used to create new objects from existing objects. The copy assignment operator sets the attributes of an existing object to the attribute values of another existing object. The destructor is used to tear down or destroy an object when it is no longer needed by the program. It is especially important to use the destructor to release any system resources an object may have access during its lifetime. An example of such a system resource is dynamic memory.

Default, compiler-supplied versions of these functions may not perform as you intend for your class objects so you should implement each special member function to ensure proper object behavior. The objective of implementing the special functions explicitly is to have well-behaved objects.

Accessor functions implement object behavior without changing the state of an object. Mutator functions implement object behavior while at the same time changing an object’s state.

There are three member access specifiers: public, protected, and private. The public access specifier allows horizontal access to class and instance data members and member functions. The protected access specifier allows access vertically but blocks horizontal access. Vertical access is discussed in detail in chapter 13. The private access specifier blocks both horizontal and vertical access.

Data encapsulation is the act of declaring data members private and supplying public interface functions to manipulate or access those data members.

Class member functions can be overloaded, and class member functions can call other class member functions. Sometimes it is a good idea to supply private member functions that are called by the public class interface functions.

It is good programming practice to declare classes in separate header files. Doing so affords better control of a project’s physical complexity and enables the creation and distribution of class library code.



 < 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