Introduction to programming / C++


For this class, we will be working with C and C++ to explore programming computers to solve problems.


As this is an intrductory course, we will be looking at programming in general, and will be working with other languages as well.


Compiler

C and C++ are Compiled languages. The program you write in human readable text must be converted to a machine readable code or compiled.


On the lab computers now is a compiler named Turbo C - http://en.wikipedia.org/wiki/Turbo_C%2B%2B

Here is a link to the download - http://www.codegear.com/downloads/free/turbo


Development Cycle:

If every program worked the first time you tried it, the complete development cycle would be:

write the program,

compile the source code

link the program and run it.


When you are working on new things, sometimes everything doesn't work perfectly. You could make typing errors, or you could have a flaw in your thinking/program strategy.


Here is a more complete listing of the development cycle:

Start

Edit source code

Compile

Fix errors in the code

Link

Fix linking errors

Run program

Fix run time errors

Done


Hello World!

Hello World is a standard way of proving that something works.

With a Hello World program, everything extra is stripped out, and it is just basic function.


Here is the code for a Hello World program in C++:

#include <iostream.h>


Hello.cpp


int main()

{

cout << “Hello World!\n;”

return 0;

}


Code this and compile it. Fix errors in your typing. It should display “Hello World!” on the screen.


Parts of a simple program.


#include <iostream.h>

Lines of code that start with # are handled by the preprocessor. This code is read before anything else in your code.

This command 'brings in' an entire program called iostream.h It is as if you typed the whole program into your code.


Include signals that what will follow is a file name.

Iostream.h is a program used by cout to handle input and output


main() is a function. Every c++ program has a main() function. A function is a block of code that performs one or more actions. Functions are invoked or called.by other functions, but main() is called automatically. main() is called automatically when you start your program. main() must state what kind of value it will return. main() always returns an int or integer.


All functions begin with an opening brace { and end with a closing brace} Everything between the opening and closing braces is considered part of the function.


cout is used to print a message to the screen.


\n tells cout to put a new line after the words Hello World!


Return 0 hands control back to the operating system.


Comments

In programming, the human users need to understand what is going on in the code. Each language has its own technique to show comments. In c++ there are two types of comments:

// is a single line comment.

/* and */ are used to comment multiple lines. Everything in between the start /* of the comment and the end */ will be ignored by the compiler.


Functions

main() is a function. It is called automatically when you start your program. All other functions are called by your code, as your program runs. A program is executed line by line in the order it appears in your source code until a function is called, then the program branches off to execute the function. When the function finishes, it returns control to the next line of the calling function. You can call functions from main() or from other functions.


Functions ether return a value or they return void (nothing) main() always returns an int. A function that adds two integers might return the sum, so could be defined to return an integer value. A function that just prints a message has nothing to return, and would be declared to return void.


int Sum(int a, int b)

Functions consist of a header and a body. The header consist of the return type, the funciton name and the parameters to that function. The parameters to a function allow values to be passed into the function. If the function were to add two numbers, the numbers would be the paramters to the function.


A parameter is a declaration of what type of value will be passed in. The actual value is passed in by calling the value with an argument. Parameters and arguments are relatively interchangeable terms.


The body of a function consists of an opening brace { zero or more statements and a closing brace} Statements do the work of the function. A function may return a value using a return statement. This statement will also cause the function to exit. If you don't put a return statement in your function it will automatically return void at the end of the function. The value returned must of the type declared in the function header.


Programming activities:

1. Write a simple HelloWorld program that displays the words Hello World! on the screen.

Save the file as Hello.cpp Compile your program, fix any errors and run the file.  Comment your name, file name and date into the top of the code.

2. Write a program that displays your initials using ascii text.  The letters should be 10 lines tall. Save the program as Initials.cpp on your hard drive.  Compile it and fix any errors.