C++ Seminar: 1094 Session 1

Handouts: Excerpts from "Day 1" of "Teach Yourself C++", answers for Days 1 through 7, Dev-C++ flier

Your goals for today: Try out Dev-C++ on some simple C++ programs and start getting familiar with elements of the language.


Day 0: Logging On

Your goal is to log on, download some C++ "source code", and then start up Dev-C++.

  1. Log on with your Physics Department account. If you don't know your username and/or password, let one of the "facilitators" know. You may need to work with someone else for the first session.
  2. In Windows, create a directory ("folder") called C++ on your "U" directory (or with another name or somewhere else) as a place to store the files we'll use. (Ask someone if you don't know how to do this.)
  3. Start up a web browser ("Firefox" is recommended but you can use "Internet Explorer" as well). Go to the C++ Seminar home page (http://www.physics.ohio-state.edu/~ntg/cpp) and find the "1094 Sessions" section. For now download only the "Day 1" files: hello.cpp, Ex0101.cpp, Ex0103.cpp, and Ex0104.cpp: "Right click" (click with the right mouse button) on each of them in turn and "Save Link as ..." each one to your C++ directory.
  4. While you are on the web page, try the Safari online books by clicking on the link Day 1 Text. You can access these books from off-campus sites by signing in through the library.
  5. Also start a tab with http://www.cplusplus.com, which is an excellent resource. Try using the search today when you get stuck or confused.
  6. Start up Dev-C++ by looking under Start->All Programs->Bloodshed Dev-C++. A Dev-C++ window should open up. Answer "ok" to all of the questions (set up may take a couple of minutes). Now we're ready to roll.

Day 1: Getting Started

Your goal here is for you to compile/link/run some simple C++ programs and identify what happens when there is an error in the program.

  1. Load the program hello.cpp by one of these three methods:
    1. From the menus: File->Open Project or File
    2. Shortcut: Ctrl+O (hold down the "Ctrl" and "O" keys together)
    3. Icons: Click the 2nd icon on the 1st row of icons (point at it with your mouse to get a momentary title)
    There are (at least) three ways to do anything in Dev-C++, analogous to these. Use the window that pops up to track down the hello.cpp file you save and press "open".
  2. The program (about 9 lines total, including blank lines) should have appeared in the big window. It will look quite cryptic if you've never programmed before! Don't Panic! All will be explained.
  3. Let's compile, link, and run it (see the discussion and figure of the "Development Cycle" in the "Teach Yourself" handout). Use the 3rd icon on the 2nd line ("Compile & Run") or F9 or Execute->Compile & Run. You should get a little status box and then a pop-up window that says "Hello World!".
  4. Try modifying the program to create some errors and see what Dev-C++ does to get used to error messages. Some possibilities (try these then invent your own):
    1. remove a semi-colon
    2. delete the "std::" part
    3. remove a } (or change it from "}" to ")" or "]")
    Each time, "compile and run" again. If there is an error, a section at the bottom of the Dev-C++ window should pop up and give an error message with the line number it thinks is a problem and an explanation (which is often not helpful!). Fix the error and make sure the program still works.
  5. Try loading and running Ex0103.cpp. See if you can identify and fix the error (hint: compare to hello.cpp, which should still be available via a tab, using the line number given in the pop up).
  6. Load Ex0101.cpp and guess here what it does first:

    Then compile and run it. Were you correct?

Day 2: The Anatomy of a C++ Program

Your goal here is to learn more about C++ programs.

  1. We'll pause here for a group question-and-answer session about the parts of the hello.cpp program.
  2. Download List0202.cpp for a sample program that uses "cout" to print data in various ways. Try some modifications (e.g. changing numbers, adding "\n" [new line] or "\t" [tab] in places), predicting the output before compiling and running. Replace "Jesse Liberty" with your name.
  3. Download List0203.cpp for an example of the "using" keyword. Note that that std:: is not in front of the "cout" and "endl" commands. Compare to List0202.cpp to see how this is done. List0204.cpp is another way to do this. What's the difference from List0203.cpp?

  4. The program help.cpp demonstrates the two kinds of comments. Add comments of your own of both types.
  5. List0206.cpp and func.cpp are examples of using functions. (We'll pause at this point for a short lecture and Q&A session.)
  6. Exercises for you to do (the answers are in the files listed):
    1. Write a program that outputs "I love C++". [Ex0201.cpp]
    2. Modify func.cpp to include a subtract function. Call the function Subtract() and use it like Add() is used. [Ex0205.cpp]
    3. Write the smallest program that can be compiled, linked, and run. [Ex0202.cpp]

C++ Seminar: 1094 Session 1. Last modified: 10:54 pm, November 05, 2006.
furnstahl.1@osu.edu