7/27/2019 3_C++ Programming.ppt
1/44
C++ Programming
1
7/27/2019 3_C++ Programming.ppt
2/44
Simple C++ Program
#include // include header file
using namespace std;
int main( )
{
cout
7/27/2019 3_C++ Programming.ppt
3/44
What problem do namespaces solve?
Well, suppose that you buy two different general-purpose class libraries from two
different vendors, and each library has some features that you'd like to use.
You include the headers for each class library:
#include "vendor1.h"
#include "vendor2.h"
and then it turns out that the headers have this in them:
// vendor1.h
... various stuff ...
class String { ... };
// vendor2.h
... various stuff ...
class String { ... };
This usage will trigger a compiler error, because class String is defined twice
3
7/27/2019 3_C++ Programming.ppt
4/44
The namespace feature gets around this difficulty by means of separatenamed namespaces:
// vendor1.h
... various stuff ...
namespace Vendor1 {
class String { ... };
}// vendor2.h
... various stuff ...
namespace Vendor2 {
class String { ... };
}
There are no longer two classes named String, but instead there are nowclasses named Vendor1::String and Vendor2::String.
4
7/27/2019 3_C++ Programming.ppt
5/44
How would you actually use the Str ing classes in these
namespaces?
Qualify the class name with the namespace name:
Vendor1::String s1, s2, s3;
This usage declares three strings, each of type Vendor1::String.
Another approach is to use a using directive:
using namespace Vendor1;
Such a directive specifies that the names in the namespace can be
used in the scope where the using directive occurs.
using namespace Vendor1; String s1, s2, s3; and pick up the String classfound in the Vendor1 namespace.
5
7/27/2019 3_C++ Programming.ppt
6/44
Commonly used Header Files
< iostream > - Standard input and standard output functions
- Manupulating the time and date
< fstream > - Functions that perform input from files on disk and output to
files on disk
< cstdlib > - Conversion of text to numbers, numbers to text, randomnumbers and memory allocation
6
7/27/2019 3_C++ Programming.ppt
7/44
Example 1
// Example - Display
#include
using namespace std;
int main ( )
{
cout
7/27/2019 3_C++ Programming.ppt
8/44
Example 2
/* ExampleDisplay * /
#include
using namespace std;
int main ( )
{
cout
7/27/2019 3_C++ Programming.ppt
9/44
Reading I nputs through Keyboard and F inding the Average
#include
using namespace std;
int main( )
{
float number1, number2, sum, average;
cout > number1; // Reads numbers
cin >> number2; // from keyboard
sum = number1+number2;
average = sum/2;
cout
7/27/2019 3_C++ Programming.ppt
10/44
Declaration ofvariables
// Operating wi th variables#include
using namespace std;
int main ( )
{// declaring variables:
int a, b;
int result; // process:
a = 5;b = 2;
a = a + 1;
result = a - b;
// print out the result:cout
7/27/2019 3_C++ Programming.ppt
11/44
Initialization ofvariables
// I ni tial ization of variables
#include
using namespace std;
int main ( )
{int a=5; // initial value = 5
int b(2); // initial value = 2
int result; // initial value undetermined
a = a + 3;result = a - b;
cout
7/27/2019 3_C++ Programming.ppt
12/44
Introduction to strings
// Str ing
#include
#include
using namespace std;
int main ( )
{
string mystring = "This is a string";
cout
7/27/2019 3_C++ Programming.ppt
13/44
Defined Constants
#include using namespace std;
#define PI 3.14159
#define NEWLINE '\n'
int main ( )
{
double r=5.0;
// radius double circle;
circle = 2 * PI * r;cout
7/27/2019 3_C++ Programming.ppt
14/44
Compound Assignment
When we want to modify the value of a variable by performing anoperation on the value currently stored in that variable we can use
compound assignment operators:
14
Expression Is equivalent to
value += increase; value = value + increase;
a - = 5; a = a - 5;
a / = 5; a = a / 5;
price *= unit+1; price = price * (unit+1);
7/27/2019 3_C++ Programming.ppt
15/44
Compound Assignment Operator
// compound assignment operators
#include
using namespace std;
int main ( )
{
int a, b=3;
a = b;
a+=2; // equivalent to a=a+2
cout
7/27/2019 3_C++ Programming.ppt
16/44
I ncreaseand DecreaseOperator
16
A++A += 1
A = A +1
A = 2
B = ++A
A = 3
B = 3
A = 2
B = A++
A = 3
B = 2
A = 2
B = --A
A = 1
B = 1
A = 2
B = A--
A = 1
B = 2
A--A -= 1
A = A -1
7/27/2019 3_C++ Programming.ppt
17/44
Relational and Equality Operators
17
= = Equal to!= Not equal to
> Greater than
< Less than
>= Greater than or equal to 4) // evaluates to true.(3 != 2) // evaluates to true.
(6 >= 6) // evaluates to true.
(5 < 5) // evaluates to false.
7/27/2019 3_C++ Programming.ppt
18/44
Conditional Operators
18
condition ? resul t1 : resul t2
7= =5 ? 4 : 3 // returns 3, since 7 is not equal to 5.
7= =5+2 ? 4 : 3 // returns 4, since 7 is equal to 5+2.
5 > 3 ? a : b // returns the value of a, since 5 is greater than 3.
a > b ? a : b // returns whichever is greater, a or b.
7/27/2019 3_C++ Programming.ppt
19/44
Conditional Operators
// conditional operator
#include
using namespace std;
int main ( )
{
int a, b, c;
a=2;
b=7;
c = (a>b) ? a : b;cout
7/27/2019 3_C++ Programming.ppt
20/44
Expressions and Their Types
An expression is a combination of operators, constants and variables
arranged as per the rules of the language
It may also include function call which return values
Types of Expressions
Constant expressions
Integral expressions
Float expressions
Pointer expressions
Relational expressions
Logical expressions
Bitwise expressions
20
7/27/2019 3_C++ Programming.ppt
21/44
Constant ExpressionsConsists of only constant values .
15, 20+5/2 x
I ntegral ExpressionsProduces integer results after implementing all the
automatic and explicit type conversions.
m, m*n-5 m+x
F loat ExpressionsProduce floating point result, after all conversions.
x+y 10.75 x+y/10
Pointer ExpressionsProduce address values.
&m ptr
(where m is a variable and ptr is a pointer)
21
7/27/2019 3_C++ Programming.ppt
22/44
Relational Expressionsyield results of type bool which take a value true
or falsex b && x = = 10 x = = 10 || y = = 5
Bitwise Expressionsused to manipulate data at bit level
x > 1
22
7/27/2019 3_C++ Programming.ppt
23/44
Special Assignment Expressions
Chained Assignment
x = ( y = 10 ); or x = y = 10;
Embedded Assignment
x = ( y = 50 ) + 10;
Compound Assignment
x += 10;
23
7/27/2019 3_C++ Programming.ppt
24/44
Control Structures
24
7/27/2019 3_C++ Programming.ppt
25/44
25
Control Structure
If-else
LoopSequenceSelection
switch do-while while, for
7/27/2019 3_C++ Programming.ppt
26/44
Simple i fand if else statement
26
if(x == 100)cout
7/27/2019 3_C++ Programming.ppt
27/44
The whileloop
// custom countdown using while
#include
using namespace std;
int main ( )
{
int n;
cout > n;
while (n > 0)
{
cout
7/27/2019 3_C++ Programming.ppt
28/44
The do-whileloop
// number echoer
#include
using namespace std;
int main ( )
{
unsigned long n;
do
{
cout > n;cout
7/27/2019 3_C++ Programming.ppt
29/44
The forloop
// countdown using a for loop
#include
using namespace std;
int main ( )
{for (int n=10; n > 0; n --)
{
cout
7/27/2019 3_C++ Programming.ppt
30/44
The breakstatement// break loop example
#include
using namespace std;int main ( )
{
int n;
for (n=10; n>0; n--)
{
cout
7/27/2019 3_C++ Programming.ppt
31/44
The continuestatement
// continue loop example
#include
using namespace std;
int main ( )
{
for (int n=10; n>0; n--)
{
if (n= =5) continue;
cout
7/27/2019 3_C++ Programming.ppt
32/44
The gotostatement
// goto loop example#include
using namespace std;
int main ( )
{
int n=10;
loop:
cout
7/27/2019 3_C++ Programming.ppt
33/44
The switchstatement
// Demonstrates switch statement
#include
int main( )
{
unsigned short int number;
cout > number;
switch (number)
{
case 0: cout
7/27/2019 3_C++ Programming.ppt
34/44
Arrays
34
7/27/2019 3_C++ Programming.ppt
35/44
Arrays
An array is a series of elements of the same type placed in contiguousmemory locations that can be individually referenced by adding an index to
a unique identifier.
35
A typical declaration for an array in C++ is:
type name [elements];
int billy [5];
7/27/2019 3_C++ Programming.ppt
36/44
Initializing Array Elements
int billy [5] = {16, 2, 77, 40, 12071}
36
When an initialization of values is provided for an array, C++ allows the
possibility of leaving the square brackets empty [ ].
In this case, the compiler will assume a size for the array that matches the
number of values included between braces { }
int billy [ ] = {16, 2, 77, 40, 12071}
7/27/2019 3_C++ Programming.ppt
37/44
Accessing the Values of an Array
37
billy[2] = 75;
a= billy[2];
At this point it is important to be able to clearly distinguish between the
two uses that brackets [ ] have related to arrays.
They perform two different tasks: one is to specify the size of arrays when
they are declared; and the second one is to specify indices for concretearray elements.
** Do not confuse these two possible uses of brackets [ ] with arrays.
int billy[5]; // declaration of a new array
billy[2] = 75; // access to an element of the array.
7/27/2019 3_C++ Programming.ppt
38/44
Array Example
#include using namespace std;
int billy [ ] = {16, 2, 77, 40, 12071};
int n, result=0;
int main ( )
{
for ( n=0 ; n
7/27/2019 3_C++ Programming.ppt
39/44
Multidimensional Arrays
Multidimensional arrays can be described as "arrays of arrays"
39
int jimmy [3][5];
jimmy [1][3]
7/27/2019 3_C++ Programming.ppt
40/44
Multidimensional Array
#define WIDTH 5
#define HEIGHT 3
int jimmy [HEIGHT][WIDTH];
int n, m;
int main ( )
{
for (n=0; n
7/27/2019 3_C++ Programming.ppt
41/44
Pointers
41
7/27/2019 3_C++ Programming.ppt
42/44
Reference Operator (&) / Dereference operator (*)
andy = 25;
fred = andy;
ted = &andy;
42
The variable that stores the reference to another variable is called as pointer
beth = *ted;
7/27/2019 3_C++ Programming.ppt
43/44
PointersExample 1
#include
using namespace std;
int main ( )
{
int firstvalue, secondvalue;
int * mypointer;
mypointer = &firstvalue;
*mypointer = 10;
mypointer = &secondvalue;
*mypointer = 20;
cout
7/27/2019 3_C++ Programming.ppt
44/44
Top Related