.Open CSci202 Computer Science II, Session 20 Review (previous): STL Algorithms .See ./19.html .Open Things not on the final Namespaces, union, bit fields, enum, union, struct. Function objects and using them in algorithms. You don't have to remember how to use all the algorithms. But I may show you a use and ask you to figure out what it does. .Close . Preparation Study this page, an earlier CSci202 final .See ./final2002.pdf and use the book's index, this web site .See ./search.php , and the handouts, and the lab work, and experiments on the machines, to fill in the gaps in your knowledge. Write down the questions, doubts, and surprises that you have on a piece of paper. Submit the most critical question to me to earn credit and generate the class work. Include the chapter number and pages numbers + a topic. . Assigned Work Due -- Submit a question . What is the most rewarding program that you've done that is still running? .See ../cookie.php . Will the final include bitwise operations Yes. . Will there be a Table of Binary numbers fill in the blank type of question on the final? No. That is arithmetic. . Will questions from all four previous quizzes be present on the final Not exactly. I make small changes so that memorizing the old answer and thoughtlessly writing it down gets few points.. . What is the most useful topic to study over the summer to prepare for CS330? Login and write a program every week. Pointers. . Chapter multiple pages many -- sorting do we need to know all types sorting an what they do for the final? You need to no the names, properties, and general forms of these sorting algorithms: .List Bubble Sort Heap Sort Insertion Sort Merge Sort Quick Sort Selection Sort Slow Sort :-) .Close.list (Alphabetical list). . Chapter pages -- C++ Are the majority of computer programs written in C++? Hard to say. Nearly all compilers, operating systems, browsers, word processors, games engines, games scripting languages, spread sheets, .... etc are written in either C or C++. You find other languages being used more than C++ in other areas. Programs that work with the web tend to be written in Java, Javascript, PHP, Python, Ruby, ASP, JSP, or some combination of these. C++ is too insecure for web work. Each area has a favorite language at a given time... take .See ../cs320/ for details. . Chapter 8 -- Pointers Can you give an overview of how pointers work? See .See ./pointers.html (notes), .See ./uml.pointer.png (Exam[ple pointer in C++ and UML), .See ./04.html (Questions and class work), .See ./12program.html (pop up simulation of RAM and pointers). The UML notation for objects. .See ./ch08c.gif .See ./ch08d.gif To find out what pointers and linked data are doing use fake addresses and the UML notation for objects. .See ./lnadrs.gif . Chapter 22 pages 1063 -- Pointers For pointers, we know that it points to a type of element in a container. But why is that so significant? What if we do not use pointers at all? Well pointers do not always point to items in containers. They can point at anything in RAM or even to a device that has an address but is not primary memory -- for example a peripheral. Without pointers you can not have linked or dynamic data. You are limitted to arrays. Lots of subscripts remembering where things are in the arrays. Basically the kind of code I had to write in the 1960's. Painful. And your programs will probably run slower as well:-( . Chapter 14 page 761 Template classes Could you give a simple rundown on template classes using a brief example? In the bad old days, before tempates, we had to design and code a new set of classes every time we wanted a linked list -- .See ./elist.jpg See example below. . Chapter 20 pages ppp-ppp -- Linked lists Can you go over linked lists and their implementation and corresponding UML diagrams please? Teacher draws the classic "Link" template class on the board (Doubly linked) in UML.... shows how to code it... how to simulate it in the UML notation.... and how to test it(if time). .See ./dlinked.png (UML diagram of List, Link, and data) . Chapter 8 - Pointers and call by reference Can you give an overview of how to pass arguments to functions by Reference with pointers? First, note that the best way to pass references to a function is to use a reference argument: .As_is type function( ...., type & reference, .... type value, ... ) ... Doing it with pointers is an academic exercise. This means the function is given an address by the compiler and actions done to the reference actually apply to the adressed object. Example .As_is void treble( double & x ) { x = 3* x;} is called like this .As_is treble(me); and `me` is multiplied by 3. A pointer is a variable or argument that is declared like this .As_is type *pointer; Its value is an `address` or the constant `NULL`. If it is NULL then it does not go any further..... but if a pointer is not NULL then it contains an address in the main memory of the computer. In that address you will find some data. This is shown as .As_is *pointer To get the address of a variable we write .As_is &variable So to get the effect of pass by reference with the treble function we would replace '&x` by `*x` and inside the function `x` by `*x`. In the call we would have to add an `&`. Miss any of these and the program has an error. Here is the correct call .As_is treble( &me ); And here is an attempt at the function.... can you spot the error? .As_is void treble( double * x ) { x = 3* *x;} . Chapter 8 pages 418-425 -- Pointers and pointer-based Strings can you go over using const with pointers? This is hairy I had to look it up. It is all a question of where the "const" and "*" are. .As_is Type const *pointer; The pointer points at a constant object -- an object that can not change its data. .As_is const Type * pointer; .As_is Type * const pointer; This is a constant pointer... it can only point at one object.... but the contents of the object can change. From here on in it gets complicated:-) . Polymorphism Can you go over Polymorphism Polymorphism makes pointers work with inheritance. Example: .As_is class Animal { virtual string noise() { return "?"; } }; .As_is class Cat:public Animal { string noise() { return "miaow";} }; .As_is class Cow:public Animal { string noise() { return "moo";} }; .As_is class Dog:public Animal { string noise() { return "woof";} }; .As_is class Chihuawa:public Dog { string noise() { return "";} }; UML -> board. We need a pointer to Animal to get polymorphic behavior: .As_is Animal * p = farm.begin(); .As_is .... .As_is ...p->noise()... If we had time we could write a program to simulate "Old MacDonald's Farm" etc... Notice -- if the "virtual" is omited in Animal, then all `p->noise()` returns "?" whatever object `p` points at. We have lost polymorphism. For more see: .See ./polymorphism.html . Chapter 10 pages 548 -- Friend functions and classes Can you briefly go over friend functions and classes? A class can declare that a function is a friend. This means that the compiler lets the friend function access to the private data and functions inside the class. A cleague defines a friend as someone you trust to go into your garage and borrow a tool to get a project done. Some one permitted, and expected, to take things and do things to and with them. They are most used for creating in/output opertors for classes. To get the right syntax: .As_is stream << object << anotherObject << .... the `operator <<` can not be a member of the 'objects' class. But it almost certainly will need access to the data in the object to output it. So we often declare the class of object and include inside the class '...friend operator<<(....)...' as a quick way of getting the effect we want. The other main use is when you are programming both the function and the class, and so you trust yourself to do the right thing with the private stuff in the class. It is not good software engineering to use friends. The increase `coupling` and `dependencies` between classes (UML on board) which tend to increase the chance of bugs and unmaintainable code. Also note: in a complex project a wise programmer separates the classes that handle the user interface (input/output) from the classes that handle aspects of the problem. They carefully add functions so that the classes can be safely changed without breaking each other. We discuss how to do this in .See ../cs375 (advert). . Chapter Review pages ppp-ppp -- Protected Private How do you change private to protected? With an editor! Why change private to protected? So that derived classes have access to the private stuff in the base class. In essence it makes the children of a class friends of the parent..... but with limited access. . Chapter 15 pages 794 -- iostream manipulation How do you align the output in the relation to the left side, not to the previous output? E.g.: .As_is Franklin 15 .As_is Bob 60 .As_is Lars 50 The simplest technique is to use the tab character '\t' -- but this does not work in all cases. It depends on the user having their tabs set to the place you have assumed. It also needs the the first field to be a moderately consistent width. The professional C++ solution is to use the iomanip library... I will assume you have two data items to output -- string name; int number; for an example. The next step is to decide how many symbols you allocate to each: .As_is Franklin 15 .As_is <---8--><-----whatever (allow one space for the sign of the number (+ is mapped to space). Then we get .As_is cout << setw(8) << name << number << endl; But does it work? No. The strings are right-aligned by default: .As_is cout << setw(8) << left << name << number << endl; Now the "number" has no space in front of it... .As_is cout << setw(8) << left << name << setw(3)<< number << endl; But we still have to right-align the number in its 3 character field: .As_is cout << setw(8) << left << name << setw(3)< > simap; This declares `simap` to be a map from a string to a vector of `int`s. In math simap : string -> #int, where #int is a number of integers. This means that .As_is simap["abc"] can be a sequence of number like { 91,92,93}, for example. In turn this means that .As_is simap["abc"][1] would be 92. As an example ... suppose I wanted to keep track of scores in a course, and didn't have a spreadsheet. Then each student would have a name to identify them -- a string. And each student would have earned a number of scores: .As_is score["John Doe"].push_back(18); would give `John Doe` a score of 18 points. To add the scores up I could use a .As_is std::map >::iterator. About now I would go back an declare an abbreviation! .As_is typedef std::map > GradeBook; I can list the total scores by something like this: .As_is for(GradeBook::iterator i=score.begin(); i!=score.end(); i++) .As_is { const string name= (*i).first; .As_is const & vector scores=(*i).second; .As_is int total = accumulate(scores.begin(), scores.end(), 0); .As_is cout << name << "\t"<