Comp 2673, Spring 2002
April 12, lecture notes
Reading: Finish reviewing Chapters 6-8 in Deitel and Deitel Case Study Continued: Here's the declaration (from last lecture) of the class Maze: maze.h Here's an implementation of some of the more interesting functions in this class: maze.cpp Last time we didn't talk about overloading the << operator. We'd like to be able to display the maze simply by saying: cout << mymaze; Recall that if I have two objects c1 and c2, then the compiler translates c1*c2 into c1.operator*(c2). There's a second way this operator can be overloaded. If the class doesn't have a member function called operator*, then the compiler next looks for a matching nonmember function, translating c1*c2 into operator*(c1, c2) This second method is one way to overload <<, because the first method can't work. If we try translating cout << mymaze using the the first (member function) method, notice that the compiler translates it to cout.operator<<(mymaze), and so operator<< would not be a member function of the Maze class, it would be a member function of the ostream class! Since the ostream class is standard, we can't add member functions to it. Instead, we use the second method of overloading operators, making a friend non-member function to handle output of Mazes: friend ostream & operator<<(ostream &output, const Maze & mz); This function gets called because the compiler translates cout << mymaze; into operator<<(cout, mymaze); You can see this function implemented in the link at the top of the page. Note that the function must return the ostream & so that output can be cascaded, as in cout << maze1 << endl; Some more Unix stuff: redirecting input/output, using pipes and filters Unix commands can take input from the standard place (the keyboard) and can send output to the standard place (the screen). But Unix has a super-cool feature that allows you to specify in the command line where you want input and output redirected. Use "<" to redirect input and ">" to redirect output. (warning: > will redirect output by wiping out an existing file, while >> will append to the end of a file) You may want output redirected to a file: % ls -l > list This gives a long listing of your directory and stores it in the file "list" You may want input redirected from a file: % more < list This takes the file "list" and gives it as input to the more command Very often you want to execute a command/program and redirect its output as input to another program, without saving it in an intermediate file This is called a pipe. Here are some examples: % ls -l | more % ls | wc -w % history | grep ps % finger | sort | more The unix command "sort" is an example of a filter - it can run with redirected input and redirected output, so it can be in the middle of a string of pipes.