You are on page 1of 17

Fundamentals of Programming

(FOP)
Instructor: Adeela Waqar
adeela@mcs.edu.pk, adeela.abbas@gmail.com

Military College of Signals, Rawalpindi
Lecture 18A
Other C++ Topics
06 January 2014
Including C++ Library Header Files
2
Including User Defined Header Files
3


Including User Defined Header Files
4
int add(int x,int y);

int add(int x,int y)
{
return x+y;
}

add.h File
add.cpp File

#include <iostream>
using namespace std;

#include add.h

main()
{
cout<<Sum of 4 and 5
is << add(4,5) << endl;
}

main.cpp File
Notice
add.h is
included
5
namespaces
Program has identifiers in different scopes
Sometimes scopes overlap, lead to problems
Namespace defines scope
Place identifiers and variables within namespace
Access with namespace_name::member
Unnamed namespaces are global
Need no qualification
Namespaces can be nested
6
namespaces
using statement
using namespace namespace_name;
Members of that namespace can be used without preceding
namespace_name::
Can also be used with individual member
Examples
using namespace std
Discouraged by some programmers, because includes
entire contents of std
using namespace std::cout
Can write cout instead of std::cout
Example 1
#include <iostream>
using namespace std;
namespace first
{
int x = 5;
int y = 10;
}
namespace second
{
double x = 3.1416;
double y = 2.7183;
}
7
Example 1 (cont. . .)
int main ()
{
using first::x;
using second::y;
cout << x << endl;
cout << y << endl;
cout << first::y << endl;
cout << second::x << endl;
return 0;
}

8
9
1 // Example 2
2 // Demonstrating namespaces.
3 #include <iostream>
4
5 using namespace std; // use std namespace
6
7 int integer1 = 98; // global variable
8
9 // create namespace Example
10 namespace Example {
11
12 // declare two constants and one variable
13 const double PI = 3.14159;
14 const double E = 2.71828;
15 int integer1 = 8;
16
17 void printValues(); // prototype
18
19 // nested namespace
20 namespace Inner {
21
22 // define enumeration
23 enum Years { FISCAL1 = 1990, FISCAL2, FISCAL3 };
24
25 } // end Inner
26
27 } // end Example
Note the using statement.
This includes all of std,
allowing us to use cout and
endl.
Note the nested namespace
Inner.
Create a new namespace,
Example. Note that it has a
variable integer1,
different from the global
integer1.
10
28
29 // create unnamed namespace
30 namespace {
31 double doubleInUnnamed = 88.22; // declare variable
32
33 } // end unnamed namespace
34
35 int main()
36 {
37 // output value doubleInUnnamed of unnamed namespace
38 cout << "doubleInUnnamed = " << doubleInUnnamed;
39
40 // output global variable
41 cout << "\n(global) integer1 = " << integer1;
42
43 // output values of Example namespace
44 cout << "\nPI = " << Example::PI << "\nE = "
45 << Example::E << "\ninteger1 = "
46 << Example::integer1 << "\nFISCAL3 = "
47 << Example::Inner::FISCAL3 << endl;
48
49 Example::printValues(); // invoke printValues function
50
51 return 0;
52
53 } // end main
Create an unnamed
namespace. Its variables are
global File Scope
11
54
55 // display variable and constant values
56 void Example::printValues()
57 {
58 cout << "\nIn printValues:\ninteger1 = "
59 << integer1 << "\nPI = " << PI << "\nE = "
60 << E << "\ndoubleInUnnamed = " << doubleInUnnamed
61 << "\n(global) integer1 = " << ::integer1
62 << "\nFISCAL3 = " << Inner::FISCAL3 << endl;
63
64 } // end printValues
12
doubleInUnnamed = 88.22
(global) integer1 = 98
PI = 3.14159
E = 2.71828
integer1 = 8
FISCAL3 = 1992

In printValues:
integer1 = 8
PI = 3.14159
E = 2.71828
doubleInUnnamed = 88.22
(global) integer1 = 98
FISCAL3 = 1992
Properties of Namespaces
We can have more than one namespace of the
same name. This gives the advantage of defining
the same namespace in more than one file
(although they can be created in the same file as
well).
The anonymous namespace you have created
will only be accessible within the file you
created it in.
13
14
typedef
Keyword typedef
Makes synonyms (aliases) for previously defined data types
Does not create new type, only an alias
Creates shorter type names
Example
typedef int* intPtr;
Defines new type name intPtr as synonym for type int*
intPtr myintPtr;
int * myintPtr;
Same as
15
Bitwise Operators
Data represented internally as sequences of bits
Each bit can be 0 or 1
8 bits form a byte
char is one byte
Other data types larger (int, long, etc.)
Low-level software requires bit and byte manipulation
Operating systems, networking

16
Bitwise Operators
Bit operators
& (bitwise AND)
1 if both bits 1, 0 otherwise
| (bitwise inclusive OR)
1 if either bit 1, 0 otherwise
^ (bitwise exclusive OR)
1 if exactly one bit is 1, 0 otherwise
Alternatively: 1 if the bits are different
~ (bitwise one's complement)
Flips 0 bits to 1, and vice versa
17
Bitwise Operators
Bit operators
<< (left shift)
Moves all bits left by specified amount
Fills from right with 0
int x = 16;
x = x << 2;
>> (right shift with sign extension)
Moves bits right by specified amount
Fill from left can vary

You might also like