Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed

Similar presentations


Presentation on theme: "Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed"— Presentation transcript:

1 Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
CP Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed

2 Basics Comments in C/C++ Preprocessor Directives Name Spaces
Header Files Program Entry Point Return Type

3 Basics … Programming Practices Identifiers Keywords Constants
Variables Algorithms Expressions Statements

4 Basic C++ Program Structure
/********************************************************** * Header Comments **********************************************************/ pre-processor directives global declarations int main() { declarations and executable statements return 0; }//end block of main // comments required by some organizations!

5 #include <iostream.h> //declares standard I/O library
/***************************************************** * Sum two numbers ******************************************************/ #include <iostream.h> //declares standard I/O library int main() //must have one and only one function named main { int number1, number2; //declare two integer variables cout << “enter two integers” << endl; //prompt for input cin >> number1 >> number2; //input values for two variables from keyboard cout << number1 + number2; //output sum of two numbers to the screen return 0; }//end block of main

6 Our First C++ Program Enter this program, then compile and run it. */
/* Program #1 - A first C++ program. Enter this program, then compile and run it. */ #include <iostream.h> // main() is where program execution begins. int main() { cout << “Hello, world!"; return 0; } Write on the blackboard too.

7 Dissection of Program Comments have two forms in C++
//Single line comments /*Multi-line comments*/ /* Program #1 - A first C++ program. Enter this program, then compile and run it. */ This is a comment (C style) Ignored by the compiler i.e. can write anything and the compiler won’t see it at all Multiline or single or mixed with code. Why use comments?

8 Dissection of Program C++ style comment
// main() is where program execution begins. C++ style comment Compiler ignores the whole line If multiline comments, use // on each line

9 Dissection of Program int main() { program statements return 0; } Every C++ program has to have this Function; execution begins here even if other functions there; operating system calls main( ) and main returns to it int means returns an integer; () means takes no arguments; {…} signify start and end of function Variations on it e.g. main( ); int main(void); and more

10 Dissection of Program cout << “Hello, world!"; Think of cout as the monitor screen (console output) << gives direction of data; Put to or Insertion operator Prints the entire string within quotes to the screen This whole line is a statement C/C++ Statement that ends with a ;

11 Dissection of Program #include <iostream> Tells preprocessor to include contents of the text file iostream with your source code before compiling iostream has details about things that handle input and output e.g. cout iostream is called an include file or header file This is called an include directive or preprocessor directive Can also use <iostream.h>; then no need to use the namespace construct on the next slide. Older version

12 Dissection of Program using namespace std;
Namespaces are regions where your function and variable names are recognized The standard library is defined in a namespace called std. E.g. cout part of std namespace; similarly all others in iostream Two functions with same names = confused compiler Declare each in its own namespace namespacemale::naseem namespacefemale::naseem std::cout Or mynamespace::cout

13 #include files <filename.h> //Standard C library header file
<filename> //Standard C++ library files Files found in the directory defined by the INCLUDE environment variable “myfile.h” //Your files Files found in the current directory

14 Simple I/O cin cout streams input from standard input
uses the >> operator (input operator, stream extraction operator) cout streams output to standard output uses the << operator (output operator, stream insertion operator)

15 Algorithms A series of instructions in a specific order leading to a solution Analogous to recipe, directions, procedure, routine Anyone knows the origin of the word Algorithm? Ninth century Persian mathematician and astronomer Al-Khowarizmi Algebra also derived from the name of his book.

16 Rise&Shine Algorithm Any comments? Get out of bed Take off pajamas
Take a shower Get dressed Eat breakfast Carpool to work Any comments?

17 A Variation of Rise&Shine
Get out of bed Take off pajamas Get dressed Take a shower Eat breakfast Carpool to work Any comments?


Download ppt "Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed"

Similar presentations


Ads by Google