C++ Bascis - Ss
C++ Bascis - Ss
#include <iostream>
using namespace std;
int main( ) //function name “main”
{
cout << “Computer Engineering Department\n”;
return 0;
} //end function body
Sahar Khalid
Functions
• Functions are one of the fundamental building blocks of C++. The FIRST program
consist of a single function called main( ).
• Every function must use this pair of braces ,{ and }, around the function body.
Always Start with main()
When you run a C++ program, the first statement executed will be at the beginning of a
function called main().
#include <iostream>
using namespace std;
int main( )
{
cout << “Computer Engineering Department\n”;
return 0;
}
Sahar Khalid
Output Using cout
• cout << “Computer Engineering Department\n”;
causes the phrase in quotation marks to be displayed on the screen.
• A semicolon ; signals the end of the statement.
Directives
The two lines that begin the FIRST program are directives.
1. The first is a preprocessor directive (#include <iostream>)
• The preprocessor directive #include tells the compiler to insert another file into
your source file.
• The type file usually included by #include is called a header file. IOSTREAM is
an example of a header file
2. and the second is a using directive. (using namespace std;)
•A C++ program can be divided into different namespaces. A namespace is a part
of the program in which certain names are recognized; outside of the namespace
they’re unknown.
Sahar Khalid
Comments
• Comments help the person writing a program, and anyone else who must read
the source file, understand what’s going on.
• Comments start with a double slash symbol (//) and terminate at the end of the
line. example
#include <iostream> //preprocessor directive
• There’s a second comment style available in C++:
/* this is an old-style comment */
• You can write a multiline comment with only two comment symbols:
/* this is a potentially
very long multiline
Comment */
Sahar Khalid
Keywords
• These are reserved words of the C++ language. We should
not try to use these words as names of variables or function
names in a program.
• int, class, if, float, for, and while are examples of keywords.
Identifiers
• Identifiers are case sensitive. Identifiers are used to name variables,
functions , and various other user defined objects.
• An Identifier is a sequence of letters, Underscore ( _ ) and digits, but must
start with a letter or Underscore ( _ ).
Sahar Khalid
Variables Type
1. Integer variables represent integer numbers like 1, 30,0, and –27. integers have
no fractional part.
• The integer types are (int, long, short).
Sahar Khalid
3. Character type( char )stores integers that range in value from –128 to 127. Variables
of this type occupy only 1 byte of memory.
• Character variables are sometimes used to store numbers in this limited range,
but they are much more commonly used to store characters such as ‘a’, ‘B’, ‘$’, ‘3’,
and so on, as numbers. Use single quotation marks around a constant characters.
4. Variables of type bool can have only two possible values: true and false. compilers
often store them as bytes because a byte can be quickly accessed.
Sahar Khalid
Sahar Khalid
Unsigned Data Types
By eliminating the sign of the character and integer types, you can change
their range to start at 0 and include only positive numbers. This allows them
to represent numbers twice as big as the signed type. Table 2.3 shows the
unsigned versions.
Sahar Khalid
Assignment Statements
The statements
var1 = 20;
var2 = var1 + 10;
• The equal sign (=), as you might guess, causes the value on the right to be
assigned to the variable on the left.
• In the first line, var1, which previously had no value, is given the value 20.
• In the second program line, the plus sign (+) adds the value of var1 and 10.
The result of this addition is then assigned to var2.
• The statements
x=9;
x=x+1;
the plus sign (+) adds the value of x and 1. The result of this addition is 10
then assigned to x.
Sahar Khalid
Integer Variables Example
#include <iostream>
using namespace std;
int main()
{
int var1; //define var1
int var2; //define var2
var1 = 20; //assign value to var1
var2 = var1 + 10; //assign value to var2
cout << “var1+10 is “; //output text
cout << var2 << endl; //output value of var2
return 0;
}
Sahar Khalid
Example for character
#include <iostream>
using namespace std;
int main()
{
char charvar1 = ‘A’; //define char variable as character
char charvar2 = ‘\t’; //define char variable as tab
cout << charvar1; //display character on the screen
cout << charvar2; //display character on the screen
return 0;
}
Note:Variables can be initialized at the same time they are defined. In
this program two variables of type char—charvar1 and charvar2—
are initialized to the character constants ‘A’ and ‘\t’.
Sahar Khalid
• Escape Sequences
• ‘\t’, it’s an example of an escape sequence. In this case the t is interpreted
not as the character ‘t’ but as the tab character. In console-mode programs,
tab stops are positioned every eight spaces.
Sahar Khalid
The const Qualifier
• The keyword const (for constant) precedes the data type of a variable. It
specify that the value of a variable will not change throughout the program.
const float PI = 3.14159; //type const float
The input cin
The keyword cin (pronounced “C in”) is an object, predefined in C++ to
correspond to the standard input stream.
Sahar Khalid
Floating-Point Example
• Write a C++ program that reads the radius of a circle and computes its area.
#include <iostream> //for cout, etc.
using namespace std;
int main()
{
float rad; //variable of type float
const float PI = 3.14159; // const float
cout << “Enter radius of circle: “; //prompt
cin >> rad; //get radius
float area = PI * rad * rad; //find area
cout << “Area is “ << area << endl; //display answer
return 0;
}
Sahar Khalid
Example: input temperature in fahrenheit, output Equivalent in Celsius.
Note: Celsius =(Fahrenheit-32)*5/9
Solution
#include <iostream> program during execution:
using namespace std;
int main() Enter temperature in fahrenheit: 212
{ Equivalent in Celsius is: 100
int ftemp; //for temperature in fahrenheit
cout << “Enter temperature in fahrenheit: “; //output statement
cin >> ftemp; //input statement
int ctemp = (ftemp-32) * 5 / 9;
cout << “Equivalent in Celsius is: “ << ctemp << ‘\n’; //output
return 0;
}
• Note: you can define variables throughout a program, not just at the beginning. Look at the
variable ctemp in the above program
Sahar Khalid