- Understanding linked list copy constructor - 3 Updates
- About static const member in template class - 2 Updates
- OT: Education - 2 Updates
- About static const member in template class - 8 Updates
- How to call virtual functions in C++ - 1 Update
- Why a const function cannot return a non-const reference to a member - 1 Update
Paul <pepstein5@gmail.com>: Jun 21 09:02AM -0700 The code below is copy-pasted from a university website. I don't understand the need for void make_empty() which is called in the copy constructor. This function deletes all the nodes in the same way the destructor does. Also, I've read that you almost never need to invoke the destructor explicitly. If you shouldn't need to invoke the destructor explicitly, then surely you also shouldn't need to call a function which is an exact copy-paste of everything the destructor does. Paul BEGIN PASTE This page presents an example of a class that implements an ordered list abstract data type using a linked list. The page begins with the class's header file; following the header file is the class's implementation file. The type of objects that a list contains is defined by the typedef statement near the beginning of the class's definition section. The comment accompanying the declaration specifies operations that must be defined for the Item type, since the class's functions apply those operations to Items. // FILE: llist.h // CLASS PROVIDED: List - an ordered list of Items. // // TYPEDEF for the List class: // typedef _____ Item; // Item is the data type of the items in a List. It may be any of // the C++ built-in types (int, char, etc.) or a class with a default // constructor, an assignment operator, operators to test for // equality (==) and inequality (!=), a comparison operator (<), and // output via the inserter (<<). // // CONSTRUCTORS for the List class: // List( ) // Postcondition: The List has been initialized as an empty List. // // List ( const List& source ) // Postcondition: The invoking list has been initialized as a copy of // the source List. // // MODIFICATION MEMBER FUNCTIONS for the List class: // void make_empty ( ) // Postcondition: The List has been re-initialized to be empty. // // void insert ( const Item& entry ) // Precondition: The list does not currently contain the value entry. // Postcondition: entry has been inserted in the position appropriate // to its value. // // void remove ( const Item& target ) // Precondition: The list contains the value target. // Postcondition: target has been removed from the List. // // void operator = ( const List& source ) // Postcondition: The invoking list has been assigned a copy of the // source list. // // CONSTANT MEMBER FUNCTIONS for the List class: // bool empty( ) const // Postcondition: If the invoking List is empty, 1 (TRUE) has been // returned; if the List is not empty, 0 (FALSE). // // size_t length( ) const // Postcondition: The return value is the length (number of Items) in // the List. // // bool present ( const Item& target ) const // Postcondition: If target is in the list, 1 (TRUE) is returned, // otherwise 0 (FALSE). // // int kth ( int k ) const // Precondition: The list is not empty and 1 <= k <= length(). // Postcondition: The kth element in the List is returned. // // FRIEND FUNCTION for the List class: // friend ostream& operator << ( ostream& out_s, const List& b ) // Postcondition: The contents of the List b have been written to the // output stream out_s. // // VALUE SEMANTICS for the List class: // Assignment and the copy constructor may be used with List objects. #ifndef LIST1_H #define LIST1_H #include <cstdlib> // Provides size_t #include <iostream> // Provides ostream namespace CSCI301_list { class List { public: // TYPEDEF typedef int Item; // What can go in a list // CONSTRUCTORS List( ) { first = NULL; } // Inline List( const List& source ); // Copy constructor // DESTRUCTOR ~List( ); // MODIFICATION MEMBER FUNCTIONS void make_empty ( ); void insert ( const Item& entry ); void remove ( const Item& target ); void operator = ( const List& source ); // CONSTANT MEMBER FUNCTIONS bool empty( ) const { return first == NULL; } // Inline std::size_t length( ) const; bool present ( const Item& target ) const; Item kth ( std::size_t k ) const; // FRIEND FUNCTION for the List class: friend std::ostream& operator << ( std::ostream& out_s, const List& l ); private: // DATA MEMBERS struct Node { Item data; Node *next; }; Node *first; // PRIVATE FUNCTION Node* get_node ( const Item& entry, Node* link ); }; }
Subscribe to:
Post Comments (Atom)
|
No comments:
Post a Comment