lecture 3: the parts of a c++ program professor: dr. miguel alonso jr. fall 2008 cgs2423/cop1220
TRANSCRIPT
![Page 1: Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220](https://reader035.vdocuments.us/reader035/viewer/2022081513/56649ef95503460f94c0b212/html5/thumbnails/1.jpg)
Lecture 3: The parts of a C++ program
Professor: Dr. Miguel Alonso Jr.
Fall 2008
CGS2423/COP1220
![Page 2: Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220](https://reader035.vdocuments.us/reader035/viewer/2022081513/56649ef95503460f94c0b212/html5/thumbnails/2.jpg)
C++ Anatomy
The Parts of a C++ Program
// A simple C++ program#include <iostream>using namespace std;
int main(){
cout << “Programming is great fun!”;return 0;
}
Comment: ignored by the compilerPreprocessor
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
![Page 3: Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220](https://reader035.vdocuments.us/reader035/viewer/2022081513/56649ef95503460f94c0b212/html5/thumbnails/3.jpg)
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
![Page 4: Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220](https://reader035.vdocuments.us/reader035/viewer/2022081513/56649ef95503460f94c0b212/html5/thumbnails/4.jpg)
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;}
![Page 5: Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220](https://reader035.vdocuments.us/reader035/viewer/2022081513/56649ef95503460f94c0b212/html5/thumbnails/5.jpg)
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;
}
![Page 6: Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220](https://reader035.vdocuments.us/reader035/viewer/2022081513/56649ef95503460f94c0b212/html5/thumbnails/6.jpg)
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;
}
![Page 7: Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220](https://reader035.vdocuments.us/reader035/viewer/2022081513/56649ef95503460f94c0b212/html5/thumbnails/7.jpg)
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;
}
![Page 8: Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220](https://reader035.vdocuments.us/reader035/viewer/2022081513/56649ef95503460f94c0b212/html5/thumbnails/8.jpg)
// 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;
}
![Page 9: Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220](https://reader035.vdocuments.us/reader035/viewer/2022081513/56649ef95503460f94c0b212/html5/thumbnails/9.jpg)
Common Escape Sequences
\a Bell (beep) \b Backspace \n Newline \r Return \t Tab \\ Backslash \' Single quote \" Double quote
![Page 10: Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220](https://reader035.vdocuments.us/reader035/viewer/2022081513/56649ef95503460f94c0b212/html5/thumbnails/10.jpg)
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
![Page 11: Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220](https://reader035.vdocuments.us/reader035/viewer/2022081513/56649ef95503460f94c0b212/html5/thumbnails/11.jpg)
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
![Page 12: Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220](https://reader035.vdocuments.us/reader035/viewer/2022081513/56649ef95503460f94c0b212/html5/thumbnails/12.jpg)
// 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;
}
![Page 13: Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220](https://reader035.vdocuments.us/reader035/viewer/2022081513/56649ef95503460f94c0b212/html5/thumbnails/13.jpg)
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
![Page 14: Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220](https://reader035.vdocuments.us/reader035/viewer/2022081513/56649ef95503460f94c0b212/html5/thumbnails/14.jpg)
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
![Page 15: Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220](https://reader035.vdocuments.us/reader035/viewer/2022081513/56649ef95503460f94c0b212/html5/thumbnails/15.jpg)
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
![Page 16: Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220](https://reader035.vdocuments.us/reader035/viewer/2022081513/56649ef95503460f94c0b212/html5/thumbnails/16.jpg)
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
![Page 17: Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220](https://reader035.vdocuments.us/reader035/viewer/2022081513/56649ef95503460f94c0b212/html5/thumbnails/17.jpg)
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