C++ Programming
Part I INFO1-CE9264
Part II INFO1-CE9265
Part III INFO1-CE9266
http://i5.nyu.edu/~mm64/INFO1-CE9264

The textbook for this course is online.
Part I reviews the language C and covers Chapters 1–2.
Part II covers Chapters 3–6.
Part III covers Chapters 7–10.

Contents

  1. How far did we get and what is the homework?
  2. Catalog descriptions that I wrote for
    1. INFO1-CE9264 C++ Part I
    2. INFO1-CE9265 C++ Part II
    3. INFO1-CE9266 C++ Part III
  3. Links to the NYU catalog
  4. GitHub accounts for students.
  5. C++ Standard. Lambda expressions are in §5.1.2. Examples.
  6. Bibliography and textbook
  7. Prerequisites for INFO1-CE9264 and INFO1-CE9265
  8. Wireless on campus
  9. Platforms and compilers
  10. Syllabus and grading policy for INFO1-CE9264, INFO1-CE9265, INFO1-CE9266
  11. Flag graphic: a C++ program that outputs a color GIF or JPEG.
  12. Bubble sort performed by Hungarian folk dancers.
  13. Game of Life by J. H. Conway
  14. Ring terminal server written in Java for INFO1-CE9266.
  15. Other links for C++ and C++0x.
  16. Source code for examples in book for INFO1-CE9264, INFO1-CE9265, and INFO1-CE9266, and for the INFO1-CE9264 in-class examples.
  17. list of students
  18. Multiple inheritance from a virtual base class is sometimes called diamond inheritance, and is so denominated on p. 102 of the Gosling Java book.
  19. How to post your source code and error messages on the Web.
  20. For the room assignments, see Mark’s home page.
  21. The killer rabbit that attacked President Carter on April 20, 1979.
  22. Class photos

How far did we get?
What’s the homework?

Summer 2013 INFO1-CE9264 (Section 1, Monday)

  1. Monday, June 3, 2013: up to ordinal2.C in the in-class examples. Admire our class photo; next week, we’ll make it touch-sensitive. Read the syllabus and grading policy. Look at the textbook. See how much homework there was last semester.

    You can use any version of C and C++ on any platform for this course. If you’re a Microsoft Windows person and you don’t already have C and C++ compilers, the easiest way to get started is with Bloodshed.

    If you want to use C and C++ with a free Unix account on the Solaris server i5.nyu.edu, get your loginname here. Your loginname is the same as your NYU NetID—two or three lowercase letters followed by several digits. Please email me at mark.meretzky@nyu.edu if you are in the class but your name is not on the list. Get your secret password here. Your secret password is the same as the secret password for your NYU NetID. Compile your program with

    /bin/g++ myprog.C
    ls -l a.out
    -rwx------   1 abc1234     users       6932 Jun  3 15:23 a.out
    ./a.out
    

    Brief notes on various platforms:

    1. Microsoft Windows
      1. Bloodshed: a free GNU C++ compiler. This is the easiest way to get started in C++ on Windows.
      2. Eclipse: a more complicated way to get started in C++ on Windows. You’ll have to do more than just install Eclipse: you also have to plug the C++ plug-in into it.
      3. Microsoft Visual C++: free compiler for PC
      4. Borland: free compiler for PC
    2. Mac OS X
      1. Xcode: download this free for Mac OS X if you don’t already have it. (Macintosh Terminal g++ people: make the same changes to term.c.)
      2. Eclipse also runs on Mac.
      3. Code::Blocks
    3. Unix and Linux
      1. Unix: on your free Solaris Unix account on the host i5.nyu.edu, or on a Macintosh OS X Terminal application.
      2. Linux: install the packages for gcc and gcc-c++ if you don’t already have them. Ubuntu 12.04 desktop Linux: to get the term.c program (Chapter 1, pp. 85–89) to compile and link, you must install these additional packages: libncurses5-dev (5.9-4) and libtinfo-dev (5.9-4).

    Look at the table of operator precedence and associativity on p. 6 of Chapter 1; multiplication and division are on line 13. Try all the programs we looked at in class. Can you find the header file iostream on your machine? What are the values of INT_MIN and INT_MAX on your machine? What happens if you add 1 to a variable that already holds INT_MAX? What happens if you subtract 1 from a variable that already holds INT_MIN? What happens if you leave a variable uninitialized or divide by zero? What are the quotient and remainder when you divide a negative number by a positive number? How long does a for loop take to loop through all 4,294,967,296 integer values? (Don’t write the commas: they’re only for human beings.) On the NYU Solaris machine i5.nyu.edu, it took 44 seconds. Don’t hand in any of the above—just do them.

    Hand in a program that inputs and outputs one or more integers, and does something interesting in between. For example, when does the turkey come out of the oven? What is Bill Gates’s net worth today? Hand in the graph paper homework in the in-class examples immediately after graph3.C where it says “Homework”. Read ahead in the in-class examples so you can see what we’re doing next week.

    For each program that you hand in, please hand in the C++ program itself and the output. On paper. And I’d be delighted to look at anything you want to show me in C or C++, even stuff that has nothing to do with the course. Surprise me!

  2. Monday, June 10, 2013: up to rose.C in the in-class examples.

    Hand in the flag homework, but do a country whose flag is more interesting than France’s. A color printout is not necessary; black and white is fine. Hand in the program and the picture, but don’t hand in the 60,000 lines of numbers.

    Also hand in the date homework.

  3. Monday, June 17, 2013: up to p. 49 in Chapter 1. I corrected the errors on pages 45 and 47.

    Hand in a C++ program consisting of at least two .C or .cpp files. Create variables and functions in one of the files; use the variables and functions in another of the files. Also create a .h file that declares the names of the variables and functions, the data types of the variables, and the data types of the arguments and return values of the functions.

    I showed you two ways of writing bubblesort: with subscripts, and rewritten with pointers on pp. 47–48 of Chapter 1. Replace the variable i in this moving average program with a pointer named p. If you want, you can also replace j with another pointer.

  4. Monday, June 24, 2013: up to p. 102 in Chapter 1.

    Run (but do not hand in) the program in Chapter 1, pp. 85–89, but don’t hand it in. It consists of three files:

    1. term.h (p. 86; acceptable to C and C++) and term.c (in the language C)
    2. main.C (p. 87; in the language C++)
    See the links in the above June 3 homework for information about compiling and linking the program on different platforms.

    Read ahead about objects in Chapter 2.

  5. Monday, July 1, 2013: up to Chapter 2, p. 131. Run and compare version1.C (pp. 105–108), version2.C (pp. 108–111), and version3.C (pp. 111–117), but don’t hand them in. Do Homework 2.1.3a (p. 117), but don’t hand it in. Hand in Homeworks 2.4a (p. 126) and 2.4b (p. 129). Read ahead.
  6. Monday, July 8, 2013: up to Chapter 2, p. 163. Hand in Homeworks 2.6a (p. 152) and 2.6b (p. 152). After you have run the program in pp. 85–89, run (but do not hand in) the program in pp. 157–163 consisting of the following five files:
    1. term.h (p. 86 in Chapter 1; acceptable to C and C++) and term.c (in the language C)
    2. terminal.h and terminal.C (in the language C++; pp. 159–161 in Chapter 2)
    3. main.C (in the language C++; pp. 158–159 in Chapter 2)
  7. Monday, July 15, 2013: up to Chapter 2, p. 200. Think of events that have to come in pairs (encrypt/decrypt), especially if the pairs are nested. Think of things that you might want to have more than one of.

    Run (but do not hand in) the game in pp. 192–200 consisting of the following nine files.

    1. term.h and term.c. See Chapter 1, pp. 85–89. These two files are in C; the others are in C++.
    2. terminal.h and terminal.C. See Chapter 2, pp. 157–163.
    3. rabbit.h and rabbit.C. See Chapter 2, pp. 192–200.
    4. wolf.h and wolf.C. Don’t forget to write the body of the constructor for class wolf.
    5. main.C
  8. Monday, July 22, 2013: up to Chapter 2, p. 244. Hand in one copy of the game incorporating Homeworks 2.10b (p. 200), 2.10c (p. 200), 2.13a (p. 235), and 2.14.1a (p. 238). In order to get Homework 2.13a to compile, you will have to comment out the code you inserted for Homework 2.10c. Only hand in the files you had to modify, and please circle each modification. Also hand in Homework 2.11b (p. 210).

    See how I simplified a flag homework.

  9. Monday, July 29, 2013: up to Chapter 2, p. 266. Course evaluations are at scps.nyu.edu/evaluate.

    If you want to learn to program in C++, go to the second version of the program here and change the function a into an object. The static local variable arr will be a data member; the global variables width, height, xmax, ymax, xmin, and ymin will be static data members. Think of p. 177 in Chapter 2: “a set of variables used by a series of function calls”.

    Do Homework 2.16a (p. 263), but don’t hand it in. You can skip paragraphs (1) and (9) of that homework.

    Hand in one copy of the rabbit game incorporating Homeworks 2.14.2a (p. 244), 2.16b (p. 264), 2.17a (p. 269), and 2.17b (p. 269). Please hand in only the files you had to modify; circle the code you changed.

    Hand in Homework 2.16c (p. 264).

  10. Monday, August 5, 2013:

Fall 2012 INFO1-CE9264 (Section 1, Monday)

  1. Monday, September 24, 2012: up to ordinal2.C in the in-class examples. Admire our class photo; next week, we’ll make it touch sensitive. Read the syllabus and grading policy. Look at the textbook. See how much homework there was last semester. You can get the wireless password each week from room 233.

    You can use any version of C and C++ on any platform for this course. If you’re a Microsoft Windows person and you don’t already have C and C++ compilers, the easiest way to get started is with Bloodshed.

    If you want to use C and C++ with a free Unix account on the Solaris server i5.nyu.edu, get your loginname here. Your loginname is the same as your NYU NetID—two or three lowercase letters followed by several digits. Please email me at mark.meretzky@nyu.edu if you are in the class but your name is not on the list. Get your secret password here. Your secret password is the same as the secret password for your NYU NetID. Compile your program with

    /bin/g++ myprog.C
    ls -l a.out
    -rwx------   1 abc1234     users       8384 Sep 24 15:22 a.out
    ./a.out
    

    Brief notes on various platforms:

    1. Microsoft Windows
      1. Bloodshed: a free GNU C++ compiler. This is the easiest way to get started in C++ on Windows.
      2. Eclipse: a more complicated way to get started in C++ on Windows. You’ll have to do more than just install Eclipse: you also have to plug the C++ plug-in into it.
      3. Microsoft Visual C++: free compiler for PC
      4. Borland: free compiler for PC
    2. Mac OS X
      1. Xcode: download this free for Mac OS X if you don’t already have it. (Macintosh Terminal g++ people: make the same changes to term.c.)
      2. Eclipse also runs on Mac.
      3. Code::Blocks
    3. Unix and Linux
      1. Unix: on your free Solaris Unix account on the host i5.nyu.edu, or on a Macintosh OS X Terminal application.
      2. Linux: install the packages for gcc and gcc-c++ if you don’t already have them.

    Look at the table of operator precedence and associativity on p. 6 of Chapter 1; multiplication and division are on line 13. Try all the programs we looked at in class. Can you find the header file iostream on your machine? What are the values of INT_MIN and INT_MAX on your machine? What happens if you add 1 to a variable that already holds INT_MAX? What happens if you subtract 1 from a variable that already holds INT_MIN? What happens if you leave a variable uninitialized or divide by zero? What are the quotient and remainder when you divide a negative number by a positive number? How long does a for loop take to loop through all 4,294,967,296 integer values? (Don’t write the commas: they’re only for human beings.) On the NYU Solaris machine i5.nyu.edu, it took 44 seconds. Don’t hand in any of the above—just do them.

    Hand in a program that inputs and outputs one or more integers, and does something interesting in between. For example, when does the turkey come out of the oven? What is Bill Gates’s net worth today? Hand in the graph paper homework in the in-class examples immediately after graph3.C where it says “Homework”. Try to get the flag to work, but don’t hand it in.

    For each program that you hand in, please hand in the C++ program itself and the output.

  2. Monday, October 1, 2012: up to automatic.C in the in-class examples. Hand in the date.C and plot.C homeworks in the in-class examples. (The plot.C homework is in the section on “Structures and Arrays Thereof”.)

    Of course, you’ll have to write more than two programs per week if you want to learn C, let alone C++. I’d be happy to read anything you want to show me. I love reading programs. How big are your long ints? Do you have the data type long double? Can you get wchar_t to work on your machine? Microsoft Windows people: does this work? Don’t let the wrong words slip while kissing persuasive lips.

  3. Monday, October 8, 2012: we did functions, dividing a program into two or more .C files, pointers (Chapter 1, pp. 44–55), zero (p. 67), and pass-by-value vs. pass-by-reference (p. 69). here’s one answer to the graph homework.

    Hand in a C++ program (together with its output) that is divided into two or more .C files. At least one of the .C files should use variables and/or functions that were created in another .C file. These variables and/or functions should be declared in a .h file that is included by the .C files.

  4. Monday, October 15, 2012: up to (Chapter 1, p. 97. Here are the Homework excerpts we read in class. Run the program in Chapter 1, pp. 85–89, but don’t hand it in. It consists of three files:
    1. term.h (p. 86; acceptable to C and C++) and term.c (in the language C)
    2. main.C (p. 87; in the language C++)

    Unix people: if tyhe executable program says “Sorry, I don”t know anything about your "xterm-256color" terminal” put the word vt100 into your TERM environment variable.

    echo $TERM
    export TERM=vt100
    echo $TERM
    
    Write an interesting C++ program. Gallia est omnis divisa in partes tres.
  5. Monday, October 22, 2012: up to Chapter 2, p. 124. Here are the Homework excerpts we read in class.

    Run version1.C, version2.C, and version3.C, but don’t hand them in. Do Homework 2.1.3a (p. 117), but don’t hand it in. Hand in a program that creates an interesting class, creates a few objects of the class, and calls their member functions.

  6. Monday, November 5, 2012: up to Chapter 2, p. 165. Run (but do not hand in) the program in pp. 157–163 consisting of the following five files:
    1. term.h (p. 86 in Chapter 1; acceptable to C and C++) and term.c (in the language C)
    2. terminal.h and terminal.C (pp. 159–161 in Chapter 2)
    3. main.C (pp. 158–159 in Chapter 2)

    Hand in Homeworks 2.4a (p. 126), 2.4b (p. 129), and 2.5a.

    Here is code we wrote in class on November 5th before 6:00 p.m. It is not a homework.

    #include <iostream>
    #include <cstdlib>
    #include <string>
    using namespace std;
    
    struct mystruct {
            string s;
            double d;
    };
    
    mystruct a[] = {
            {"moe", 10},
            {"larry", 20},
            {"curly", 30}
    };
    
    const size_t n = sizeof a / sizeof a[0];
    
    int main()
    {
            for (const mystruct *p = a; p < a + n; ++p) {
                    cout << p->s << "\n";
            }
    
            cout << "\nPlease type one of the above names:\n";
            string choice;
            cin >> choice;
    
            for (const mystruct *p = a; p < a + n; ++p) {
                    if (p->s == choice) {
                            cout << "found " << p->d << "\n";
    			return EXIT_SUCCESS;
                    }
            }
    
            cerr << choice << " not found\n";
            return EXIT_FAILURE;
    }
    
  7. Monday, November 12, 2012: up to Chapter 2, p. 195. I corrected my typos in the Homework excerpts. Nothing to hand in this week, but I’d be interested in reading anything you want to hand in. Digitize Timothy Leary and Brian W. Kernighan.

    Run (but do not hand in) the game in pp. 192–200 consisting of the following nine files.

    1. term.h and term.c. See Chapter 1, pp. 85–89. These two files are in C; the others are in C++.
    2. terminal.h and terminal.C. See Chapter 2, pp. 157–163.
    3. rabbit.h and rabbit.C. See Chapter 2, pp. 192–200.
    4. wolf.h and wolf.C. Don’t forget to write the body of the constructor for class wolf.
    5. main.C
  8. Monday, November 19, 2012: up to Chapter 2, p. 248. Homework excerpts. Do Homeworks 2.10b (destructors, p. 200), 2.10c (copy constructors, p. 200), 2.13a (array of objects, p. 235), 2.14.1a (static data members, p. 238), 2.14.2a (static member functions, p. 244), but don’t hand them in.
  9. Monday, November 26, 2012: up to the end of Chapter 2. Hand in one copy of the game including all the changes you had to make for Homeworks 2.16a paragraph 6 (p. 264), 2.16b (p. 264), 2.17a (p. 269), 2.17b (p. 269), plus all the changes you had to make for the Homeworks assigned on November 19 (e.g., the array of rabbits). Do not hand in term.h or term.c. You get no credit if you hand in more than one copy of the game. You fail the course if you hand in code that did not compile and run, unless you write on it “didn’t compile” and/or “didn’t run.”
  10. Monday, December 3, 2012:

Summer 2012 INFO1-CE9264 (Section 1, Monday)

  1. Monday, May 21, 2012: up to factor.C in the in-class examples. Admire our class photo; on June 4th, we’ll make it touch-sensitive.

    If you want to use C and C++ on a free Unix account on the Solaris server i5.nyu.edu, get your loginname here. Your loginname is the same as your NYU NetID—two or three lowercase letters followed by several digits. Get your secret password here. Your secret password is the same as the secret password for your NYU NetID. Compile your program with

    /opt/gcc453/bin/g++ myprog.C
    ls -l a.out
    ./a.out
    

    Read the syllabus and grading policy. Look at the textbook. See how much homework there was last semester.

    You can use any version of C and C++ on any platform for this course. If you’re a Microsoft Windows person and you don’t already have C and C++ compilers, the easiest way to get started is with Bloodshed.

    1. Microsoft Windows
      1. Bloodshed: a free GNU C++ compiler. This is the easiest way to get started in C++ on Windows.
      2. Eclipse: a more complicated way to get started in C++ on Windows. You’ll have to do more than just install Eclipse: you also have to plug the C++ plug-in into it.
      3. Microsoft Visual C++: free compiler for PC
      4. Borland: free compiler for PC
    2. Mac OS X
      1. Xcode: download this free for Mac OS X if you don’t already have it. (Macintosh Terminal g++ people: make the same changes to term.c.)
      2. Eclipse also runs on Mac.
      3. Code::Blocks
    3. Unix and Linux
      1. Unix: on your free Solaris Unix account on the host i5.nyu.edu, or on a Macintosh OS X Terminal application.
      2. Linux: install the packages for gcc and gcc-c++ if you don’t already have them.

    Try all the programs we looked at in class. Come prepared to answer questions such as the following. What are the values of INT_MIN and INT_MAX on your machine? What happens if you add 1 to a variable that already holds INT_MAX? What happens if you subtract 1 from a variable that already holds INT_MIN? What happens if you leave a variable uninitialized or divide by zero? What are the quotient and remainder when you divide a negative number by a positive number? How long does it take to loop through all four billion integer values? (On the NYU Solaris machine i5.nyu.edu, it took 44 seconds.) Look at the table of operator precedence and associativity on p. 6 of Chapter 1; multiplication and division are on line 13.

    Hand in a program that inputs and outputs one or more integers. For example, when does the turkey come out of the oven? What is Bill Gates’s net worth today? Hand in the graph paper homework immediately after graph3.C where it says “Homework” in the in-class examples. Try to get the flag to work, but don’t hand it in.

    For each program that you hand in, please hand in the C++ program itself and the output.

    No class May 28 (Memorial Day).
  2. Monday, June 4, 2012: up to array2.C in the in-class examples. Click on the tip of each nose in the class photo. Homework excerpts.

    Make an interesting flag with some if statements. Hand in the program and the picture, but not the 60,000 lines of numeric output.

  3. Monday, June 11, 2012: up to static in the in-class examples. Hand in the date.C homework, plus one more program that does something interesting with arrays. Run the programs that are divided into two or more source files, but don’t hand them in.

    The Microsoft Windows input example now works correctly.

  4. Monday, June 18, 2012: up to Chapter 1, p. 86. Study pointers in pp. 44–55, pass-by-value vs. pass-by-reference in pp. 69–71, references in pp. 71–81, and C functions in pp. 81–84. Hand in the plot homework, and hand in an interesting program that you invented yourself using an array of structures. Homework excerpts.

    Here’s an example of arrays and pointers.

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
    	//An array containing two pointers.
    	//Each pointer points to the start of an array of chars.
    	const char *a[] = {
    		"hello",
    		"goodbye"
    	};
    
    	//Prints &a[0], i.e., the address of the first element of a.
    	//The first element of a is a pointer to char.
    	cout << a << "\n";
    
    	//Prints the characters hello, because a[0] is a pointer to char.
    	cout << a[0] << "\n";
    
    	//Prints the character h, because a[0][0] is a char.
    	cout << a[0][0] << "\n";
    
    	//An array containing two smaller arrays.
    	//Each smaller array contains 8 chars.
    	const char b[][8] = {
    		"hello",
    		"goodbye"
    	};
    
    	//Prints &b[0], i.e., the address of the first element of b.
    	//The first element of b is an array of 8 chars.
    	cout << b << "\n";
    
    	//Prints the characters hello, because b[0] is the same as &b[0],
    	//which is a pointer to a char.
    	cout << b[0] << "\n";
    
    	//Prints the character h, because b[0][0] is a char.
    	cout << b[0][0] << "\n";
    
    	return EXIT_SUCCESS;
    }
    
    0xffbff6a8
    hello
    h
    0xffbff698
    hello
    h
    
  5. Monday, June 25, 2012: up to Chapter 2, p. 117. Homework excerpts. Run the program in Chapter 1, pp. 85–89, but don’t hand it in. It consists of three files:
    1. term.h (p. 86; acceptable to C and C++) and term.c (in the language C)
    2. main.C (p. 87; in the language C++)

    Study version1.C, version2.C, and version3.C in parallel (Chapter 2, pp. 105–117). Write a program that creates a class containing private data members and public member functions. Create one or more objects of this class and call their member functions. Don’t just write a class with getters and setters. How about a weather_report class containing temperature and wind speed data members, and a wind_chill member function that returns the wind chill? Or something to do with your height and ideal weight? On Mars or Jupiter? Or your interest rate?

  6. Monday, July 2, 2012: up to Chapter 2, p. 137. Homework excerpts. Hand in Homeworks 2.4a (p. 126) and 2.4b (p. 129).
  7. Monday, July 9, 2012: up to Chapter 2, p. 163. Hand in Homeworks 2.6a (p. 152) and 2.6b (p. 152), including the typedef value_type on pp. 153–154.

    Run (but do not hand in) the program that tests class terminal. It consists of these five files:

    1. term.h (p. 86 in Chapter 1; acceptable to C and C++) and term.c (in the language C)
    2. terminal.h and terminal.C (pp. 159–161 in Chapter 2)
    3. main.C (pp. 158–159 in Chapter 2)
  8. Monday, July 16, 2012: up to Chapter 2, p. 189. I fixed the bugs I accidentally introduced into the Homework excerpts. Think about the four ways of viewing an object:
    1. a structure with better security
    2. a thing that triggers a pair of events
    3. something you might want to make more than one of
    4. a group of variables that are used by a series of function calls.
    Write and hand in an interesting but not-too-complicated class. Construct a few objects of the class and call their member functions.
  9. Monday, July 23, 2012: up to Chapter 2, p. 209. I corrrected the typos in this week’s Homework excerpts. Run the game in pp. 192–200 consisting of the nine files
    1. term.h and term.c. See Chapter 1, pp. 85–89. These two files are in C; the others are in C++.
    2. terminal.h and terminal.C. See Chapter 2, pp. 157–163.
    3. rabbit.h and rabbit.C. See Chapter 2, pp. 192–200.
    4. wolf.h and wolf.C. Don’t forget to write the constructor for class wolf.
    5. main.C
    Then hand in one copy of the game incorporating Homeworks 2.10b (destructors, p. 200) and 2.10c (no copy constructors, p. 200). Hand in only the files that you had to modify for homeworks 2.10b and 2.10c. For example, don’t hand in term.h, term.c, terminal.h, terminal.C. and main.C.

    Read about friend functions (pp. 200–210). Hand in Homework 2.11b (friends, p. 210).

  10. Monday, July 30, 2012: Homework excerpts. If you have not yet emailed your course evaluation to NYU, please go here and login with your NetID/Password.

Summer 2010 X52.9265 (Section 1, Wednesday)

  1. Wednesday, September 19, 2012: up to p. 299 in Chapter 3. We also looked at class myrandom in Chapter 2, pp. 174–177. Admire our class photo; on October 3rd, we’ll make it touch-sensitive.

    If you want to use C and C++ on a free Unix account on the Solaris server i5.nyu.edu, get your loginname here. [As of Fri Sep 21 11:17:38 EDT 2012 the i5.nyu.edu accounts have been created.] (Please email me at mark.meretzky@nyu.edu if you are in the class but your name is not on the list.) Your loginname is the same as your NYU NetID—two or three lowercase letters followed by several digits. Get your secret password here. Your secret password is the same as the secret password for your NYU NetID. Compile your program with

    /opt/gcc453/bin/g++ myprog.C
    ls -l a.out
    ./a.out
    

    Read the syllabus and grading policy. Look at the textbook. See how much homework there was last semester.

    You can use any version of C and C++ on any platform for this course. If you’re a Microsoft Windows person and you don’t already have C and C++ compilers, the easiest way to get started is with Bloodshed.

    1. Microsoft Windows
      1. Bloodshed: a free GNU C++ compiler. This is the easiest way to get started in C++ on Windows.
      2. Eclipse: a more complicated way to get started in C++ on Windows. You’ll have to do more than just install Eclipse: you also have to plug the C++ plug-in into it.
      3. Microsoft Visual C++: free compiler for PC
      4. Borland: free compiler for PC
    2. Mac OS X
      1. Xcode: download this free for Mac OS X if you don’t already have it. (Macintosh Terminal g++ people: make the same changes to term.c.)
      2. Eclipse also runs on Mac.
      3. Code::Blocks
    3. Unix and Linux
      1. Unix: on your free Solaris Unix account on the host i5.nyu.edu, or on a Macintosh OS X Terminal application.
      2. Linux: install the packages for gcc and gcc-c++ if you don’t already have them.

    Make sure you can run the C++ programs we looked at in class, e.g., the program consisiting of the three files

    1. date.h on pp. 273–275
    2. date.C on pp. 275–278
    3. equals.C on p. 278
    You don’t have to hand this in.

    Make a class with overloaded operators. Write a program that demonstrates them. Hand in the program and the output on paper on October 3. The following are some sketchy suggestions.

    	color yellow = red + green;
    	vector northeast = north + east;
    	element helium = hydrogen + 1;
    
    	//There are 4 stops on the #6 subway
    	//between Union Square and Grand Central Terminal.
    	int n = grandCentral - unionSquare;
    
    	station bleekerStreet = unionSquare - 1;  //one stop below Union Square
    	if (myCurrentStation < bleekerStreet) {
    		cout << "I am way downtown.\n";
    	}
    

    Run (but do not hand in) the program in Chapter 1, pp. 85–89, but don’t hand it in. It consists of the following three files. Email me at mark.meretzky@nyu.edu if you get intro trouble; see the above links to the various platforms.

    1. term.h (p. 86; acceptable to C and C++) and term.c (in the language C)
    2. main.C (p. 87; in the language C++)

    No class September 26.

  2. Wednesday, October 3, 2012: up to p. 340 in Chapter 3. Here is color. I fixed the bug in pointer.

    Run (but do not hand in) the program that tests class terminal. The program consists of these five files. You have already downloaded the first two.

    1. term.h (p. 86 in Chapter 1) and term.c (in the language C)
    2. terminal.h and terminal.C (pp. 159–161 in Chapter 2)
    3. main.C (pp. 158–159 in Chapter 2)
  3. Wednesday, October 10, 2012: up to Chapter 4, p. 391. Here are the Homework excerpts. Write a class with some i/o manipulators, like the polar and cartesian in pp. 362–366, or the scale in pp. 371–375.
  4. Wednesday, October 17, 2012: up to Chapter 4, p. 436. Poke around in memory and discover the overhead that’s prefixed on your platform to each block of memory that’s dynamically allocated by malloc in C or new in C++. The overhead will probably consist of a couple of numbers (probably of data type size_t) and/or a couple of pointers (probably void *). How is the overhead different when you allocate block that holds an array of objects that have destructors?

    Also write a C++ program that needs an array that grows because the program appends elements to the end of the array as it runs. Assume that there is no way to know in advance how many additional elements will be appended. Now there is no such thing in C or C++ as an array that grows: the number of elements in an array is fixed. But there are two ways to get the same effect:

    1. dynamically allocate a block of memory that grows with malloc and realloc; or
    2. make a vector that grows.

    Program it both ways. Which one is simpler?

  5. Wednesday, October 24, 2012: up to Chapter 4, p. 470. Dynamic memory allocation examples with malloc/realloc vs. vector.

    Run the game consisting of the following 9 files, but don’t hand it in.

    1. term.h and term.c See Chapter 1, pp. 85–89. These two files are in C; the others are in C++.
    2. terminal.h and terminal.C. See Chapter 2, pp. 157–163.
    3. rabbit.h and rabbit.C. See Chapter 2, pp. 192–200.
    4. wolf.h and wolf.C
    5. main.C
  6. The do Homework 4.4a (p. 466). Hand in one copy of only the files you had to modify for this homework. For example, do not hand in term.h, because you did not have to modify it. Circle or highlight the code you modified.

  7. Wednesday, October 31, 2012: no class due to Hurricane Sandy.
  8. Wednesday, November 7, 2012: up to Chapter 5, p. 499. Nothing to hand in, but you should study and play with all this stuff. Homework excerpts.
  9. Wednesday, November 14, 2012: up to Chapter 5, p. 544. Can you understand pp. 498—501? Hand in Homework 5.8a (p. 535). The HTML5 canvas example. No class on November 21. The last class will be on December 12.
  10. Wednesday, November 28, 2012: up to the end of Chapter 5. Hand in one copy of the game incorporating Homeworks 5.9.5a (multiple inheritance, p. 563), 5.9.5b (wabbit destructor, p. 571), and 5.12a (private inheritance, p. 582).

    Want to see a problem in object-oriented design? We have to write code in one class (class visionary) that appears to need access to the private data members of another class (the x and y data members of class wabbit). How can we handle this? If you’re interested in the solution, do Homeworks 5.11a (a friend of class wabbit, p. 576), 5.11b, 5.11c, and 5.11d.

    The object-oriented design course INFO1-CE9267: OOAD_Syllabus.pdf, OOAD.pdf.

  11. Wednesday, December 5, 2012: up to the end of Chapter 6. It’s okay with me if you want to start templates nxt week. Homework excerpts. Hand in onec opy of the game incorporating Homeworks 6.9.3b (p. 628) and 6.9.3c (p. 629).
  12. Wednesday, December 12, 2012:

Summer 2010 X52.9265 (Section 1, Wednesday)

  1. Wednesday, May 26, 2010: up to p. 302 in Chapter 3 of the textbook. Please email me at mark.meretzky@nyu.edu if you are enrolled in the class but your name is not on this list. Read the syllabus and grading policy. Look at the textbook. See how much homework there was last semester. Admire last semester’s class photo; next week, we’ll have our own.

    You can use any version of C and C++ on any platform for this course. On Microsoft Windows, the easiest way to get started is with Bloodshed (see below). On Mac OS X, use Xcode. On the Unix host at NYU, use the GNU compilers /opt/gcc450/bin/gcc and /opt/gcc450/bin/g++ (withe the -std=c++0x option if you want to experiment with C++0x; see lambda functions). Put /opt/gcc450/lib into the environment variable LD_LIBRARY_PATH.

    export LD_LIBRARY_PATH=/opt/gcc450/lib
    On Linux, install the packages for gcc and gcc-c++ if you don’t already have them.

    1. Unix: on your free Unix account on the host i5.nyu.edu, or on a Macintosh OS X Terminal application.
    2. Bloodshed: a free GNU compiler for PC.
    3. Xcode: on Mac OS X.
    4. Code::Blocks: on Mac OS X.
    5. Microsoft Visual C++: free compiler for PC
    6. Borland: free compiler for PC

    Run the game consisting of the following nine files, but don’t hand it in.

    1. term.h and term.c See Chapter 1, pp. 85–89. These two files are in C; the others are in C++.
    2. terminal.h and terminal.C. See Chapter 2, pp. 157–163.
    3. rabbit.h and rabbit.C. See Chapter 2, pp. 192–200.
    4. wolf.h and wolf.C
    5. main.C

    Then update class terminal by repeating the Homeworks you did in C++ Part I: 2.14.2a (static member functions for class terminal, Chapter 2, p. 244) and 2.17b (constant data members for class terminal, Chapter 2, p. 269), but don’t hand them in.

    Remember the 300 × 200 pixel flag we did last semster? Invent a structure to hold ech pixel:

    	struct pixel {
    		unsigned char red;
    		unsigned char green;
    		unsigned char blue;
    	};
    
    Make a class named picture. Each picture should contain the following non-static, private data member:
    	pixel a[200][300];	//200 rows, 300 columns
    
    The constructor for class picture should take one argument, a “pointer to pixel” that points to the first element of a 300 × 200 array of pixels. Class picture should also have two static data members named black and white. These static data members should be all-black and all-white pictures. (See Chapter 2, p. 239, Homework 2.14.1b, ¶ (4), where class point acquired a static data member of type point.)

    Now for the operator overloading. I’d like to be able to compare pictures for equality and inequality:

    	picture p1(argument for constructor);
    	picture p2(argument for constructor);
    
    	if (p1 == p2) {
    
    I’d like to be able to halve the color intensity of a picture. The following example would halve the red, green, and blue value of every pixel in p.
    	picture p(argument for constructor);
    	p /= 2;
    
    I’d also like to say
    	picture p1(argument for constructor);
    	picture p2 = p1 / 3;
    
    (You will have a justifiable feeling of pride when your operator/ does its work by calling your operator/-.) And superimpositions:
    	picture p1(argument for constructor);
    	picture p2(argument for constructor);
    	picture p3 = (p1 + p2) / 2;
    
    Warning: an unsigned char can hold only values in the range 0 to 255 inclusive (depending on your machine). When you add two unsigned chars together, the sum will often be too big to fit in an unsigned char. We saw the solution in the “timebomb” example in Chapter 3, pp. 295–299. The sum of two picture objects should be a superimposition object, which will be just like a picture object except that the red, green, blue fields of its structures should be unsigned int instead of unsigned char. And when you divide a superimposition object by 2, the quotent should be a picture object.
    	picture p1(argument for constructor);
    	picture p2 = (p1 + picture::white) / 2;
    

    I’d like to break a picture down into three pictures containing its red, green, blue components. Invent operators that will do this. I’d like to increase or decrease the contrast of a picture. Invent an operator that will do this. An operator to change a picture to back and white? An operator to subtract one picture from another? A weighted superimposition?

    	picture p1(argument for constructor);
    	picture p2(argument for constructor);
    	picture result = (5 * p1 + 2 * p2) / 7;
    
    You don’t have to do all of these. Just invent some operators that will do interesting things to picture objects. Keep them all 300 × 200.

  2. Wednesday, June 2, 2010: up to Chapter 4, p. 362. Hand in Homework 3.12a in Chapter 3, p. 343.
  3. Wednesday, June 9, 2010: up to Chapter 4, p. 405. Write some i/o manipulators and demonstrate that they work, either revolutionary and nonrevolutionary, or manipulators with argument(s), like scale. Discover the hidden numbers before a dynamically allocated block on your platform.
  4. Wednesday, June 16, 2010: up to Chapter 4, p. 449. I corrected the output of vector_obj.C on p. 439; study it. The fork examples are here. Nothing to hand in.
  5. Wednesday, June 23, 2010: up to Chapter 5, p. 487. Hand in one copy of the game, incorporating Homeworks 4.4a (Chapter 4, p. 466), 4.4b (p. 471), and 4.4c (p. 471). You get no credit if you hand in a separate copy of the game for each homework. Hand in only the files you needed to create or modify. A .h file comes before the corresponding .C file.

    Instead of making classes wolf and rabbit friends of class game, should we have given class game member functions such as the following:

    public:
    	void put(x, y, c) const {term.put(x, y, c);}	//just a call-through
    
    Then p. 466, line 7 could be simplified to
    	g->put(x, y, c);
    

    See my home page for my Fall, 2010 teaching schedule. Any suggestions how I could squeeze in a 30-hour C++ Part III INFO1-CE9266?

  6. Wednesday, June 30, 2010: up to Chapter 5, p. 526. Nothing to hand in. Could you figure out why the following program does not compile on i5.nyu.edu?
    #include <cmath>
    int main()
    {
            std::sqrt(2.0);
    }
    
    /opt/gcc450/bin/g++ junk.C
    init2.c:37:  assertion failed: ((64 - 0)+0) == (((64 - 0)+0)/8) * 8 && sizeof(mp_limb_t) == (((64 - 0)+0)/8)
    junk.C: In function 'int main()':
    junk.C:4:22: internal compiler error: Abort
    Please submit a full bug report,
    with preprocessed source if appropriate.
    See <http://gcc.gnu.org/bugs.html> for instructions.
    
  7. Wednesday, July 7, 2010: up to Chapter 5, p. 564. Hand in Homework 5.8a (p. 535).
  8. Wednesday, July 14, 2010: up to Chapter 6, p. 606.

    I fixed the bug in exception1.C on pp. 590–591. The problem was that the value of the expression -2147483648 % -1 is 0 with g++ version 4.2.1, and -1 with our current g++ version 4.5.0. See the loophole in §5.6, ¶ 4, of the C++ Standard (p. 113): “if the quotient … is representable” (italics mine).

    Hand in one copy of the game incorporating Homeworks 5.9.5a (multiple inheritance, p. 563) and 5.9.5b (wabbit destructor doesn’t beep, p. 571).
  9. Wednesday, July 21, 2010: up to Chapter 6, p. 630. Hand in one copy of the game incorporating Homeworks 5.11a (p. 576), 5.11b, 5.11c, 5.11d (class visionary)., 6.9.3b (throw exceptions, p. 628), and 6.9.3c (catch exception, p. 629). On July 28, we’ll start doing templates.
  10. Wednesday, July 28, 2010:

Summer 2011 X52.9266 (Section 1, Wednesday)

Please bring a digital camera to the first class to take the class photo.

  1. Wednesday, June 1, 2011: up to Chapter 7 of the textbook, p. 656. To compile the min examples on pp. 634–640, you will need date.h and date.C files; get them from http://i5.nyu.edu/~mm64/book/src/mydate/. See if you can figure out how stand and step work on pp. 655–658. Read ahead in Chapter 7. My email address is mark.meretzky@nyu.edu

    Read the syllabus and grading policy. See how much homework there was last semester. Admire our class photo; next week, we’ll make it touch-sensitive.

    If you want to use your Solaris Unix account on i5.nyu.edu, get your login name and secret password. You can connect to i5.nyu.edu from a PC via PuTTY, from a Mac via ssh, from an iPhone via TouchTerm, or from Android via ConnectBot.

    Our GNU gcc and g++ compilers in the directory /opt/gcc450/bin on i5.nyu.edu fail an assertion for the following simple program. If the assertion fails when you’re trying to compile one of your own programs, use the gcc and g++ in the directory /opt/gcc/bin instead.

    extern "C" double sqrt(double x);
    
    int main()
    {
            double d = sqrt(2.0);
    }
    

    Run (but do not hand in) the program in Chapter 1, §1.7.3, pp. 85–89, consisting of the following three files. You may have to change the filename suffixes when you download them (Save As…). Any machine, compiler, and operating system is fine with me. Uncomment one line in term.c; see p. 89.

    1. term.h acceptable to C and C++
    2. term.c in the language C
    3. main.C in the language C++

    You’ll have to follow special instructions for this program because

    1. it consists of more than one source file
    2. half of the program is in C and half in C++
    3. Unix people have to link in the curses library.

    To find these special instructions, search for 1.7.3 in the following instructions for Unix, Bloodshed, Xcode, and Code::Blocks.

    1. Unix: on your free Unix account on the host i5.nyu.edu, or on a Macintosh OS X Terminal application.
    2. Bloodshed: a free GNU compiler for PC. Install “Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2”.
    3. Xcode: on Mac OS X.
    4. Code::Blocks: on Mac OS X.
    5. Microsoft Visual C++: free compiler for PC
    6. Borland: free compiler for PC
  2. Wednesday, June 8, 2011: up to Chapter 7 of the textbook, p. 685.

    Run (but do not hand in) the program in Chapter 2, pp. 157–163, retrofitted with the exceptions in in Chapter 6, pp. 628–629. It consists of the following six files. You downloaded the first two last week. On my platform, C files end with lowercase .c; C++ files end with uppercase .C. On your platform, you may have to rename them.

    1. term.h and term.c
    2. except.h, thrown by the member functions of class terminal.
    3. terminal.h and terminal.C
    4. main.C
  3. Wednesday, June 15, 2011: up to Chapter 7, p. 696.

    Run (but do not hand in) the game consisting of the following 22 files (plus whatever additional grandchild classes you want, plus class visionary if you wrote it in C++ Part II).

    1. term.h and term.c
    2. except.h, thrown by the member functions of class terminal.
    3. terminal.h and terminal.C
    4. game.h and game.C
    5. wabbit.h and wabbit.C
    6. manual.h and manual.C
    7. The other motion classes: immobile.h, brownian.h
    8. The rank classes: inert.h, victim.h, predator.h, halogen.h
    9. The grandchild classes: wolf.h, rabbit.h, sitting_duck.h, boulder.h
    10. main.C

    Then hand in one copy of the game incorporating Homeworks 7.2.2b (p. 693) and 7.2.2c (p. 695). Hand in only the files you had to create or modify.

  4. Wednesday, June 22, 2011: up to Chapter 7, p. 716. Watch bubble sort (Chapter 7, p. 711) perfomed by Hungarian folk dancers. Then hand in Homework 7.2.4.1c (p. 711). Does your platform give you a way to see the assembly language into which your C++ program is translated (p. 714)?
  5. Wednesday, June 29, 2011: up to Chapter 7, p. 738. Hand in Homework 7.2.6d (p. 727).
  6. Wednesday, July 6, 2011: up to Chapter 7, p. 770. Hand in Homework 7.2.8a (p. 740). The C loop we saw in class was
    	int c;
    
    	while ((c = getchar()) != EOF) {
    		//do something with c;
    	}
    
  7. Wednesday, July 13, 2011: up to Chapter 8, p. 789. Write a C program that copies its sandard input to its standard output, byte by byte. Run it from your operating system command line (e.g., the Windows command prompt cmd.exe, or the Macintosh Terminal window) as follows.
    progname < infile > outfile
    
    Then see how to do the same thing in C++ in Chapter 3, p. 329, get.C.
  8. Wednesday, July 20, 2011: up to Chapter 8, p. 817. Hand in Homework 8.2.1a (p. 815).
  9. Wednesday, July 27, 2011: up to Chapter 8, p. 854. Hand in Homework 8.3c (p. 841).
  10. Wednesday, August 3, 2011:

Spring 2011 X52.9266 (Section 1, Tuesday)

  1. March 1, 2011: up to Chapter 7 of the textbook, p. 658, except that we haven’t looked at the definitions of stand and step yet. To compile the min examples on pp. 634–640, you will need date.h and date.C files; get them from http://i5.nyu.edu/~mm64/book/src/mydate/. See if you can figure out how stand and step work on pp. 655–658. Read ahead in Chapter 7.

    Read the syllabus and grading policy. See how much homework there was last semester. Admire our class photo; next week, we’ll make it touch-sensitive.

    If you want to use your Solaris Unix account on i5.nyu.edu, get your login name and secret password. You can connect to i5.nyu.edu from a PC via PuTTY, from a Mac via ssh, from an iPhone via TouchTerm, or from Android via ConnectBot.

    Our GNU gcc and g++ compilers in the directory /opt/gcc450/bin on i5.nyu.edu fail an assertion for the following simple program. If the assertion fails when you’re trying to compile one of your own programs, use the gcc and g++ in /opt/gcc/bin instead.

    extern "C" double sqrt(double x);
    
    int main()
    {
            double d = sqrt(2.0);
    }
    

    Run (but do not hand in) the program in Chapter 1, §1.7.3, pp. 85–89, consisting of the following three files. You may have to change the filename suffixes when you download them (Save As…). Any machine, compiler, and operating system is fine with me. Uncomment one line in term.c; see p. 89.

    1. term.h acceptable to both languages
    2. term.c in the language C
    3. main.C in the language C++

    You’ll have to follow special instructions for this program because

    1. it consists of more than one source file
    2. half of the program is in C and half in C++
    3. Unix people have to link in the curses library.

    To find these special instructions, search for 1.7.3 in the following instructions for Unix, Bloodshed, Xcode, and Code::Blocks.

    1. Unix: on your free Unix account on the host i5.nyu.edu, or on a Macintosh OS X Terminal application.
    2. Bloodshed: a free GNU compiler for PC. Install “Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2”.
    3. Xcode: on Mac OS X.
    4. Code::Blocks: on Mac OS X.
    5. Microsoft Visual C++: free compiler for PC
    6. Borland: free compiler for PC
  2. March 8, 2011: up to Chapter 7, p. 678. Click on the tip of each nose in the class photo. Does anyone know how to host open source code on Github? No class March 15 (Spring Break).

    Here’s something that won’t be possible until the end of Chapter 7, but it would be educational to try it now and see why it can’t be done. The print template functions in pp. 643–644 printed a different message for a non-pointer, pointer, pointer to pointer, pointer to pointer to pointer, etc. Can we get the name template function on pp. 668–669 to do the same? For example, it should be capable of returning the strings "int", "int *", "int **", "int ***", etc., either as arrays of characters or as C++ string objects, whichever is easier for you. Note that name takes no function arguments (i.e., arguments in the parentheses). Would it help if name did take an argument, preferably by reference?

    Run (but do not hand in) the program in Chapter 2, pp. 157–163, retrofitted with the exceptions in in Chapter 6, pp. 628–629. It consists of the following six files. You downloaded the first two last week. On my platform, C files end with lowercase .c; C++ files end with uppercase .C. On your platform, you may have to rename them.

    1. term.h and term.c
    2. except.h, thrown by the member functions of class terminal.
    3. terminal.h and terminal.C
    4. main.C
  3. March 22, 2011: up to Chapter 7, p. 700.

    Run (but do not hand in) the game consisting of the following 22 files (plus whatever additional grandchild classes you want, plus class visionary if you wrote it in C++ Part II).

    1. term.h and term.c
    2. except.h, thrown by the member functions of class terminal.
    3. terminal.h and terminal.C
    4. game.h and game.C
    5. wabbit.h and wabbit.C
    6. manual.h and manual.C
    7. The other motion classes: immobile.h, brownian.h
    8. The rank classes: inert.h, victim.h, predator.h, halogen.h
    9. The grandchild classes: wolf.h, rabbit.h, sitting_duck.h, boulder.h
    10. main.C

    Then hand in one copy of the game incorporating Homeworks 7.2.2b (p. 693) and 7.2.2c (p. 695). Hand in only the files you had to create or modify.

  4. March 29, 2011: up to Chapter 7, p. 722. Hand in Homework 7.2.4.1c (p. 711).
  5. April 5, 2011: up to Chapter 7, p. 757. Hand in Homeworks 7.2.6d (p. 727) and 7.2.8a (p. 740).
  6. April 12, 2011: up to Chapter 8, p. 789. Nothing to hand in this week.
  7. April 19, 2011: up to Chapter 8, p. 812. No homework to hand in this week.
  8. April 26, 2011: up to Chapter 8, p. 831. Hand in Homeworks 8.2.1a (p. 815, pedestrian but educational) and 8.2.3a (p. 831, gruesome but educational). Study “Eliminate the proxy class” on pp. 831–832.
  9. May 3, 2011: up to Chapter 8, p. 859. Hand in Homework 8.3c (p. 841).
  10. May 10, 2011: In-class examples.

Spring 2010 X52.9266 (Section 1, Wednesday)

  1. February 24, 2010: up to Chapter 7 of the textbook, p. 660. To compile the min examples on pp. 636–640, you will need date.h and date.C files; get them from http://i5.nyu.edu/~mm64/book/src/mydate/. See if you can figure out how stand and step work on pp. 657–660. Read ahead in Chapter 7.

    Read the syllabus and grading policy. See how much homework there was last semester. Admire the class photo; next week, we’ll make it touch-sensitive.

    If you want to use your Solaris Unix account on i5.nyu.edu, get your login name and secret password. You can connect to i5.nyu.edu from a PC via PuTTY, from a Mac via ssh, from an iPhone via TouchTerm, or from Android via ConnectBot.

    Run (but do not hand in) the program in Chapter 1, §1.7.3, pp. 86–90, consisting of the following three files. You may have to change the filename suffixes when you download them (Save As…). Any machine, compiler, and operating system is fine with me. Uncomment one line in term.c; see p. 89.

    1. term.h acceptable to both languages
    2. term.c in the language C
    3. main.C in the language C++

    You’ll have to follow special instructions for this program because

    1. it consists of more than one source file
    2. half of the program is in C and half in C++
    3. Unix people have to link in the curses library.

    To find these special instructions, search for 1.7.3 in the following instructions for Unix, Bloodshed, Xcode, and Code::Blocks.

    1. Unix: on your free Unix account on the host i5.nyu.edu, or on a Macintosh OS X Terminal application.
    2. Bloodshed: a free GNU compiler for PC. Install “Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2”.
    3. Xcode: on Mac OS X.
    4. Code::Blocks: on Mac OS X.
    5. Microsoft Visual C++: free compiler for PC
    6. Borland: free compiler for PC

  2. March 3, 2010: up to Chapter 7, p. 687 (our first template class, which also happens to be our first class template). The explicit specialization of the template function mantissa that we saw in class on March 3 is in precision.C. I improved it to save and restore the precision.

    To really appreciate what a template function does for you, write the step and stand examples in Chapter 7, pp. 657–660 without using templates. You’ll be amazed! Do not hand this in.

    Could you write the following template function alignment? It should return a size_t. Is there a way you could write this template function as a single function template that would return the answer for any data type? Or would you have to write the template function as a general-purpose template followed by an explicit specialization for each built-in data type, like name in Chapter 7, p. 670 and mantissa in precision.C? Don’t hand this in, but you can show your template function to the class on March 10 if you want to.

    	cout << "On this machine, an int must be aligned on a "
    		<< alignment<int>() << "-byte boundary.\n";
    

    Click on Thomas A. Sullivan’s nose in the class photo to see the C++ Draft Standard. Search for “self-immolation”; it’s on p. 357.

    If you have the Microsoft Visual C++ compiler, please email me the version number and the output you get when you compile and run the “point of definition/point of instantiation” program in Chapter 7, pp. 680–681?

    Run (but do not hand in) the program in Chapter 2, pp. 159–165, retrofitted with the exceptions in in Chapter 6, pp. 630–631. It consists of the following six files. You downloaded the first two last week. On my platform, C files end with lowercase .c; C++ files end with uppercase .C. You may have to rename them.

    1. term.h and term.c
    2. except.h, thrown by the member functions of class terminal.
    3. terminal.h and terminal.C
    4. main.C

  3. March 10, 2010: up to Chapter 7, p. 712. Here is the align template function.

    Run the game consisting of the following 22 files (plus whatever additional grandchild classes you want, plus class visionary if you wrote it in C++ Part II).

    1. term.h and term.c
    2. except.h, thrown by the member functions of class terminal.
    3. terminal.h and terminal.C
    4. game.h and game.C
    5. wabbit.h and wabbit.C
    6. manual.h and manual.C
    7. The other motion classes: immobile.h, brownian.h
    8. The rank classes: inert.h, victim.h, predator.h, halogen.h
    9. The grandchild classes: wolf.h, rabbit.h, sitting_duck.h, boulder.h
    10. main.C
    No class on March 17 (Spring Break). On March 24, hand in one copy of the game incorporating Homeworks 7.2.2.b (Chapter 7, p. 695) and 7.2.2.c (p. 697). Do Homeworks 7.2.4a and 7.2.4b (pp. 707–708), but don’t hand them in. It would make your instructor as happy as Barney if you would read about template metaprogramming on pp. 709–721.

  4. March 24, 2010: up to Chapter 7, p. 747. Hand in Homework 7.2.6d (step and stand, p. 729) and Homework 7.2.8a (p. 742). Not to be handed in: given an n-bit mantissa, how many decimal digits can it hold? bubble3.C

  5. March 31, 2010: up to Chapter 7, p. 782. What is the smallest possible whole number that cannot be held in a double on your machine (p. 750)? Write a program that tries to store this number into a double and prints the resulting contents of the double. Does it round up or down? Also print out the round_style data member of class numeric_limits<double>. Also demonstrate that a double can hold the next largest and next smallest whole numbers. Hand in Homework 7.2.9c (p. 751). Do Homework 7.2.9d (p. 755), but don’t hand it in.

  6. April 7, 2010: up to Chapter 8, p. 823. Do Homework 8.1a (p. 795), but don’t hand it in. Do the three rabbit game homeworks 8.1c (p. 796), 8.1d (p. 797), and 8.1e (the most interesting of the three, p. 799), but don’t hand them in. Hand in Homework 8.1.2a (node:const_iterator, p. 817). Does my printable.h (Homework 7.2.9c, p. 751) compile on your platform?

  7. April 14, 2010: up to Chapter 8, p. 848. Hand in Homework 8.3b (p. 842). Do Homework 8.3c (p. 843), but don’t hand it in. Hand in Homework 8.3d (p. 843). Here is stepper.h and step.C from Chapter 7, pp. 657 and 729.

  8. April 21, 2010: up to Chapter 8, p. 873. Hand in Homeworks 8.4.2e on p. 866 (bind1st; it will be an imitation of the bind2nd on p. 865) and 8.4.2g on p. 873 (compose1; it will be a scaled-dwon version of the compose2 on pp. 868–869). Here is the in-class quiz I gave my C++ Part I students on April 19; it’s made entirely of homeworks they handed in on April 12. Please email me a link about C++ lambda functions.

  9. April 28, 2010: up to Chapter 8, p. 899. We also did Chapter 9, pp. 967–987.

    If you use your i5.nyu.edu account, email comment@i5.nyu.edu asking him/her/them politely for g++ version 4.5.0 at http://gcc.gnu.org/gcc-4.5/. You might want to explain why we want it by showing them the “before” and “after” examples of lambda functions we looked at in class on April 28:
    http://i5.nyu.edu/~mm64/book/src/find_if/find_if1.C contains

    	p = find_if(a, a + n,
    		__gnu_cxx::compose2(
    			logical_and<bool>(),
    			bind2nd(greater<int>(), 35),
    			bind2nd(   less<int>(), 45)
    		)
    	);
    
    http://i5.nyu.edu/~mm64/INFO1-CE9266/src/find_if1.C contains
    	p = find_if(a, a + n, [](int x) {return x > 35 && x < 45;});
    
    Invent your own example, so you’re not all mailing him/her/them the same code.

    Point your browser at the ring terminal. Create a copy of main.C with the IP address of the host on which your browser is running (or modify main.C to take the address as a command line argument via argc and argv, or as an environment variable via getenv). Then run the C++ program consisting of makefile, term0.h, term0.c, and your main.C. With the browser window as the active window, type characters ending with lowercase q. It didn’t receive my keystrokes in class on April 28 was because in my excitement I was typing them into the wrong window on the Mac. Don’t be shy about saying

    netstat -a -f inet -P tcp
    

    Run the program in Chapter 9, pp. 972–984 consisting of the two files terminal.h and main.C. Then write a C++ program that reads and writes this new terminal. Or write a class terminal0 to sit on top of the term0_ C functions that read and write the ring terminal, and a C++ program that constructs and uses a terminal0.

    Next week: valarrays and dispatching in Chapter 8, RTTI in Chapter 10.


  10. May 5, 2010: for_each.C

Summer 2009 X52.9266 (Section 1, Wednesday)

  1. May 27, 2009: up to Chapter 7, p. 650. Read the syllabus and grading policy. See how much homework there was last semester. Admire the class photo; next week, we’ll make it touch-sensitive. If you want to use your Solaris Unix account on i5.nyu.edu, get your login name and secret password. You can connect from a PC via PuTTY, from a Mac via ssh, from an iPhone via TouchTerm, or form Android via ConnectBot. Call the computer labs and see if they will let you in.

    With the GNU C compiler gcc, and the C++ compiler g++, you can have parentheses around the left operand of an assignment. This came up in Chapter 7, p. 646, second line 5.

    	int i = 10;
    	(i) = 20;
    

    Run (but do not hand in) the program in Chapter 1, §1.7.3, pp. 86–90, consisting of the following three files. You may have to change the filename suffixes when you download them (Save As…). Any machine, compiler, and operating system is fine with me. Uncomment one line in term.c; see p. 89.

    1. term.h acceptable to both languages
    2. term.c in the language C
    3. main.C in the language C++

    You’ll have to follow special instructions for this program because

    1. it consists of more than one source file
    2. half of the program is in C and half in C++
    3. Unix people have to link in the curses library.

    To find these special instructions, search for 1.7.3 in the following instructions for Unix, Bloodshed, Xcode, and Code::Blocks.

    1. Unix: on your free Unix account on the host i5.nyu.edu, or on a Macintosh OS X Terminal application.
    2. Bloodshed: a free GNU compiler for PC. Install “Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2”.
    3. Xcode: on Mac OS X.
    4. Code::Blocks: on Mac OS X.
    5. Microsoft Visual C++: free compiler for PC
    6. Borland: free compiler for PC

    Make a flag if you have not already done so.

    Study the hardest stuff: “Change T to the simplest data type” in Chapter 7, pp. 637–639. After seeing your expressions of shock and horror, I simplified the peek function; see the new description on pp. 646–647. Extra credit: I showed you the layout of a double on my Sparc Sun machine running Solaris. Can you use peek to discover the layout of a double on a different machine?

    On Wednesday, June 3, at the last possible moment, print a little more of Chapter 7 and bring it to class. (How far do you think we will get?) Please email me if things don’t work: mark.meretzky@nyu.edu


  2. June 3, 2009: up to Chapter 7, p. 673. Pointer to data member and pointer to member function are in Chapter 2, pp. 253–256. Data types with a last name are in Chapter 4, pp. 417–422. Click on the tip of each nose in the class photo.

    Run the program in Chapter 2, pp. 157–163, consisting of the following six files. You downloaded the first two last week. On my platform, C files end with .c; C++ files end with .C. You may have to rename them.

    1. term.h and term.c
    2. except.h, thrown by the member functions of class terminal.
    3. terminal.h and terminal.C
    4. main.C

    Then run the program consisting of the following 22 files (plus whatever additional grandchild classes you want, plus class visionary if you wrote it in C++ Part II).

    1. term.h and term.c
    2. except.h, thrown by the member functions of class terminal.
    3. terminal.h and terminal.C
    4. game.h and game.C
    5. wabbit.h and wabbit.C
    6. manual.h and manual.C
    7. The other motion classes: immobile.h, brownian.h
    8. The rank classes: inert.h, victim.h, predator.h, halogen.h
    9. The grandchild classes: wolf.h, rabbit.h, sitting_duck.h, boulder.h
    10. main.C

  3. June 10, 2009: up to Chapter 7, p. 705. Hand in one copy of the game on June 17 including Homeworks 7.2.2.b (p. 689) and 7.2.2.c (p. 691). Hand in only the files you had to create or modify; circle or highlight (by hand—don’t bother to do it in the files themselves) the code you created or modified.

  4. June 17, 2009: up to Chapter 7, p. 736. Hand in Homeworks 7.2.4.1c (template metaprogramming, p. 706) and 7.2.6d (template member function, p. 722). Pointers to data members were in Chapter 2, p. 253. A simple class similar to the class printable in Chapter 7, p. 732 is the class sat in Chapter 3, pp. 339–341.

    Yes, you can have three template preambles stacked on top of each other (Chapter 7, p. 724):

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    template <class T>
    class outer {
    public:
            template <class U>
            class inner {
            public:
                    template <class V>
                    void f();
            };
    };
    
    template <class T>
    template <class U>
    template <class V>
    void outer<T>::inner<U>::f()
    {
            cout << sizeof (T) << " " << sizeof (U) << " " << sizeof (V) << "\n";
    }
    
    int main()
    {
            outer<char>::inner<short> i;
            i.f<int>();
            return EXIT_SUCCESS;
    }
    
    1 2 4
    

  5. June 24, 2009: up to Chapter 7, p. 765. Hand in Homework 7.2.8a (p. 736). Please hand in only the files you had to change, and underline or circle the code you changed.

    The Big Idea: code is packaged as a class so that it can be passed to a template. The code is passed at compile time, as a template argument. The isprint error checking was packaged as a class printable (p. 732) so that it could be passed to the template class terminal on pp. 736–741. The > comparison was packaged as a class greater_int (p. 764) so that it could be passed to the template function sorter on p. 763. (In this case, we are passing in more than just the template argument COMP. We are also passing in the function argument comp. But comp is a big nothing—it doesn’t even have data members.)

    Run (but do not hand in) the C program qsort.c on p. 771.


  6. July 1, 2009: up to Chapter 8, p. 799. Hand in one copy of the game incorporating the three map Homeworks 8.1c, 8.1d, 8.1e. Please hand in only the files you had to modify; circle or highlight the code you changed.

  7. July 8, 2009: up to Chapter 8, p. 825. Hand in Homework 8.2.1a (node::const_iterator) on p. 811.

    The easiest thing we did on July 8: took the exposed p and fragments of code in the for loop in Chapter 8, p. 803, lines 15–17 of main.C, and packaged them in p. 802, lines 13–29 and 32–35 of node.h.

    The five hardest things we did on July 8:

    1. When p. 804, line 41 of main.C calls count, how does count decide whether it should return an int, long, or whatever?
    2. Exactly how did the base class std::iterator<forward_iterator_tag, int>, used in p. 810, line 13 of node3.h, aquire the following public member?
      typedef int value_type;
      And then how did the derived class node::iterator aquire the same member? And then how did the class iterator_traits<node::iterator> aquire the same member?
    3. On p. 815, in lines 18–20 of main1.C, why is the first record (if any) read from the input file by the iterator’s constructor, and why are the remaining records (if any) read from the input file by the iterator’s operator++?
    4. Why did the substitution property fail on p. 822?
    5. On p. 824, in line 19 of main.C, why can’t the operator* member function of it write the integer to the output file?


  8. July 15, 2009: up to Chapter 8, p. 855. Hand in Homeworks 8.3a (find_distance, p. 829), 8.3b (adjacent_find, p. 831), and 8.3c (sort, p. 833).

    Extra credit. Could you write an algorithm (i.e., a template function) that would accept an iterator passed by value (or a pair of iterators, if necessary) and return true (or at least compile) if the iterator is an input iterator? It would return false (or fail to compile) if the iterator is not an input iterator. To qualify as an input iterator, its specialization of iterator_traits must have the five typedefs. You would have to verify that statements such as the following do in fact compile.

    	typename iterator_traits<IT>:pointer p = &*it;
    	typename iterator_traits<IT>:value_type v = *p;
    
    Should the goal be to return true or merely to compile? If the goal is to return true, would the iterator(s) passed in as arguments have to refer to elements in a container, or could they be the end-of-container iterator?

    Could you write an algorithm that would accept an iterator passed by value (only one iterator this time) and return true (or at least compile) if the iterator is an output iterator? It would return false (or fail to compile) if the iterator is not an output iterator. Since an output iterator never checks for end-of-container, you don’t need to verify that you can compare two iterators. In fact, the function will need only one iterator argument. To qualify as an output iterator, its specialization of iterator_traits does not need to have the last four typedefs.

    Ditto for the other three categories: forward, bidirectional, random access.


  9. July 22, 2009: up to Chapter 8, p. 910 (except we skipped the min_element algorithm, which we’ll do in 30 seconds on July 29). Hand in Homeworks 8.4.2e (p. 860) and 8.4.2g (p. 867). Look at Homework 8.4.2f (p. 864), but don’t do it. What would be a better example for the for_each algorithm? Stroustrup’s C++ page. Please remind me to bring Einstein’s brownian movement book.

  10. July 29, 2009:

Catalog Descriptions

Here are the catalog descriptions I wrote for the course (with peppy suggestions from a student).

C++ Part I INFO1-CE9264 / Y12.1003

From online gaming services to global real-time trading systems, C++ is the language of choice for complex, large-scale applications that demand peak performance. This three-part course teaches you the techniques for creating bigger classes and objects out of smaller ones, in Part I with aggregation, in Part II with inheritance, and in Part III with templates. Part I covers the non-object topics of C++: standard i/o, expression evaluation, placement of declarations, type conversion, pointers and references, C functions called from a C++ program, function name overloading, default values for function arguments, and inline functions. You then build classes and objects without inheritance, using data members, member functions, constructors, destructors, friend functions, arrays of objects, constant and static members, pointers to members, aggregation, and operator overloading. Large-scale programming exercises. Prerequisite: knowledge of pointers and structures in the language C.

C++ Part II INFO1-CE9265 / Y12.1004

Make a long-term investment in your future as a developer with this three-semester course. Pick up from Part I with three applications of operator overloading: formatted and file i/o, dynamic memory allocation, and standard containers such as vector, list, and string. Then concentrate on building classes using the different varieties of inheritance: single and multiple, virtual and non-virtual, public and private, and inheritance from an abstract base class with pure virtual functions. See how object-oriented design results in maintainable, extensible architectures; also explore the implementations of, and alternatives to, inheritance. Report and recover from runtime errors by throwing and catching exceptions. Large-scale programming exercises. Prerequisite: C++ Part I INFO1-CE9264.

C++ Part III INFO1-CE9266

See how the problems plaguing inherently difficult programming projects are solved with the C++ Standard Template Library (STL) in Part III of this three-semester course. Deploy template functions and template classes, and explore their interaction with the inheritance in Part II. Master the design of the Standard Template Library: containers, iterators, and algorithms; iterator traits and iterator categories, predicates and other function objects, and adapters. Learn how the STL delivers performance competitive with hand-crafted assembly language; decipher the library error messages by tracing how the algorithms are dispatched. Exploit iterators and other design patterns to achieve a loosely coupled architecture for migrating software to new environments and international locales. Large-scale programming exercises. Prerequisite: C++ Part II INFO1-CE9265.

Prerequisites

INFO1-CE9264

This is a C++ course for people who already know C well enough to handle pointers, structures, and pointers to structures. You must also be able to use a pointer to a structure as the argument of a function, since this is the take-off point from C into C++. No one cares how many semesters of C you have taken, how long ago it was, or what your grades were.

You must also know the pairs of C functions fopen/fclose and malloc/free, since events in C++ happen in pairs. (These pairs are triggered by “constructors” and “destructors”.)

INFO1-CE9264 and INFO1-CE9265 are platform independent. We cover only the C++ language, not the different vendors’ compilation, execution, and debugging environments. You must therefore know how to create, compile, and execute a C or C++ program on the platform of your choice. In Unix, you must type a command line or create a script or “makefile”; in Microsoft Visual C++ and Borland C++, you must create a “project”.

You must also know how to create, compile, and execute a multi-file C or C++ program. For example, the main function of your program may call a function defined in another source file.

INFO1-CE9265

You must have mastered INFO1-CE9264.

INFO1-CE9266

You must have mastered INFO1-CE9265.

Wireless on Campus

To connect to the wireless at 48 Cooper Square, you need an NYU NetID consisting of several lowercase letters followed by several digits. If you don’t already have it, get it from here. You also need a password for the NetID. If you don’t already have it, get it from here. Then configure your device for the wireless network. The SSID is “nyu”. The security should be set to “WPA2 Enterprise.” The username and password is the NetID and its password. More detailed instructions for connecting to the WiFi with the iPhone and other devices are here.

Platforms and compilers

Any current version of C++ on any machine is okay for these courses, at home, at work, or on campus at one of the NYU computer labs.

NYU’s Unix

Every student gets a free Unix account on the host i5.nyu.edu for the duration of the course. It has the GNU compilers gcc and g++. Follow these directions.

Your own Unix or Linux (including Macintosh)

To run C and C++ on your own Unix or Linux, download the free GNU compilers gcc and g++.

Microsoft Windows

To run C and C++ on your own Windows computer, download the free Bloodshed Dev-C++, a GUI-interface for g++. Follow these directions.

Xcode on Mac OS X

Follow these directions.

Bibliography

The lectures are accompanied by a book available on the web. Here are book reviews, including C and C++ books.
  1. Working Draft, Standard for Programming Language C++
  2. C++ Primer, Fourth Edition by Stanley B. Lippman, Barbara E. Moo, and Josée Lajoie; Addison-Wesley, 2005, ISBN 0-201-72148-1.
  3. The C++ Programming Language, Special Edition by Bjarne Stroustrup; Addison-Wesley, 2000; ISBN 0201700735. Magisterial but hard to understand. Its web site has errata.
  4. The Annotated C++ Reference Manual (ARM) by Margaret A. Ellis and Bjarne Stroustrup; Addison-Wesley, 1990; ISBN 0-201-51459-1. The reference manual from the back of The C++ Programming Language, Second Edition, with discussion and examples. Organized like the Talmud: holy writ surrounded by commentary. Strong on inheritance, weak on templates, non-existant on the C++ Standard Library.
  5. The Design and Evolution of C++ by Bjarne Stroustrup; Addison-Wesley, 1994; ISBN 0-201-54330-3. The inventor of C++ explains why various features were added to and excluded from the language.
  6. Generic Programming and the STL by Matthew Austern; Addison-Wesley, 1998; ISBN 0-201-30956-4. How the C++ standard library fits together: containers, iterators, algorithms, function objects. Strong on theory, modest on examples. Great book!
  7. The STL website at Silicon Graphics
  8. The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie; Prentice-Hall, 1988; ISBN 0-13-110362-8. C++ is a superset of C, and this is the definitive (and shortest) C book. Magisterial but hard to understand.The Elements of Programming Style, Second Edition by Brian W. Kernighan and P. J. Plauger; McGraw-Hill, 1978; ISBN 0-07-034207-5. This is the greatest book ever written about programming. In particular, it’s the best book ever written about what is now pejoratively called “procedural programming” (Lippman pp. 1, 329; Stroustrup p. 23), but which was then called “structured programming”. Structured programming is a prerequisite for object-oriented programming.
  9. Design Patterns: Elements of Reusable Object-Oriented Software Erich Gamma, Richard Helm, Ralph Johnson, John M. Vlissides; Addison-Wesley, 1995, ISBN 0-201-63361-2. This is the object-oriented successor to The Elements of Programming Style. Many of the patterns are used in the C++ standard library.

John Horton Conway’s Game of Life

  1. The Wikipedia Life page.
  2. Life was first published in Scientific American 223 (October 1970): 120–123.
  3. Glider guns were presented in Scientific American 224 (February 1971): 112–117.
  4. Three-dimensional versions are in Scientific American 256 (February 1987): 8–13.
  5. TIME Magazine had a good short article on January 21, 1974.

Other C++ links

  1. Bjarne Stroustrup invented C++; here are his pages for C++ and C++0x.

Post your source code and error messages on the web

Go to ftp://ftp.ssh.org/pub/ssh/old/ and look for the highest numbered version of SSHWinClient-x.x.x.exe. It will install a program containing Fetch (secure FTP) and PuTTY (the secure shell ssh) on Windows.

Get your i5.nyu.edu login name, activate your account, and log into i5.nyu.edu via ssh. Create a public_html subdirectory of your home directory, and change the nine mode bits of both directories to rwxr-xr-x.

cd
pwd
mkdir public_html
ls -ld public_html    (minus lowercase LD)
chmod 755 . public_html      (dot means yourcurrent directory)
ls -ld . public_html
Move or copy the files that you want to display on the web to your public_html subdirectory, and change the mode bits of the files to rw-r--r--.
mv yourfile ~/public_html      (with a tilde)
cp yourfile ~/public_html
cd ~/public_html
pwd
ls -l | more          (minus lowercase L)
chmod 644 *
ls -l | more          (minus lowercase L)
Then point your web browser at http://i5.nyu.edu/~yourloginname/ (with a tilde).

Multiple inheritance from a virtual base class (“diamond inheritance”)

Then take me disappearin’ through the smoke rings of my mind,
Down the foggy ruins of time, far past the frozen leaves,
The haunted, frightened trees, out to the windy beach,
Far from the twisted reach of crazy sorrow.
Yes, to dance beneath the diamond sky with one hand waving free,
Silhouetted by the sea, circled by the circus sands,
With all memory and fate driven deep beneath the waves,
Let me forget about today until tomorrow.

Hey, Mister Tambourine Man