Basics of a Typical C++
Environment
Program is created in the editor and
Editor Disk stored on disk
Preprocessor Disk Preprocessor program processes the code
Compiler Compiler creates object code and stores it on
Disk disk
Linker Linker links the object code with the
Disk libraries, creates a.out and stores it
Primary memory on disk
Loader
Loader puts program in memory
Disk
Primary memory
CPU CPU takes each instruction and executes
. it, possibly storing new data values
as the program executes
Introduction to C++ programming
A simple program: printing a line of Text
// A first program in C++
#include <iostream> //this line is going to be preprocessed
int main ()
{
std::cout<<“welcome to c++!\n”; // this is a statement
Return 0; //indicates that program ended successfully
}
Comment-semicolon (syntax error)-standard output stream object.
// and /*….*/ Comments Readability-
documentation
Comments ignored by compiler no
corresponding object code.
#include <iostream> preprocessor directive
– Includes the content of ht input/output stream header
file <iostream>
– When should we insert it?
– What happens if we don’t?
Int main()part of every C++ program
()maina program building
blockfunction
C++ program 1 or more functions one of
which is Main.
“Main” 1st function executed
– Even if not 1st function in program
“Int” main return type
return 0 int (more later)
{ and } braces limits the body of each
function.
What’s a statement??
The Semicolon ; statement terminator
Input and output in C++ streams of
characters.
Std::cout <<“welcome to c++”;
– Stream of characters = welcome to c++
– Receiver: the standard output stream object:
» Std::cout connected to the screen
<< stream insertion operator
>> stream extraction operator
Std::cout
– cout=name
– Std=namespace
We should insert std before every
occurrence of cout-cin tedious
The using statement
\n: \ escape sequence
Common Escape sequences
Escape Sequence Description
\n New Line
\t Horizontal Tab
\r Carriage Return:1st of
current line. no
\a Alert: system bell
\\ Backslash
\” Double quote
Return 0 int main
“Return” C++ keyword exiting functions
Return 0: 0successful termination
DO NOT FORGET TO CLOSE THE {
Other versions of COUT statement
1st
– Std :: cout << “welcome ”;
– Std :: cout <<“to c++!\n”;
– ???
2nd
– Std :: cout <<“\”welcome\nto\n\n\nC++!\””;
– ???
Introduction to C++ programming
Another simple program: adding two integers
// Addition program
#include <iostream>
int main ()
{
Int integer1, integer2, sum; //declarations
std::cout<<“Enter first integer\n”; // prompt
std::cin >>integer1; // read an integer
std::cout<<“Enter second integer\n”; // prompt
std ::cin >>integer2; //read an integer
sum = integer1 + integer2; //assignment of sum
std::cout <<“Sum is “sum <<std::endl; //print sum
Return 0; //indicates that program ended successfully
}
Int integer1, integer2, sum;
– Multiple declarations
– Or each declaration alone
Declaration allocating a location in memory for
a value to be stored.
Int integer1the variables will contain integer
values. (otherwise runtime or compilation error).
Declaration data type + name.
Int integers
Double real numbers
Char a single character. (int or char)
Example
Note:
– Char x =2;
– Char y =3;
– Char sum =x + y;
– Std::cout<<“the sum of x and y is “sum <<“\n”;
Identifiers
Identifiers variable names
C++ identifiers:
– Any series of characters (letters, digits, underscores) not starting
with a digit.
– C++ is Case Sensitive A1 ≠ a1
Declarations could take place anywhere before its use in a
statement or assignment.
Sum= Integer1 + integer2; assignment.
Std::cout<< “enter 1st digit\n”;
Std::cin>>integer1;
– Waits for the user (keyboard);
– Assigns the entered value to the var integer1.
– The computer converts the character
presentation to an integer (in this case).
Subsequent use of integer1same value
Std::cout << “sum is” <<sum <<std::endl;
– Output: sum is (the sum value)
– Endl endline (another name in std
namespace)
– Endl prints an new line (\n) + flushes the
output buffer.
Another possibility:
– Std::cout << sum is”<<integer1 +
integer2<<std::endl;
Memory Concepts
Variable names such as integer1, integer2 and
sum actually corresponds to locations in the
computer’s memory. Every variable has a name,
a type, a size and a value.
In the addition program, when the statement :
std::cin >>integer1; is executed, the characters
typed by the user are converted to an integer
that is placed into a memory location to which
the name integer1 has been assigned by the C++
compiler.
Suppose the user enters the number 45 as the
value for integer1, the computer will place
45 into location integer1:
Example:
Memory
integer1 45
integer2 72
sum 117