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++.
- 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.
- 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.)
- 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.
-
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.
- 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.
- 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.
- Load the program hello.cpp by one of these three methods:
- From the menus: File->Open Project or File
- Shortcut: Ctrl+O (hold down the "Ctrl" and "O" keys together)
- 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".
- 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.
- 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!".
- 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):
- remove a semi-colon
- delete the "std::" part
- 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.
- 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).
- 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.
- We'll pause here for a group question-and-answer session about the parts
of the hello.cpp program.
- 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.
- 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?
- The program help.cpp demonstrates the two kinds of comments.
Add comments of your own of both types.
- List0206.cpp and func.cpp are examples of using functions.
(We'll pause at this point for a short lecture and Q&A session.)
- Exercises for you to do (the answers are in the files listed):
- Write a program that outputs "I love C++". [Ex0201.cpp]
- Modify func.cpp to include a subtract function. Call the
function Subtract() and use it like Add()
is used. [Ex0205.cpp]
- 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