|
|
|
|
|
Please visit course web site periodically. |
|
www.ccs.neu.edu/course/com1100 |
|
COM1100_Winter_Announcement.html |
|
Packages are now available at Gnomon Copy. |
|
Package 1 – Patterns |
|
Package 2 – Recitation 1 ~ 5 |
|
Package 3 – Lab 1 ~5 |
|
|
|
|
|
Textbook |
|
Chapter 1 : Introduction |
|
Chapter 2 : Fundamental Data Types |
|
Package 1 – Patterns |
|
Name Use Pattern - Part 1 - Variables and
Objects |
|
Read-Process-Write Pattern |
|
Reading Data Pattern |
|
|
|
|
|
|
C++ Origins |
|
Line by line analysis of our first C++ program |
|
Output statement : cout |
|
Input statement : cin |
|
|
|
|
|
|
In 1970’s, Dennis Ritchie at AT&T Bell Labs
created the C programming language under UNIX |
|
Many ideas came from a programming language
BCPL(Basic Combined Programming Language, or B) |
|
C++ was developed in 1985 at Bell Labs by Bjarne
Stroustrup |
|
|
|
|
|
|
|
|
Bjarne added features for data abstraction and
object-oriented programming. Instead of naming it as programming language
D, the Bell Labs group in a humorous vein named it C++ (suggests an
enhancement version of C) |
|
Structural programming, not object-oriented
programming in COM1100. |
|
|
|
|
Because there are many versions of C++, we need
to have a standard. |
|
ANSI (American National Standards Institute) |
|
|
|
|
|
|
|
Writing source code (source program: test.cpp) |
|
Text editor (Notepad, VC++ IDE) |
|
Compiling (object program : test.obj) |
|
Compiler to translate source code to machine
language |
|
Linking (Executable program : test.exe) |
|
Linker to combine object files and library files
so that they can be executed as a unit |
|
Loading and Executing |
|
Loader to load .exe file into memory and be
executed |
|
|
|
|
|
|
1 #include <iostream> |
|
2 using namespace std; |
|
3 int main() |
|
4 { |
|
5 cout
<< "Hello, World\n"; |
|
6 |
|
7 return 0; |
|
8 } |
|
|
|
|
|
|
#include <iostream> |
|
– It is a preprocessor command.
Preprocessor command begins with a pound sign(#) and perform some action
before the compiler translate the source program into machine language. |
|
–
Tells the compiler to read the file iostream.h . This file contains
definition for stream input/output package. |
|
– In our example, cout performs output
onto the screen and therefore requires the service provided in iostream.h |
|
|
|
|
|
|
-- The iostream is referred as header file
because it is placed at the top, or head, of a C++ program using #include
command |
|
– Preprocessor commands do not end with a
semicolon |
|
#include <iostream> |
|
#include <string> |
|
|
|
|
|
|
–Tell the compiler that all names that are used
in the program belong to the “standard name space” |
|
– A namespace is a mechanism for expressing
logical grouping. |
|
– That is, if some declarations logically belong
together according to some criteria, they can be put in a common namespace
to express that fact. |
|
– You will always use the standard name space in
this course. |
|
|
|
|
|
|
Begins the definition of a function called main.
A C++ program begins execution in the function main. |
|
– A function is a collection of programming
instructions that carry out a particular task. |
|
– Every C++ program must have a main function |
|
– Most C++ programs contain other functions
besides main |
|
|
|
|
|
|
Curly braces marks the beginning and the ending
of the block of statements belonging to the function main. |
|
– The instructions or statements in the body of
main function – that is, the statements inside the curly braces { } – are
executed one by one. |
|
– Each statement in a C++ program ends in a semicolon. |
|
|
|
|
|
|
Invokes library function cout to display output
on the screen (standard output device). |
|
–The quoted text string is called a constant
string and is displayed as-is. The double quotes are used to delimit the
beginning and ending of the constant string and are not considered part of
the string. |
|
– The << operator denotes the “put to” or
“send to” command. |
|
|
|
|
– The two character \ and n, when used together
( \n ), is called a newline escape sequence. They tell cout to send
instruction to the display device to move to a new line. |
|
– In C++, the backslash ( \ ) character provides
an “escape” from the normal interpretation of the character following it by
altering the meaning of the next character. |
|
|
|
|
|
|
|
|
|
|
The return statement denoted the end of the main
function. |
|
When the main function ends, the program
terminates. |
|
The zero value is a signal that the program ran
successfully. |
|
|
|
|
|
|
1 #include <iostream> |
|
2 using namespace std; |
|
3 int main() |
|
4 { |
|
5 cout
<< "Hello, World\n"; |
|
6 |
|
7 return 0; |
|
8 } |
|
|
|
|
|
|
header files |
|
using namespace std; |
|
int main() |
|
{ |
|
statements; |
|
return 0; |
|
} |
|
|
|
|
|
|
Textbook, chapter1, page 26 “Advanced Topic 1.1” |
|
The header file of older compilers have an
extension .h |
|
#include <iostream.h> |
|
But new compilers do not need .h extension |
|
#include <iostream> |
|
using
namespace std; |
|
|
|
|
Purpose: |
|
Print
the value of one or more expressions |
|
Syntax: |
|
cout << expression1 <<
expression2 << … << expressionN; |
|
Expressions should be separated by << |
|
|
|
|
cout << “Hello, World \n” ; |
|
Hello,
World |
|
cout << “Hello, World ” << endl; |
|
Hello,
World |
|
cout << “Hi, ” << “Elice” << endl; |
|
Hi,
Elice |
|
cout << “Hi, \n” << “Elice” << endl; |
|
Hi, |
|
Elice |
|
|
|
|
|
|
cout << 7 + 8 ; |
|
15 |
|
cout << “7 + 8 = “ << 7 + 8 ; |
|
7 + 8
= 15 |
|
cout << “\” Hi ! \” ” << endl; |
|
“Hi !” |
|
cout << “ Hello \\ World ! “ <<
endl; |
|
Hello
\ World ! |
|
|
|
|
|
|
Purpose: |
|
Read
the value of one or more variables from the input |
|
Syntax: |
|
cin >> variable1 >> variable2
>>…>> variableN; |
|
Variables should be separated by >> |
|
|
|
|
|
|
int first, second, last; |
|
cin
>> first; |
|
cin
>> second; |
|
cin
>> last; |
|
It is also possible to read more than one values
from the keyboard |
|
int
first, second, last; |
|
cin
>> first >> second >> last; |
|
|
|
|
|
|
int first, second, last; |
|
cin
>> first >> second >> last; |
|
When type in these three numbers from keyboard,
they should be separated by white space (blank spaces, tabs, or newlines) |
|
12
6 15 |
|
12 |
|
6 |
|
15 |
|
|
|
|
C++ was developed in 1985 at Bell Labs by Bjarne
Stroustrup |
|
C++ is a better C. |
|
header files |
|
using namespace std; |
|
int main() |
|
{ |
|
statements; |
|
return 0; |
|
} |
|
cout << expression1 << expression2
<< … << expressionN; |
|
cin >> variable1 >> variable2
>>…>> variableN; |
|
|
|
|
|
|
|
|
|
|
|
|
Variable declare and use |
|
Programming style |
|
Safe I /O operations |
|
Assignment statements |
|
Arithmetic operations |
|