Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12COSC 181 – Foundations of Computer ProgrammingClass 2Goals for TodayFinish Up Chapter 1 – IntroductionStart Chapter 2 – Intro to C++ ProgrammingGo 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 RunComputers are superbly literal and always do only what they are instructed to and no moreIf 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 Simplefind the most straightforward solution2. Read the Documentation3. Make use of the resources available to you4. DON’T get frustrated5. DON’T be scared to experimentbuild 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 programEach function does some processing and returns a value (this value may be empty)exactly one function must be called “main”beginning of every C++ programin this case “returns” an integer valuereturn 0;In main() this is the very last statement of a C++ programIn general, it’s the very last statement of any function that its inCould also have said “return 1;” for instancewhite space – used for making code easier to readNote: 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 meaningOutputstd::cout << “Welcome to C++!\n”;the line as a whole is a statementstd::cout Standard character output streamOutput in C++ occurs over “streams” The standard stream (above) is connected to the monitor by defaultnamespace :: commandstd is the namespace defined by “iostream” cover namespaces laterOutput Continued“<<“ stream insertion operatorValue 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++ QuirkinessIf we run the program as entered then we will only see the output for a flashNot long enough to tell what happenedThis is not the case in every development environmentNeed 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 FileType in the CodeUnder the “Execute” menu First hit “Compile”Save the code on your DesktopWait for the screen to say Done (0 errors, 0 warnings)Then, under “Execute” menu hit “Run”Congratulations on your first C++ programNow, try and make it say something
View Full Document