Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMS 261 Computer Science I

Similar presentations


Presentation on theme: "COMS 261 Computer Science I"— Presentation transcript:

1 COMS 261 Computer Science I
Title: C++ Fundamentals Date: September 7, 2005 Lecture Number: 5

2 Announcements Homework Assignment Continue reading Chapter 2
Should have read 1.1 – 1.2, 2.1 – 2.5 before lab on Thursday Read/skim the lab for Thursday Bring lab handout to lab (class) on Thursday

3 Review Assembly Language High-Level Languages
Think – Edit – Compile – Execute - Test Cycle Program Organization

4 Outline Code Warrior First C++ Program Area.cpp Hello, World
Implemented in Code Warrior Project Area.cpp

5 A First Program - Hello.cpp
// Author(s): Haul Pemler // Date: 10/07/2005 #include <iostream> #include <string> using namespace std; int main() { cout << "Hello world!" << endl; return 0; } Preprocessor directives Comments Provides simple access Function named main() indicates start of program Insertion statement String Literal Ends executions of main() which ends program Function Definition

6 A First Program - Hello.cpp
Comments Not executed, so use them liberally to document your code Program and programmer name, date, what the program should do, special assumptions, input required, output produced, … // Program: Hello // Author(s): Haul Pemler // Date: 10/07/2005

7 A First Program - Hello.cpp
#include <iostream> #include <string> Preprocessor Directives Program executed before the compiler Program #include means that the contents of the file named in the angle brackets should be input Usually contains common definitions There are other types of preprocessor directives

8 A First Program - Hello.cpp
Includes all the symbols and objects in a package The package name is std Package contains many common objects used by most programs using namespace std;

9 A First Program - Hello.cpp
Defines a special function called main It is the first function called when the program is executed Therefore: All C/C++ programs must have a function call main int main() { cout << "Hello world!" << endl; return 0; }

10 A First Program - Hello.cpp
Functions can return a value when they are finished The int before main () { means the function called main returns an integer Where does the main function return the integer to? Part of the ANSI Standard int main() { cout << "Hello world!" << endl; return 0; }

11 A First Program - Hello.cpp
<< the insertion symbol (operator) Insert the string literal “Hello, World” into an object called cout Defined in the namespace called std Called the standard output, stdout cout knows to print the string (and other things) to the console int main() { cout << "Hello world!" << endl; return 0; }

12 A First Program - Hello.cpp
int main() { cout << "Hello world!" << endl; return 0; } endl Manipulator defined in iostream << endl Insert a carriage return into the cout object Flush the output buffer Print all items inserted into the cout object ; statement terminator Identifies the end of the statement

13 A First Program - Hello.cpp
Immediately stops function execution Communicates a value to the caller Returning a zero from the main function means the function terminated normally int main() { cout << "Hello world!" << endl; return 0; }

14 Hello Output


Download ppt "COMS 261 Computer Science I"

Similar presentations


Ads by Google