Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12COSC 181 – Foundations of Computer ProgrammingClass 2Goals for TodayFinish Up Chapter 1 – IntroductionStart Chapter 2 – Intro to C++ ProgrammingGo Ahead and Login If you can’t, sit next to someone who canTypical C++ Development1. Edit – actually write the code (labor intensive)2. Preprocess – handle compiler directives (automatic)text replacement, outside compilations (if any)3. Compile – create object code and store on disk4. Link – connect with necessary external resources (libraries, etc.) & store executable (machine code)5. Load – put program in main memory (really a function of the operating system)6. Execute – central processing unit (processor) performs each individual machine instruction and updates/stores data when/where indicatedFailing To RunComputers are superbly literal and always do only what they are instructed to and no moreIf instructions cannot be completed as given then the program will fail to run (cause an error)Run-time vs Compile-time errorsGood Programming Practices1. Keep it Simplefind the most straightforward solution2. Read the Documentation3. Make use of the resources available to you4. DON’T get frustrated5. DON’T be scared to experimentbuild your knowledge base as you go6. DON’T be forced to hurryIntroduction to C++// Fig. 2.1: fig02_01.cpp// Text-printing program.#include <iostream> // allows program to output data to the screen// function main begins program executionint main(){ std::cout << "Welcome to C++!\n"; // display message return 0; // indicate that program ended successfully} // end function mainThese are comments : denoted by “//”Important Components#include<foo>tells the preprocessor to treat the contents of a specified file (foo) as if those contents had appeared in the source program int main(){…}1 or more “functions” for each programEach function does some processing and returns a value (this value may be empty)exactly one function must be called “main”beginning of every C++ programin this case “returns” an integer valuereturn 0;In main() this is the very last statement of a C++ programIn general, it’s the very last statement of any function that its inCould also have said “return 1;” for instancewhite space – used for making code easier to readNote: each statement (command) is ended by a “;” each function is opened by “{“ and closed by “}”certain terms are “keywords” (#include, int, main, return)Have a pre-determined meaningOutputstd::cout << “Welcome to C++!\n”;the line as a whole is a statementstd::cout Standard character output streamOutput in C++ occurs over “streams” The standard stream (above) is connected to the monitor by defaultnamespace :: commandstd is the namespace defined by “iostream” cover namespaces laterOutput Continued“<<“ stream insertion operatorValue on right of << is inserted into the stream which appears on the left of <<“Welcome to C++!\n”Value on the right of the << operator“ ” – denote a literal string\n is a special command called an “escape sequence” that represents “new line” in C++the backslash (“\”) – lets the compiler know that you want to “escape” a character and denote an “escape sequence”See Fig 2.2 in your book for list of escape sequencesDevC++ QuirkinessIf we run the program as entered then we will only see the output for a flashNot long enough to tell what happenedThis is not the case in every development environmentNeed to add an extra line of code to make the program wait for us to dismiss it.Slightly Updated Version// Fig. 2.1: fig02_01.cpp// Text-printing program.#include <iostream> // allows program to output data to the screen// function main begins program executionint main(){ std::cout << "Welcome to C++!\n"; // display message system(“PAUSE”); // prepackaged wait function return 0; // indicate that program ended successfully} // end function mainNow… Try It!Open up DevC++File -> New -> Source FileType in the CodeUnder the “Execute” menu First hit “Compile”Save the code on your DesktopWait for the screen to say Done (0 errors, 0 warnings)Then, under “Execute” menu hit “Run”Congratulations on your first C++ programNow, try and make it say something


View Full Document

UVa-Wise COSC 181 - Foundations of Computer Programming

Download Foundations of Computer Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Foundations of Computer Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Foundations of Computer Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?