Download presentation
Presentation is loading. Please wait.
Published byAlvin Griffith Modified over 9 years ago
1
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220
2
C++ Anatomy The Parts of a C++ Program // A simple C++ program #include using namespace std; int main() { cout << “Programming is great fun!”; return 0; } Comment: ignored by the compiler Preprocessor Directive: sets up the code for the compiler Namespace: used to organize the names of program entries, such as variables, functions, and objects Function: a group of one or more programming statements that collectively have a name Left-brace: part of the C++ syntax and encloses all the statements that make up a function Statement: consists of cout object, << operator, and string literal or constant, conluded by ; Return statement: indicates the program executed succesfully
3
The cout Object cout is used to produce console output, hence cout! It is classified as a stream object, which means it works with a stream of data, such as a string literal (stream of characters) Run Example << is the stream insertion operator The item immediately to the right of << is sent to cout for display
4
Program 2-2 Can be used to send more than one item out. // A simple C++ program #include using namespace std; int main() { cout << “Programming is “ << “great fun!”; return 0; }
5
Program 2-3 Output can also be broken up into many statements, but still shows up on a single line. // A simple C++ program #include using namespace std; int main() { cout << “Programming is ”; cout << “great fun!”; return 0; }
6
Program 2-4 Unless otherwise specified, the output of cout is displayed on a continuous stream. Sometimes, this is not desireable // A simple C++ program #include using namespace std; int main() { cout << “The following items were top sellers”; cout<<“during the month of June:”; cout<<“Computer Games”; cout<<“Coffee”; cout<<“Apirin”; return 0; }
7
Solution: start a new line Stream manipulator : endl (end-L or end line) Newline escape sequence: \n // A simple C++ program #include using namespace std; int main() { cout << “The following items were top sellers”<< endl; cout<<“during the month of June:” << endl; cout<<“Computer Games” << endl; cout<<“Coffee” << endl; cout<<“Apirin” << endl; return 0; }
8
// A simple C++ program #include using namespace std; int main() { cout << “The following items were top sellers\n”; cout<<“during the month of June:\n”; cout<<“Computer Games\n”; cout<<“Coffee\n”; cout<<“Apirin\n”; return 0; }
9
Common Escape Sequences \a Bell (beep) \b Backspace \n Newline \r Return \t Tab \\ Backslash \' Single quote \" Double quote
10
The #include directive Example: #include This is a header file that contains information describing iostream objects, such as cout Part of the input-output stream library Header file contains C++ code and typically describes complex objects, like cout Later on, we will create our own header files
11
Variables and Literals Variables represent storage locations in the computers memory int number; variable definition Sets up name and type of data the variable will hold number = 5; variable assignment (integer literal) number = “5”; string literal
12
// A simple C++ program #include using namespace std; int main() { int number; number = 5; cout<< “The value in number is “ << number << endl; return 0; }
13
Identifiers Identifier: programmer-defined name that represents some element of a program Example: variable names Usually indicates what the variable is used for Caveat: cannot use C++ key words and must be legal
14
Identifier Rules First character must be a – z, A – Z, or an underscore( _ ) After the first character, a – z, A – Z, an underscore, or 0 – 9 Upper and lower are distinct, that is C++ is case sensitive
15
Integer Data Types Two general data types: Numeric Character Number is further broken down integer floating point Variables declared as integers can only hold whole numbers i.e. 1, 2, 3… Floating point can store decimal numbers i.e. 4.657, -8.96, 3.5
16
Concerns when selecting numeric data types Largest and smallest value that may be stored How much memory that value uses Signed or unsigned Decimal Precision
17
Tips use unsigned Combine same type variable definitions on one line Use “L” after integer literals if long is needed Example : number = 32L Uses 4 bytes of memory instead of just 2 Program examples 2-10 and 2-11
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.