OOPS Notes - Unit-2
OOPS Notes - Unit-2
Credits: 4
• A Compiler, like GCC, to translate the C++ code into a language that the computer will understand
Example
IDE = Integrated Development Environment
Compiler
Debugger A debugger is a software tool that can help the software development process by identifying coding errors at various
stages of the operating system or application development
Basics of C++ >> ASCII Code
Basics of C++ >> ASCII Code
Let’s look at a very simple C++ program. This program is called FIRST, so its source file is FIRST.CPP. It simply prints a sentence on the screen.
Basics of C++ >> Understanding Simple C++ Program >> Directives
Pre-processor directive
Using directive
• The first line of the FIRST program might look like a program statement, but it’s not. It isn’t part of a function body and
doesn’t end with a semicolon, as program statements must.
• Instead, it starts with a number sign (#). It’s called a preprocessor directive. A program statements are instructions to
the computer to do something, such as adding two numbers or printing a sentence. A preprocessor directive, on the
other hand, is an instruction to the compiler.
• A part of the compiler called the preprocessor deals with these directives before it begins the real compilation process.
The preprocessor directive #include tells the compiler to insert another file into your source file. In effect, the #include
directive is replaced by the contents of the file indicated. #include is only one of many preprocessor directives, all of
which can be identified by the initial # sign.
• In the FIRST example, the preprocessor directive #include tells the compiler to add the source file IOSTREAM to the
FIRST.CPP source file before compiling
• A Stream refers to the flow of data in the form of sequence of bytes in and out of the program
• In C++, streams handle all I/O operations. Streams, which help to consume data from various sources, namely, keyboard,
mouse, storage devices, etc. In order to supply it to the program are called input streams. Streams also receive data
from the program and direct it to the physical devices or other programs. Such streams are called output streams
Streams facilitate the data to be passed in and out of a medium such as a console or a file.
The stream class istream defines the behaviour of the extraction operator (>>), which is used to extract data from various
sources such as keyboard. The istream supports reading the basic types of data from various input streams.
The stream class ostream defines the behaviour of the insertion operator (<<) which is used to direct the output to various
destinations such as screen.
The stream class defines some standard stream in the I/O headers to handle the console I/O operations. This section discusses
these standard streams and the I/O headers.
The four standard built-in stream objects, namely, cin, cout, cerr and clog enable the use of standard input and output
devices
Basics of C++ >> Understanding Simple C++ Program>>Output Using Cout
• The standard output stream normally flows to the screen display—although it can be redirected to other output devices.
• It directs the contents of the variable on its right to the object on its left.
• In FIRST it directs the string constant “Every age has a language of its own\n” to cout, which sends it to the display.
• (If you know C, you’ll recognize << as the left-shift bit-wise operator and wonder how it can also be used to direct output. In
C++, operators can be overloaded. That is, they can perform different activities, depending on the context)
Basics of C++ >> Understanding Simple C++ Program>>Input Using Cin
• The statement cin >> ftemp; causes the program to wait for the user to type in a number. The resulting number is placed
in the variable ftemp.
• The keyword cin (pronounced “C in”) is an object, predefined in C++ to correspond to the standard input stream. This
stream represents data coming from the keyboard (unless it has been redirected).
• The >> is the extraction or get from operator. It takes the value from the stream object on its left and places it in the
variable on its right.
Basics of C++ >> Understanding Simple C++ Program>>Header Files
Basics of C++ >> Understanding Simple C++ Program>>Header Files
Basics of C++ >> Understanding Simple C++ Program>>Namespace
• The directive says that all the program statements that follow are within the std namespace. Various program
components such as cout are declared within this namespace. If we didn’t use the using directive, we would need to
add the std name to many program elements. For example, in the FIRST program we’d need to say
To avoid adding std :: dozens of times in programs we use the using directive instead
Basics of C++ >> Understanding Simple C++ Program>>Namespace
• The end of a line isn’t important to a C++ compiler. Actually, the compiler ignores whitespace almost completely.
Whitespace is defined as spaces, carriage returns, linefeeds, tabs, vertical tabs, and formfeeds. These characters are
invisible to the compiler.
• You can put several statements on one line, separated by any number of spaces or tabs, or you can run a statement
over two or more lines. It’s all the same to the compiler.
Two Types:
Basics of C++ >> Understanding Simple C++ Program>>Comments