C++ - Chapter 2 - C++ Programming Basics
C++ - Chapter 2 - C++ Programming Basics
PROGRAMMING IN C++
Contents
2
Chapter
Content
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Structures
Chapter 5
Functions
Chapter 6
Chapter 7
Chapter 8
Operator Overloading
Chapter 9
Inheritance
Chapter 10 Pointers
Chapter 11 Virtual Functions and Polymorphism
Ahmed Mohamed Sayed Ahmed - Computer lecturer
1.
2.
3.
4.
5.
6.
7.
8.
Getting Started
Basic Program Construction
Output Using cout
Directives
Comments
Integer Variables
Character Variables
Input with cin
1. Getting Started
4
1. Getting Started
5
1. Getting Started
6
File
New
1. Getting Started
7
1. Getting Started
8
In Project name box, write a name for your project, for example
My Project. Then press OK button.
4
1. Getting Started
9
1. Getting Started
10
1. Getting Started
11
File
New
1. Getting Started
12
10
1. Getting Started
13
In File name box, write a name for your source code file, for example
FIRST. Then press OK button.
11
12
1. Getting Started
14
1. Getting Started
15
#include<iostream>
using namespace std;
int main( )
{
cout<<"Hello World. "<<endl;
system("pause");
return 0;
Output:
Hello World.
}
Press any key to continue
Ahmed Mohamed Sayed Ahmed - Computer lecturer
1. Getting Started
18
main function
The parentheses following the word main are the
distinguishing feature of a function. Without the
parentheses the compiler would think that main refers to
a variable.
Well put parentheses following the function name. Later
on well see that the parentheses arent always empty.
Theyre used to hold function arguments: values passed
from the calling program to the function.
The word int preceding the function name indicates that
this particular function has a return value of type int.
Ahmed Mohamed Sayed Ahmed - Computer lecturer
1. Getting Started
19
1. Getting Started
20
Program Statements
A semicolon signals the end of the statement. If you
leave out the semicolon, the compiler will often
(although not always) signal an error.
The last statement in the function body is return 0;. This
tells main ( ) to return the value 0 to whoever called it,
in this case the operating system or compiler.
In older versions of C++ you could give main ( ) the
return type of void and dispense with the return
statement, but this is not considered correct in Standard
C++.
You can put several statements on one line, separated
by any number of spaces or tabs.
Ahmed Mohamed Sayed Ahmed - Computer lecturer
#include<iostream>
using namespace std;
int main( )
{ cout<<"Hello World. "<<endl; system("pause"); return 0; }
#include
<iostream>
using namespace std;
int main( )
{ cout<<"Hello World. "<<endl; system("pause"); return 0; }
or
cout<<"Hello World. \n";
Ahmed Mohamed Sayed Ahmed - Computer lecturer
4. Directives
24
The two lines that begin the FIRST program are directives.
#include<iostream>
using namespace std;
4. Directives
25
#include<iostream>
4. Directives
26
5. Comments
27
5. Comments
28
// comments.cpp
// demonstrates comments
#include<iostream>
//preprocessor directive
using namespace std;
//using directive
int main( )
//function name main
{
//start function body
cout<<"Hello World. "<<endl;
//
system("pause");
//statement
return 0;
//statement
}
//end function body
Ahmed Mohamed Sayed Ahmed - Computer lecturer
5. Comments
29
potentially
very long
multiline
comment
*/
5. Comments
30
6. Integer Variables
31
6. Integer Variables
32
Variable Names
You can use uppercase and lowercase letters, and the digits
from 1 to 9. You can also use the underscore (_).
The first character must be a letter or underscore.
The compiler distinguishes between uppercase and
lowercase letters, so Var is not the same as var or VAR.
You cant use a C++ keyword as a variable name. A
keyword is a predefined word with a special meaning. int,
class, if, and while are examples of keywords.
A variables name should make clear to anyone reading the
listing the variables purpose and how it is used.
Ahmed Mohamed Sayed Ahmed - Computer lecturer
6. Integer Variables
33
6. Integer Variables
34
6. Integer Variables
35
#include<iostream>
using namespace std;
int main( )
{
int var1;
int var2;
var1 = 20;
var2 = var1 + 10;
cout<<"var1+10 is ";
cout<<var2<<endl;
system("pause");
return 0;
}
Ahmed Mohamed Sayed Ahmed - Computer lecturer
//define var1
//define var2
//assign value to var1
//assign value to var2
//output text
//output value of var2
Output:
var1+10 is 30
Press any key to continue
Thursday, November 22, 2012
6. Integer Variables
36
6. Integer Variables
37
Output Variations
The statement cout<<"var1+10 is "; displays a string constant.
The next statement cout<<var2<<endl; displays the value of
the variable var2.
The output of the program is var1+10 is 30
As you can see, the output of the two cout statements
appears on the same line on the output screen. No linefeed
is inserted automatically. If you want to start on a new line,
you must insert a linefeed yourself.
cout<<"var1+10 is \n";
cout<<"var1+10 is "<<endl;
or
cout<<var2<<endl;
cout<<var2<<endl;
7. Character Variables
38
Type char stores integers that range in value from 128 to 127.
Variables of this type occupy only 1 byte (eight bits) of memory.
Character variables are much more commonly used to store
ASCII characters.
The ASCII character set is a way of representing characters such
as a, B, $, 3, and so on, as numbers.
Character constants use single quotation marks around a
character, like a and b. (Note that this differs from string
constants, which use double quotation marks).
When the C++ compiler encounters such a character constant, it
translates it into the corresponding ASCII code. The constant a
appearing in a program, for example, will be translated into 97.
ASCII Chart
39
ASCII
Chart
ASCII Chart
40
ASCII Chart
41
7. Character Variables
42
#include<iostream>
using namespace std;
int main( )
{
char ch1 = 'A';
char ch2 = '\t';
cout<<ch1;
cout<<ch2;
ch1 = 'B';
cout<<ch1;
cout<<'\n';
return 0;
}
Ahmed Mohamed Sayed Ahmed - Computer lecturer
7. Character Variables
43
Escape Sequences
The character constant \t is an example of an escape sequence.
The name reflects the fact that the backslash causes an escape
from the normal way characters are interpreted.
Escape Sequence
Character
\n
Newline
\t
Tab
\'
\"
\\
Backslash
\ xdd
Hexadecimal notation
7. Character Variables
44
// fahren.cpp
// demonstrates cin, newline
#include<iostream>
using namespace std;
int main( )
{
int ftemp;
//for temperature in fahrenheit
cout<<"Enter temperature in fahrenheit: ";
cin>>ftemp;
int ctemp = (ftemp-32) * 5 / 9;
cout<<"Equivalent in Celsius is: "<<ctemp<<'\n';
return 0;
Output:
}
Enter temperature in fahrenheit: 212
Equivalent in Celsius is: 100
Press any key to continue
Ahmed Mohamed Sayed Ahmed - Computer lecturer
Expressions
Any arrangement of variables, constants, and operators that
specifies a computation is called an expression.
Thus, alpha+12 and (alpha-37)*beta/2 are expressions.
Precedence
Any Arithmetic operation proceeds according to an
established precedence of operations. Thus, Precedence is
the order in which operators are evaluated.
Precedence Summary
48
#include<iostream>
using namespace std;
int main( )
{
int a=4, b=6, c=3, d, e, f, g;
d = a + b * c;
e = (a + b) * c;
f = a * b / c;
g = a * (b / c);
cout<<"d = "<<d<<'\t';
cout<<"e = "<<e<<'\n';
cout<<"f = "<<f<<'\t';
cout<<"g = "<<g<<'\n';
return 0;
}
Ahmed Mohamed Sayed Ahmed - Computer lecturer
Output:
d = 14 e = 30
f=8 g=8
Press any key to continue
Thursday, November 22, 2012
Precedence Summary
49
Type float
38
Type float stores numbers in the range of about 3.4x10
to 3.4x1038, with a precision of seven digits. It occupies 4
bytes in memory.
Type double
The larger floating point types, double, is similar to float
except that it requires more memory space and provide a
wider range of values and more precision.
Type double requires 8 bytes of storage and handles
numbers in the range from 1.7x10308 to 1.7x10308 with a
precision of 15 digits.
Ahmed Mohamed Sayed Ahmed - Computer lecturer
//circarea.cpp
//demonstrates floating point variables
#include<iostream>
//for cout, etc.
using namespace std;
int main( )
{
float rad;
//variable of type float
const float PI = 3.14159F;
//type 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;
Output:
}
Enter radius of circle: 0.5
Ahmed Mohamed Sayed Ahmed - Computer lecturer
Area is 0.785398
Press any key to
continue
22, 2012
Thursday,
November
Exponential Notation
You can write floating-point constants using exponential notation.
Exponential notation is a way of writing large numbers without
having to write out a lot of zeros.
For example, 1,000,000,000 can be written as 1.0E9 in
exponential notation. Similarly, 1234.56 would be written
1.23456E3. (This is the same as 1.23456 *103).
The number following the E is called the exponent. It indicates
how many places the decimal point must be moved to change the
number to ordinary decimal notation.
The exponent can be positive or negative. The exponential
number 6.35239E5 is equivalent to 0.0000635239 in decimal
notation. This is the same as 6.35239 *105.
Ahmed Mohamed Sayed Ahmed - Computer lecturer
// width1.cpp
// demonstrates need for setw manipulator
#include<iostream>
using namespace std;
int main( )
{
long sd1=83, sd2=78, sd3=91;
cout<<"Name "<<"Degree"<<endl;
cout<<"Ahmed "<<sd1<<endl;
cout<<"Ali "<<sd2<<endl;
cout<<"Mohamed "<<sd3<<endl;
return 0;
}
Output:
Name Degree
Ahmed 83
Ali 78
Mohamed 91
Press any key to continue
// width1.cpp
// demonstrates need for setw manipulator
Output:
NAME DEGREE
Ahmed
83
Ali
78
Mohamed
91
Press any key to continue
#include<iostream>
#include<iomanip>
// for setw
using namespace std;
int main( )
{
long sd1=83, sd2=78, sd3=91;
cout<<setw(8)<<"NAME"<<setw(10)<<"DEGREE"<<endl;
cout<<setw(8)<<"Ahmed"<<setw(10)<<sd1<<endl;
cout<<setw(8)<<"Ali"<<setw(10)<<sd2<<endl;
cout<<setw(8)<<"Mohamed"<<setw(10)<<sd3<<endl;
return 0;
}
Ahmed Mohamed Sayed Ahmed - Computer lecturer
Output:
#include<iostream>
*****NAME**DEGREE
#include<iomanip>
// for setw
****Ahmed*******83
using namespace std;
********Ali*******78
int main( )
*Mohamed *******91
{
Press any key to continue
long sd1=83, sd2=78, sd3=91;
cout<<setw(8)<<setfill('*')<<"NAME;
cout<<setw(10)<<setfill('*')<<"DEGREE"<<endl;
cout<<setw(8)<<setfill('*')<<"Ahmed";
cout<<setw(10)<<setfill('*')<<sd1<<endl;
cout<<setw(8)<<setfill('*')<<"Ali";
cout<<setw(10)<<setfill('*')<<sd2<<endl;
cout<<setw(8)<<setfill('*')<<"Mohamed";
cout<<setw(10)<<setfill('*')<<sd3<<endl;
return 0;
}
Ahmed Mohamed Sayed Ahmed - Computer lecturer
Output:
#include<iostream>
NAME
DEGREE
#include<iomanip>
// for setw
Ahmed
83
using namespace std;
Ali
78
int main( )
Mohamed 91
{
Press any key to continue
long sd1=83, sd2=78, sd3=91;
cout<<setw(8)<<setiosflags(ios::left)<<"NAME;
cout<<setw(10)<<setiosflags(ios::left)<<"DEGREE"<<endl;
cout<<setw(8)<<setiosflags(ios::left)<<"Ahmed";
cout<<setw(10)<<setiosflags(ios::left)<<sd1<<endl;
cout<<setw(8)<<setiosflags(ios::left)<<"Ali";
cout<<setw(10)<<setiosflags(ios::left)<<sd2<<endl;
cout<<setw(8)<<setiosflags(ios::left)<<"Mohamed";
cout<<setw(10)<<setiosflags(ios::left)<<sd3<<endl;
return 0;
}
Ahmed Mohamed Sayed Ahmed - Computer lecturer
62
Thanks for
Listening