CS62 - Spring 2011 - Lecture 29

  • Admin
       - How's HexAPawn going?
          - I suggest you follow the process from the book
          - check your solution as you go to make sure you're right before moving on. For example, the book tells you how many nodes should be in a complete tree. Check this before moving on!
          - he also gives you some hints to the side if you don't get the right number.
          - as you move on, print out what your program is doing
       - C++ from here on out
       - Optional C++ book

  • C++: a quick history
       - C was created in 1972. Back then, computers were slow, didn't have a lot of memory and were shared.
       - Because of this C was designed to be fast! Sacrifices were made with respect to security and correctness checking.
       - C++ is a new language, but based on much of the ideas of C. Crucially, all C++ was supposed to be backward compatible with C, that is, any C program should compile and run as a C++ program.
       - C++ was meant to be a replacement for C, however, since the designers were concerned that people wouldn't switch if their programs got slower, C++ made many of the same sacrifices as C.
       - This is where many of the eccentricities in C++ came fromÉ
       - The rest, came from the fact that programming languages are not static entities. We've already seen some of this with Java generics and the fact that we can't create a new array of a generic type variable. C++ has evolved over the years to include different features and, to make matters worse, the compiler and computer technology changed drastically during this period.

  • Why C++?
       - We'll talk about this more as we go, butÉ
       - C and C++ are still commonly used, particularly for applications that interact directly with the hardware.
       - Because C++ is performance driven, it gives the programmer more control and exposes more of what is happening under the covers. This allows us to better understand exactly what is happening (from an educational perpsective).

  • look at first.cpp code
       - our first C++ program!
       - what are some things that you notice that are common to Java?
          - main method
             - like Java, execution of the code starts in the main method
          - int variables
             - C++ has many of the standard types we've seen: int, double, float, char
             - and a few ones that are slightly different:
                - "long" in Java is "long int" in C++
                - "boolean" in Java is "bool" in C++
                - "unsigned int/long/short"
             - while we're on it, Java has 8 built in types:
                - short, int, long, double, float, boolean, char, byte
          - similar method construction
             - return type
             - method name
             - parameters
          - curly braces
          - control constructs
             - for loop
             - if statement
             - C++ has all the other familiar ones as well:
                - while
                - do-while
                - switch
                - if/else
          - method invocation
             - method name followed by parenthesis, which enclose the actual parameters
          - class method invocation with '.'
          - vector class: similar to ArrayList/Vector in Java
          - use of generics with the vector class, i.e. <int>
             - C++ has had generics for a while
             - Java just got them for version 1.5, which came out a few years ago (the latest version is v1.6, soon to be 1.7)
       - C++ can be challenging to get right sometimes, but it should be noted many similarities there are between Java and C++
       - what are the differences?
          - where is the class???
             - unlike java, C++ code does not need to be in a class
                - in fact, although we will use multiple files to make our life easier, you can think of it as one gigantic chunk of code
             - a function, is a standalone method, i.e. a method that is not associated with a class
          - main method
             - returns an int rather than being of type void
                - don't forget to return a value
                - a value of 0 means everything went ok
                - any other value, reports an error
             - doesn't have all that other stuff... public static
                - why did it need to be static in Java?
                   - methods have to be inside a class
                   - but to call a non-static method, we need an instance of the class (chicken and the egg :)
                - since it's not in a class, scope (public) isn't needed
             - doesn't take any arguments
                - though we can have a version that does take command-line arguments, but that's for another day
          - #include???
             - C++ can be thought of as one giant file of code (eventually)
             - if we need to use code from another class, we need to use #include
             - anything starting with # is called a pre-processor directive
                - the preprocessor runs before the compiler does
                - it can manipulate files, etc.
             - #include <x> says "copy the code from 'x' at the beginning of this file"
             - <> denotes a built-in, system class
                - Java has a lot of built-in classes we've used like anything in java.util or java.io
             - we can use quotes "" to denote a user class/file and give the path to the file
                - in Java, if they're in the same package (for now, you can think of this as being in the same directory), then you don't need to do anything
                - we've also used some user classes in Java, which one?
                   - in CS51, you used objectdraw
                   - in cs062, you used the bailey library and also the code for concurrency
          - using namespace std
             - what does the import command do in Java?
                - the import command makes our life easier
                - let's say we want to use the ArrayList class
                   - we could specify the full package path and use it that way:

                   java.util.ArrayList list = new java.util.ArrayList();
             
                   - this gets old pretty quickly
                   - if we import java.util.Arraylist, that says to make ArrayList part of our namespace, that is, whenever we refer to ArrayList, we're actually referring to java.util.ArrayList
             - in C++, the "using namespace std" commands tells the compiler to allow everything in the "std" namespace to be referred to without having to do anything special
                - equivalent of saying "import std.*" or something similar in Java
                   - Java defaults to the namespace of java.lang which includes String and a number of other useful default classes
                - in C++, to refer to something in a namespace you type the name of the namespace followed by "::", i.e. "std::"
             - the vector class is in the std namespace, so it saves us from writing std::vector everywhere
             - in general, for most of our programs, you'll want to have this there (don't forget!!!)
          - vector
             - vector, similar to the Java Vector (or ArrayList) class
             - what happened to the "get" and "set" methods of Java?
                - in C++, you can access vector elements using the "[]"
                - in fact, there is no get or set method in the sense that Java has
                - why does this work?
                   - one of the neat things you can do in C++ is "operator overloading"
                      - what is an operator?
                         - special symbols that perform specific operations
                      - what operators have we seen?
                         - +, -, ...
                         - =, +=, ...
                         - ==, !=, <=
                         - &, |, ^
                         - <<, >>
                         - ++, --
                         - ?
                      - in C++, a class can change the functionality of built-in operators, e.g. '=', '<<' or, in this case '[]'
                   - Java can't do this, which is why we need methods like "equals"
             - it looks like in the simpleVersion method we're just creating a new variable to hold a vector, but this actually creates a vector!
                - "vector<int> nums" calls the zero parameter constructor in the vector class
             - how do we call constructors with parameters?
                - "vector<int> nums(10)" call the constructor with one parameter in the vector class
             - be careful!
                - "vector<int> nums()" does NOT call the zero parameter constructor
                - you'll get a compiler error
             - vector class is sort of a blend of arrays and ArrayList class
             - vector is part of a number of data structures in the STL (Standard Template Library)
                - look at C++ references on CS page
                - look at the different STL library classes
                - look at the vector class documentation
                   - many similar methods
                      - size
                      - insert
                   - some slightly renamed methods
                      - push_back instead of add
                   - there is an "at" method, but it's not the same as get... more on this later
                   - notice it has two things that start with "operator"
                      - operator[] (overloading of the [] for element access)
                      - operator= (overloading of the =)
                         - notice that it's listed as a constructor
                         - more on this in a bit
          - what is this "cout" business?
             - made available because of "#include <iostream>"
             - cout is an outputstream (similar to streams in Java)
             - cout is the particular name for the stream that allows you to print to the console/terminal
             - again, look at C++ documentation
                - C++ Standard Library: Input/Output Stream Library
                - if we scroll down, you can see "cout"
                   - "cout is an object of class ostream that represents the standard output stream"
                   - notice that it's just an object
                - look at ostream class
                   - what do you think we'll find in this class?
                   - "operator<<"
                      - "performs an output operation on that stream"
                      - for cout, that means printing to the console
                      - notice that it has methods for many different types
                      - notice that it returns an ostream
                         - how does that help us?
                         - allows us to chain multiple << together
                      - how is this handled in Java?
                         - in Java, all classes have a toString() method
             - back to the code... what does "cout << val << endl" do?
                - prints value of val to the console
                - prints "endl" to the console?
                - endl is a variable for the "end of line"
                   - it's included with <iostream>
          - filename is first.cpp?
             - what are the filename restrictions in Java?
                - class A, must be in A.java
             - in C++, there are no naming restrictions. You can use one file, multiple files and the filenames do not matter
             - by convention, when you have a class definition, you should put it in a filename with that same name .cpp