[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci202] / 20
[Text Version] [Syllabus] [Schedule] [Glossary] [Resources] [Grading] [Contact] [Question] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] <20>
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Mon Jun 15 12:25:36 PDT 2009
Opening the PDF files on this page may require you to download Adobe Reader or an equivalent viewer (GhostScript).

Contents


    CSci202 Computer Science II, Session 20 Review


      (previous): STL Algorithms [ 19.html ]

      Things not on the final

      1. Namespaces, union, bit fields, enum, union, struct.
      2. Function objects and using them in algorithms.
      3. 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.

      Preparation

      Study this page, an earlier CSci202 final [ final2002.pdf ] and use the book's index, this web site [ 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?

      [ ../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:

      1. Bubble Sort
      2. Heap Sort
      3. Insertion Sort
      4. Merge Sort
      5. Quick Sort
      6. Selection Sort
      7. Slow Sort :-)
      (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 [ ../cs320/ ] for details.

      Chapter 8 -- Pointers

      Can you give an overview of how pointers work? See [ pointers.html ] (notes), [ uml.pointer.png ] (Exam[ple pointer in C++ and UML), [ 04.html ] (Questions and class work), [ 12program.html ] (pop up simulation of RAM and pointers).

      The UML notation for objects. [ ch08c.gif ] [ ch08d.gif ]

      To find out what pointers and linked data are doing use fake addresses and the UML notation for objects. [ 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 -- [ 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).

      [ 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:

       		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

       		void treble( double & x ) { x = 3* x;}
      is called like this
       		treble(me);
      and me is multiplied by 3.

      A pointer is a variable or argument that is declared like this

       		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
      		*pointer

      To get the address of a variable we write

       		&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

       		treble( &me );

      And here is an attempt at the function.... can you spot the error?

       		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.

       		Type const *pointer;
      The pointer points at a constant object -- an object that can not change its data.

       		const Type * pointer;
       		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:

       		class Animal { virtual string noise() { return "?";    } };
       		class Cat:public Animal    { string noise() { return "miaow";} };
       		class Cow:public Animal    { string noise() { return "moo";} };
       		class Dog:public Animal    { string noise() { return "woof";} };
       		class Chihuawa:public Dog  { string noise() { return "";} };

      UML -> board.

      We need a pointer to Animal to get polymorphic behavior:

       			Animal * p = farm.begin();
       			....
       				...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: [ 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:

       		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 [ ../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.:

       	Franklin 15
       	Bob      60
       	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:

       	Franklin 15
       	<---8--><-----whatever
      (allow one space for the sign of the number (+ is mapped to space). Then we get
       		cout << setw(8) << name << number << endl;
      But does it work?

      No. The strings are right-aligned by default:

       		cout << setw(8) << left << name << number << endl;

      Now the "number" has no space in front of it...

       		cout << setw(8) << left << name << setw(3)<< number << endl;

      But we still have to right-align the number in its 3 character field:

       		cout << setw(8) << left << name << setw(3)<<right << number << endl;

      In my experience 0f 40 years programming -- getting the look and feel right is iterative process. Lots of trial an error.

      Here [ align.cpp ] is the test program.

      Not the kind of thing I should test in the final.

      Chapter 18 pages 894-915 -- string Streams

      Can you go over string stream processing briefly?

      Used mainly to convert strings into to other data types and vice versa.

      Example [ tss2009.cpp ] , more in the book.

      Chapter 22 -- maps

      What could be a use for a map like this:
       		std::map<std::string, std::vector<int> > simap;

      This declares simap to be a map from a string to a vector of ints. In math

    1. simap : string -> #int, where #int is a number of integers.

      This means that

       		simap["abc"]
      can be a sequence of number like { 91,92,93}, for example.

      In turn this means that

       		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:

       		score["John Doe"].push_back(18);
      would give John Doe a score of 18 points.

      To add the scores up I could use a

       		std::map<std::string, std::vector<int> >::iterator.

      About now I would go back an declare an abbreviation!

       	typedef std::map<std::string, std::vector<int> > GradeBook;

      I can list the total scores by something like this:

       		for(GradeBook::iterator i=score.begin(); i!=score.end(); i++)
       		{	const string name= (*i).first;
       			const & vector scores=(*i).second;
       			int total = accumulate(scores.begin(), scores.end(), 0);
       			cout << name << "\t"<<total<<"\n";
       		}

      Prepare a cheat sheet for the final

    . . . . . . . . . ( end of section CSci202 Computer Science II, Session 20 Review) <<Contents | End>>

    Lab 10 on data structures

    [ lab10.html ]

    Next -- The Final

    [ final.html ]

    Bring your final project 5 (Optional) to the final.

    Also Bring your Cheat sheet.

    Abbreviations

  1. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  2. class::="A description of a set of similar objects that have similar data plus the functions needed to manipulate the data".
  3. Data_Structure::=A small data base.
  4. Function::programming=A selfcontained and named piece of program that knows how to do something.
  5. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  6. KDE::="Kommon Desktop Environment".
  7. object::="A little bit of knowledge -- some data and some know how", and instance of a class".
  8. OOP::="Object-Oriented Programming", Current paradigm for programming.
  9. Semantics::=Rules determining the meaning of correct statements in a language.
  10. SP::="Structured Programming", a previous paradigm for programming.
  11. STL::="The standard C++ library of classes and functions" -- also called the "Standard Template Library" because many of the classes and functions will work with any kind of data.
  12. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
  13. Q::software="A program I wrote to make software easier to develop",
  14. TBA::="To Be Announced", something I should do.
  15. TBD::="To Be Done", something you have to do.
  16. UML::="Unified Modeling Language".
  17. void::C++Keyword="Indicates a function that has no return".

End