0% found this document useful (0 votes)
13 views

OOPS Notes - Unit-2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

OOPS Notes - Unit-2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Course Name: Object Oriented Programming Systems (OOPS)

Course Code : CS/IT103

Credits: 4

Prepared by: Dr. Rakesh Chowdhury

Assistant Professor (EEE)


Email: [email protected]
UNIT-2: Basics of “C++”
Basics of C++ >> IDE

To start using C++, you need two things:

• A Text editor, like Notepad, to write C++ code

• A Compiler, like GCC, to translate the C++ code into a language that the computer will understand

In this Course, we will use an “IDE”

Example
IDE = Integrated Development Environment

Text Editor Compiler Debugger

Note: Web-based IDE's can work as well, but functionality is limited.


Basics of C++ >> IDE
Text Editor
The first thing you do while writing a code or program is to writing a
code using your keyboard. You can use notepad or notepad++. These are
known as text editor.

Compiler

Once the program is written, it should be


converted into machine language. For
this we need a compiler.

How complier works?

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

More Details: https://www.ascii-code.com/


Basics of C++ >> Understanding Simple C++ Program

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.

• The type file usually included by #include is called a header file.


Basics of C++ >> Understanding Simple C++ Program>>iostream

• 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

Fig. Console I/O Stream Classes


Basics of C++ >> Understanding Simple C++ Program>>iostream
Basics of C++ >> Understanding Simple C++ Program>>iostream

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 identifier cout (pronounced “C out”) is actually an object.

• It is predefined in C++ to correspond to the standard output stream.

• A stream is an abstraction that refers to a flow of data.

• The standard output stream normally flows to the screen display—although it can be redirected to other output devices.

• The operator << is called the insertion or put to operator.

• 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 “using” Directive


• A C++ program can be divided into different namespaces. A namespace is a part of the program in which certain
names are recognized; outside of the namespace they’re unknown.

• 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

To avoid adding std ::


dozens of times in
programs we use the using
directive instead
Basics of C++ >> Understanding Simple C++ Program>>Whitespaces

• 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.

• Thus, the FIRST program could be written this way:


Basics of C++ >> Understanding Simple C++ Program>>Comments

Ignores the comment Makes use of comment to


understand the working of code

Two Types:
Basics of C++ >> Understanding Simple C++ Program>>Comments

What will be the output??

You might also like