how a class works by restricting access to data members, which can only be manipulated by member functions In other words, the implementation details of a class are hidden within the classes themselves Also called “data hiding” or “information hiding”
interfaces Hiding the details, while supplying an interface is an important underlying theme behind classes (user-defined data types) An interface of a class is a list of function prototypes, so the user does not have access to the body (code) of the member functions
software A derived class inherits the data members and member functions of a base class, and then additional data members and member functions are added In C++, you only need the base class object code and the base class header file, which has the class interface (the list of function prototypes)
sale If the user has the object code and header files, then the user can create new software to suit their own needs The vender’s source code is never revealed
“s” is lowercase) For now, we will only declare, pass strings to functions, and output strings See example program at: strings.cpp #include <string> //code . . . string greeting = “Aloha!\n”; cout<<greeting; //Aloha!
the same header files are often included at the top of several files To prevent multiple inclusions of the header file in the final executable file, the header file has these if statements #ifndef NODE_H #define NODE_H //code . . . #endif
list of the prototypes of the member functions of the class public: Node(string, Node *); string getData() const; void setData(string); Node *getNext() const; void setNext(Node *);
the functions, are in a separate file from the header file with the function prototypes and data members See function definitions at: node.cpp The header file is included at the top of the file #include “node.h”
scope resolution operator (::) before the function name, so the compiler knows which class the function belongs to Node::Node(string data2, Node *next2){ data = data2; next = next2; }
no need “Node::” in front of function name Overloading operator<<(), so that the data member “data” is displayed for that node ostream &operator<<(ostream & output, const Node & node){ output<<node.data; return output; }
environment of functions Bottom of stack segment contains heap storing data using “new” and “delete” Data Segment Code Segment runtime stack heap Stack Segment Stack Segment
function call creates a local environment (return address, arguments, local variables, return value) Function call: push stack frame Function exit: pop stack frame Grows from top of stack segment downwards
using keywords “new” and “delete” At bottom of stack segment and grows upwards When the runtime stack and the heap meet, your program is out of memory, which may occur if you don’t use “delete” to deallocate memory
memory on the heap at execution time (while program is running) Create and store new nodes Use keyword “new” to allocate memory on the heap Returns a pointer to the place in memory on the heap Node *node1 = new Node("apple", NULL);
on the runtime stack The address of “new Node” object is stored in “node1” The “new Node” object is stored on the heap cout<<&node1; //0xffbffa34 cout<<node1; //0x23798 cout<<node1->getData(); //"apple" cout<<node1->getNext(); //0 (NULL)
“node1” with the address to a Node object on the heap The heap with a Node object with data members data and next runtime stack heap "apple" NULL data next 0x23798 node1 0xffbffa34
“node1” and “node2” with address to objects on the heap The heap with the new Nodes pointing from the first to second node "apple" 0x237a8 data next 0x23798 node1 0xffbffa34 0x237a8 node2 0xffbffa1c “banana" NULL data next
can take a pointer and move from one node to the next Node *pointer = node1; cout<<*pointer; //“apple” pointer = pointer->getNext(); cout<<*pointer; //“banana”
stack is pointing to 1st node object on the heap Node *pointer = node1; "apple" 0x237a8 data next 0x23798 node1 0xffbffa34 0x237a8 node2 0xffbffa1c “banana" NULL data next 0x23798 pointer 0xffbffa18
stack is pointing to 2nd node object on the heap pointer = pointer->getNext(); "apple" 0x237a8 data next 0x23798 node1 0xffbffa34 0x237a8 node2 0xffbffa1c “banana" NULL data next 0x237a8 pointer 0xffbffa18
(node-driver.cpp), we can now loop through all the nodes from the first node to the last node, which has NULL for next Node *i = NULL; for(i=node1; i!=NULL; i=i->getNext()){ cout<<*i<<endl; }
and link together, so the makefile is a little more complicated The C++ program files are: node-driver.cpp, node.cpp, and node.h The makefile is: makefile-node
is that you can fill up the heap, so your program should release space no longer needed Use keyword “delete” to deallocate memory Deallocates the memory on the heap referred to by the pointer so the memory can be reused delete node1;
by links (pointer to next node) Pointer points to (has address of) the first node Next pointer refers to (has address of) next node Last node has a NULL next pointer Insert and delete only at the top of the linked list
strings See header file with interface (function prototypes): stack.h See function definitions: stack.cpp See driver (test) program: stack-driver.cpp See makefile: stack-makefile
points to current top Node, and point the “top” data member to this node void Stack::push(string data){ //new Node on heap points to top Node *newTop = new Node(data, top); //top points to new Node top = newTop; }
all of the functions and makes sure they are working properly In particular, we tested the overloaded assignment operator=() to make sure it makes an independent copy of the stack See driver (test) program: stack-driver.cpp Compile and like all the files with: makefile-stack