Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3 -- 1Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.

Similar presentations


Presentation on theme: "Lecture 3 -- 1Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of."— Presentation transcript:

1 Lecture 3 -- 1Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of the program rUse a compiler to convert the source file into a machine code file (object file). l convert from English to binary with machine operations rUse a linker to convert the object file into an executable file. l include other machine code that the program requires l In Visual C++ make sure you create an empty project or you will get linker errors rRun the executable file. rIterate from any point as needed. Editor Program.cpp Compiler Program.obj Libraries Linker Program.exe Run the Program Done success errors

2 Lecture 3 -- 2Computer Science I - Martin Hardwick The First C++ Program // Hello World Program // Author: Martin Hardwick #include using namespace std; int main () { cout << Hello World! << endl; return 0; } rAnything following // is a comment intended to improve the readability of the program. r#include is used to tell the compiler and linker what library resources the program uses. l the resource defines everything you need to display messages on the screen rThe line using namespace std; says you want to use standard c++ names for things rThis program contains one function named main.

3 Lecture 3 -- 3Computer Science I - Martin Hardwick Defining A C++ Function rA C++ function has the following form: rThe name of this function is main. rThe word int, means that this functions returns an integer number. l usually 0 to indicate that the program ran correctly. l this is what the return statement does at the end of the function rThe braces define the beginning and the end of the function. rThe first line of the function is called the function header. int main ( ) { sequence of statements separated by semicolons... return 0; }

4 Lecture 3 -- 4Computer Science I - Martin Hardwick Displaying Messages On The Screen rTo display a message on the computer screen use the cout statement. rThe << operator says to display whatever immediately follows it. l the << operator applies only to the first thing following it l to display two things, use the << operator twice, once immediately before each rThe word endl means to start a new line. rText strings must always be surrounded by double quotes. rMore examples: cout << This is a message to display << endl; cout << Hello World! << endl; cout << First part of long message << Second part of long message << endl;

5 Lecture 3 -- 5Computer Science I - Martin Hardwick C++ Variables rA computer does not remember numbers from one statement in your program to the next. l you must save any number that you will need later in a word of memory (called a variable in C++) rEach variable that a C++ program uses must be declared. l specify a name for the variable l specify the type of the variable –valid types include:int-- one word integer double -- two word floating-point (real) rThe variable declarations in a C++ function are usually placed at the beginning of that function by convention. rExamples double radius; int quantity;

6 Lecture 3 -- 6Computer Science I - Martin Hardwick Assignment Statement rAn assignment statement is used to put a value into a variable. l The previous value is replaced rSyntax: = ; l is any declared variable in the program l is anything that produces a value of the appropriate type (more on this later) l the number produced by the is assigned as the new value for the variable rExamples: size = 10; width = length - 2; count = count + 1;

7 Lecture 3 -- 7Computer Science I - Martin Hardwick The cin Object rUsed to assign a value typed on the keyboard to a variable. rThe >> operator says to assign the next value typed on the keyboard to the variable appearing immediately after the >> operator. rThe user must type a value followed by the Enter Key. rAn integer number is converted to a real number for assignment to a double variable. cin >> x; cin >> y; cin >> z; equivalent to: cin >> x >> y >> z; If x, y and z are double variables and the user types: 10.2 -5.2 6 x is assigned 10.2 y is assigned -5.2 z is assigned 6.0 The user can also type: 10.2 -5.2 6

8 Lecture 3 -- 8Computer Science I - Martin Hardwick Names In C++ rVariable names in C++ are called identifiers. l identifiers are used for many other things as well rRules for constructing valid identifiers in C++: l can contain letters (upper and lower case), digits, and the underscore ( _ ) character l cannot start with a digit l cannot be a reserved word l can be at most 256 characters long l are case sensitive l Must not already be used rReserved words in C++: asm auto bool break case catch char class const const_cast continue default delete do double dynamic_cast delete else enum explicit extern false float for friend goto if inline int long mutable namespace new operator private protected public register reinterpret_cast return short signed sizeof static static_cast switch template this throw true try typedef typeid typename union unsigned using virtual void volatile wchar_t while union unsigned

9 Lecture 3 -- 9Computer Science I - Martin Hardwick Putting it all together rLets write a program to convert dollars to euros l Find out current exchange rate l Get amount in dollars l Apply formula to convert from dollars to euros l Display amount in euros int main () { } rQuiz 1 Monday after Labor Day (September 14)


Download ppt "Lecture 3 -- 1Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of."

Similar presentations


Ads by Google