210 likes | 459 Views
Lecture 3: The parts of a C++ program. Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220. C++ Anatomy. The Parts of a C++ Program. Function: a group of one or more programming statements that collectively have a name.
E N D
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220
C++ Anatomy • The Parts of a C++ Program Function: a group of one or more programming statements that collectively have a name Namespace: used to organize the names of program entries, such as variables, functions, and objects Left-brace: part of the C++ syntax and encloses all the statements that make up a function Comment: ignored by the compiler Preprocessor Directive: sets up the code for the compiler // A simple C++ program #include <iostream> using namespace std; int main() { cout << “Programming is great fun!”; return 0; } Return statement: indicates the program executed succesfully Statement: consists of cout object, << operator, and string literal or constant, conluded by ;
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
Program 2-2 • Can be used to send more than one item out. // A simple C++ program #include <iostream> using namespace std; int main() { cout << “Programming is “ << “great fun!”; return 0; }
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 <iostream> using namespace std; int main() { cout << “Programming is ”; cout << “great fun!”; return 0; }
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 <iostream> 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; }
Solution: start a new line • Stream manipulator : endl (end-L or end line) • Newline escape sequence: \n // A simple C++ program #include <iostream> 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; }
// A simple C++ program #include <iostream> 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; }
Common Escape Sequences • \a Bell (beep) • \b Backspace • \n Newline • \r Return • \t Tab • \\ Backslash • \' Single quote • \" Double quote
The #include directive • Example: #include <iostream> • 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
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
// A simple C++ program #include <iostream> using namespace std; int main() { int number; number = 5; cout<< “The value in number is “ << number << endl; return 0; }
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
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
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
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
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