Sie sind auf Seite 1von 24

Session 1

Prof. M. N. Sahoo
Dept. of CSE, NIT Rourkela
Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 1 of 29
Reference Books

Programming with C++ by John R Hubbard, 2nd Ed

Object Oriented Programming in C++ by Robert Lafore, 4th Ed

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 2 of 29


Session Objectives
Define the structure of a C++ program

Identify the standard input and output functions

Use comments, width() and endl

Execute a c++ program in linux environment

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 3 of 29


Programs
A program can be defined as a set of instructions, which tell
a computer what to do in order to serve the requirements of
the user.

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 4 of 29


Evolution Of C++

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 5 of 29


C++ Program Structure
Let us see a simple C++ program -
#include <iostream.h>
Using namespace std;

int main()
{
cout<< Hello there !!!;
}

The above program prints the following message on your screen

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 6 of 29


# include . statement
The # include statement is the first statement in any C++ program.

It is the most fundamental set of instructions, which is required for


writing any C++ program.

The # notation at the beginning of the statement indicates that the


following instruction is a special instruction to C++.

It establishes a reference to the header file.

It is termed as a preprocessor directive.

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 7 of 29


namespace
namespace is named group of definitions

cout is an output stream object defined in namespace std

so cout object can be accessed as std::cout

e.g. std::cout << Hello there !!!;

The using namespace std; tells compiler to apply prefix std::


to any object or function of std, if referred inside the current
scope

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 8 of 29


The main() function
Functions can be defined as a group of program statements.

The execution of every program in C++ begins with the function


main().

It marks the starting point of execution of the program.

This is a special name recognized by the system under which C++


runs.

The main() function is thus the centralized point for all processing
in a C++ program.

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 9 of 29


Processing Statements
The statement declaring the main() function is followed by the
symbol {.
The right brace } indicates the end of the main() function.
All processing statements related to the main() function
should be defined within the curly braces.
The area within the braces is defined as the body of the function.

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 10 of 29


Header Files
Header files are used to enable the feature of reusability of program
code.

The function present in a library can be used through a header file


without having access to its actual code structure.

To enable this feature you need to include a declaration of the


function, contained in a .h file, called the header file

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 11 of 29


Input / Output
Input is the process of retrieving data from an input device - Keyboard
Output is the process of transmitting data to an output device - Monitor

The I / O operations in C++ involve moving bytes from devices to


memory and vice versa in a consistent and reliable manner.

using namespce std;


int

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 12 of 29


Standard Input Streams
The following object, in combination with its corresponding insertion
operator performs input in C++.

The insertion operator is


The object used with the cin
corresponds to statement
the standard for the input to be
input stream. redirected to the memory.
Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 13 of 29
Standard Output Streams
The following object, in combination with its corresponding extraction
operator performs output in C++.

The extraction operator is


The object used with the cout
corresponds to the statement
standard output for the output to be
stream. redirected to the monitor.
Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 14 of 29
Cascading I / O Operators
The input and output streams can be used in combination and this
method of using I / O streams is referred to as Cascading of I / O
operators.

using namespce std;


int

cout << \nEnter your age and salary : ;


cin >> age >> salary;

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 15 of 29


Formatting In C++ - 1
Output in C++ can be formatted using special characters associated
with the cin and cout statements.
Example :

using namespce std;


int

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 16 of 29


Formatting In C++ - 2

Output :

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 17 of 29


Formatting Functions In C++ - 1

This function inserts a new line.

It clears the current output stream of any existing data.

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 18 of 29


Formatting Functions In C++ - 2

The width function used by the output stream is used to


indicate the current output stream width.

It is also used to modify the output stream width.

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 19 of 29


Certain Essentials - 1
The essential components of a program construct are -

Functions are defined to break up large tasks into smaller tasks

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 20 of 29


Certain Essentials - 2

Delimiters { } are used to delimit blocks of code in loops and


functions.

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 21 of 29


Certain Essentials - 3

Each code instruction in C++ must be terminated with a semi-


colon (;).

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 22 of 29


Certain Essentials - 4

Comments can be single line comments (//) or multiple line


comments (/* */)

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 23 of 29


Executing a C++ program in Linux Environment

Write the program in VI or VIM editor.


Save the file with .cpp extension. (e.g add.cpp)
Compile the file in the $ prompt with the command g++.
e.g. g++ add.cpp
After the successful compilation, see the output by
./a.out

Linux, C, C++/ Object Oriented Programming with C++ / Session 1/ 24 of 29

Das könnte Ihnen auch gefallen