Comp 2673, Spring 2002
April 29, lecture notes

Reading assignment: Finish reading Chapter 9 in Deitel and Deitel. You should skip Section 9.7, 9.15 and 9.16 More of job control: - Last Wednesday we learned about Ctrl-c, Ctrl-z, % command &, bg, fg, top, nice, and renice. - Often you need to know the process id of a job or process you've got running. How do you find it? The ps (process status) command tells you. There are many options, including -a (all of the processes of most interest) and -f for a full listing. ps can be used more creatively, as in % ps -a -o pid -o pcpu -o user -o comm - You can use this process id as an argument to kill: % kill 23456 kills process 23456, provided you own it! - You can use this process id as an argument to renice: % renice 12345 changes the priority on process 12345 More on pipes: - Recall that % command > outfile runs the command "command", and send the output (standard out, cout) to the file "outfile" instead of sending it to the screen. Similarly, % command < infile runs the command "command", and takes the input (standard in, cin) from the file "infile" instead of from the keyboard. - Sometimes you don't want the output of a program to go a file, but want it to go straight into another program - that is what a pipe does. So % command1 | command2 takes the standard output of command1 and makes it the standard input to command 2 - More examples How do you get a listing of a directory to go to the screen in pages? % ls -l | more How do you find out how many files are in your directory? % list | wc -w How do you see what the last "ps" command you typed was? % history | grep ps How do you see a sorted list of everyone logged onto the machine? % finger | sort | more More on inheritance: - The difference between inheritance and composition. A class can have an object as a data member, and this is known as composition. Here's a simple example: class Point { private: float x,y; } class Shape { public: Person(); private: Point location; } This is NOT inheritance, it is composition. You can tell the difference by answering the question is it "has a" or is it "is a"? A Shape *has a* location. But a Shape *is not* a location. - Constructors We learned that a constructor for a derived class can call the base class constructor - this is something you often want to do, and we saw an example of this last week. If the derived class does not explicitly call a base class constructor, then the base class default constructor is automatically called. The constructor for the base class is called BEFORE the constructor for the derived class runs. This is reminiscent of how classes work with composition: if a class has an object as a data member, then the constructor for data member object is called BEFORE the constructor for the main object itself. - Destructors Destructors are called in the opposite order of constructors. So the destructor for the object itself is called before the destructor for the data members and the destructor for the derived class is called before the destructor for the base class - Functions in the base class and the derived class If you have a member function in a derived class with the same name as a function in the base class, then you have overridden the base class function. In other words, if you call the function with a derived class object, the derived class version is called. If you want to call the base class version, then say BaseClass::functionname - Derived class pointers vs base class pointers If you have a derived class object, then you can point to it either with a base class pointer or a derived class pointer - you can treat it either way. If you have a derived class pointer you can static_cast it to a base class pointer - this is a reasonable thing to do. If you have a base class object, then you can point to it with a base class pointer, and by casting it you can actually point to it with a derived class pointer, but this is dangerous! If you subsequently try to access derived class members, you've made a severe logic error.