Review

The two aspects of object orientation - encapsulation and inheritance - are facilitated in C++ by classes. Classes can be viewed as "blueprints" for objects. In a class, the programmer thus defines all data items that each object of the class should have (the so-called data members of the class) and also defines all operations with the data that can be performed and how (member methods ). By stipulating which of the data members can be accessed from outside the object and by whom - as well as which of the member methods can be called from outside the object and by whom - the programmer manages both the encapsulation and the interface to the outside word. Though the concept of "object" is not very different from the concept of "function" (both are a form of modules), the C++ "class" approach allows for a much more flexible and controlled specification of the interface and of data transfer.

Every class is supposed to have one or more specialized methods that include constructors. A constructor is a blueprint of how to build an object and how to initialize it once it has been built. A constructor with no arguments is called a default constructor. If a class does not have an explicit constructor, the compiler builds a default constructor of its own. A derived class does not inherit the constructor(s) of its base class, yet they may need to be invoked explicitly in the definition of the derived class constructs. Each class is supposed to have a single destructor; if one is not explicitly specified, the compiler provides a default one. A derived class does not inherit the destructor of its base class, which nonetheless is called at the onset of executing the destructor of the derived class.

The operator new is used both to allocate raw memory for a dynamic object and to call the appropriate constructor that actually creates the object. Its counterpart , delete , is used both to call the appropriate destructor and to deallocate the memory. In order to create arrays of dynamic objects, the new[] operator must be used. The operator new[] can only be used in conjunction with the default constructors. Arrays created using new[] must be destroyed using the operator delete[] . The operator new[] , in conjunction with innate data, functions just as a raw memory allocator; for example, x = new char[1000]; simply allocates 1000 bytes. All C++ memory operators can be used with the so-called placement syntax when additional arguments are passed to them. Unlike their global versions, placement-syntax versions must be explicitly defined by the programmer. A particular version of memory allocators using placement syntax (placement- new and placement- new[] , with a single additional argument void* pointer) are in the C++ Standard Library; their task is not to allocate any memory but rather to construct an object (or objects) in memory secured by other means. All memory operators can be overloaded, and thus a class may have its own versions of memory operators.

If an object needs to be copied (passed by value to a function), an explicit copy constructor ought to be provided in the definition of the class. In its absence, a memberwise copy is used by the compiler, which may lead to subtle memory problems as discussed in Chapter 3.

If an object needs to be assigned to another object, an explicit assignment ( operator=() ) ought to provided in the definition of the class. In its absence, a memberwise copy is used by the compiler, which may lead to the same kind of problems as in the missing copy constructor case; moreover, memory leaks can thereby be created.



Memory as a Programming Concept in C and C++
Memory as a Programming Concept in C and C++
ISBN: 0521520436
EAN: 2147483647
Year: 2003
Pages: 64

Similar book on Amazon

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