1. Prepared By: Asst. Prof. Sejal Jadav
Unit-1
Principles of Object Oriented Programming Tokens,
expressions & Control Statements
(14 MARKS)
B.C.A & B.Sc.(IT) – 3
CS-13 C++ and Object Oriented
Programming
Prepared By: Asst. Prof. Sejal Jadav
2. Prepared By: Asst. Prof. Sejal Jadav
Introduction to namespace in C++
• A namespace is a declarative region that provides a scope
to the identifiers (names of the types, function, variables
etc) inside it.
• Using namespace, you can define the context in which
names are defined. In essence, a namespace defines a
scope.
3. Prepared By: Asst. Prof. Sejal Jadav
• Defining a Namespace:
A namespace definition begins with the
keyword namespace followed by the namespace name as
follows:
namespace namespace_name
{
// code declarations
}
4. Prepared By: Asst. Prof. Sejal Jadav
• To call the namespace-enabled version of either function
or variable, prepend the namespace name as follows:
• name::code; // code could be variable or function.
• Let us see how namespace scope the entities including
variable and functions:
• Let’s see C++ Namespace Example (Practical)
6. Prepared By: Asst. Prof. Sejal Jadav
Header Files in C++
• Header files contain definitions of Functions and Variables,
which is imported or used into any C++ program by using the
pre-processor #include statement.
• Header file have an extension ".h" which contains C++
function declaration and macro definition.
8. Prepared By: Asst. Prof. Sejal Jadav
• Each header file contains information (or
declarations) for a particular group of functions.
• Like stdio.h header file contains declarations of
standard input and output functions available in C++
which is used for get the input and print the output.
• Similarly, the header file math.h contains
declarations of mathematical functions available in
C++.
9. Prepared By: Asst. Prof. Sejal Jadav
• Types of Header Files in C++
• System header files: It is comes with compiler.
• (Location: C:Program Files (x86)Dev-
CppMinGW64libgccx86_64-w64-mingw324.9.2include)
• User header files: It is written by programmer.
10. Prepared By: Asst. Prof. Sejal Jadav
Why need of header files
• When we want to use any function in our C++ program
then first we need to import their definition from C++
library, for importing their declaration and definition we
need to include header file in program by using #include.
• Header file include at the top of any C++ program.
11. Prepared By: Asst. Prof. Sejal Jadav
• For example if we use clrscr() in C++ program, then we
need to include, conio.h header file, because in conio.h
header file definition of clrscr() (for clear screen) is
written in conio.h header file.
Syntax:
#include<conio.h>
12. Prepared By: Asst. Prof. Sejal Jadav
• See another simple example why use header files
Syntax
#include<iostream>
int main()
{
using namespace std;
cout << "Hello, world!" << endl;
return 0;
}
13. Prepared By: Asst. Prof. Sejal Jadav
•In above program print message on scree hello
world! by using cout but we don't define cout
here actually already cout has been declared
in a header file called iostream.
14. Prepared By: Asst. Prof. Sejal Jadav
How to use header file in Program
• Both user and system header files are include using the pre-
processing directive #include. It has following two forms:
Syntax: #include<file>
• This form is used for system header files. It searches for a file
named file in a standard list of system directives.
15. Prepared By: Asst. Prof. Sejal Jadav
Syntax: #include"file“
This form used for header files of our own program.
It searches for a file named file in the directive containing the
current file.
Note: The use of angle brackets <> informs the compiler to
search the compilers include directory for the specified file.
The use of the double quotes "" around the filename inform the
compiler to search in the current directory for the specified file.
16. Prepared By: Asst. Prof. Sejal Jadav
Namespace Declaration Rule
• Namespace declarations appear only at global scope.
• Namespace declarations can be nested within another
namespace.
• Namespace declarations don’t have access specifies. (Public
or private)
• No need to give semicolon after the closing brace of
definition of namespace.
• We can split the definition of namespace over several units.