Self-Referential Classes

A self-referential class contains a pointer member that points to a class object of the same class type. For example, the definition

class Node
{
public:
 Node( int ); // constructor
 void setData( int ); // set data member
 int getData() const; // get data member
 void setNextPtr( Node * ); // set pointer to next Node
 Node *getNextPtr() const; // get pointer to next Node
private:
 int data; // data stored in this Node
 Node *nextPtr; // pointer to another object of same type
}; // end class Node

defines a type, Node. Type Node has two private data membersinteger member data and pointer member nextPtr. Member nextPtr points to an object of type Nodeanother object of the same type as the one being declared here, hence the term "self-referential class." Member nextPtr is referred to as a linki.e., nextPtr can "tie" an object of type Node to another object of the same type. Type Node also has five member functionsa constructor that receives an integer to initialize member data, a setData function to set the value of member data, a getdata function to return the value of member data, a setNextPtr function to set the value of member nextPtr and a getNextPtr function to return the value of member nextPtr.

Self-referential class objects can be linked together to form useful data structures such as lists, queues, stacks and trees. Figure 21.1 illustrates two self-referential class objects linked together to form a list. Note that a slashrepresenting a null (0) pointeris placed in the link member of the second self-referential class object to indicate that the link does not point to another object. The slash is only for illustration purposes; it does not correspond to the backslash character in C++. A null pointer normally indicates the end of a data structure just as the null character ('') indicates the end of a string.

Figure 21.1. Two self-referential class objects linked together.


Common Programming Error 21.1

Not setting the link in the last node of a linked data structure to null (0) is a (possibly fatal) logic error.


Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

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