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

Basic Input Output in CPP

The document discusses input and output in C++. It explains that input and output in C++ occurs through streams of bytes and describes input streams as flowing from devices into memory and output streams as flowing from memory to devices. It lists common header files for input/output operations like iostream, iomanip, and fstream. It describes the cin and cout objects for input and output and how they are used with extraction and insertion operators. Two short code examples are provided to demonstrate basic input and output using cout and cin.

Uploaded by

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

Basic Input Output in CPP

The document discusses input and output in C++. It explains that input and output in C++ occurs through streams of bytes and describes input streams as flowing from devices into memory and output streams as flowing from memory to devices. It lists common header files for input/output operations like iostream, iomanip, and fstream. It describes the cin and cout objects for input and output and how they are used with extraction and insertion operators. Two short code examples are provided to demonstrate basic input and output using cout and cin.

Uploaded by

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

Basic Input / Output in C++

C++ comes with a library which provides us with many ways for performing input and
output. In C++ input and output is performed in the form of a sequence of bytes or
more commonly known as streams.
• Input Stream: If the direction of flow of bytes is from the device (for example,
Keyboard) to the main memory then this process is called input.
• Output Stream: If the direction of flow of bytes is opposite, i.e. from main
memory to device( display screen ) then this process is called output.

Header files available in C++ for Input/Output operations are:


1. iostream: iostream stands for standard input-output stream. This header file
contains definitions to objects like cin, cout, cerr etc.
2. iomanip: iomanip stands for input output manipulators. The methods declared
in this files are used for manipulating streams.
This file contains definitions of setw, setprecision etc.
3. fstream: This header file mainly describes the file stream. This header file is used
to handle the data being read from a file as input or data being written into the
file as output.
The two keywords cout in C++ and cin in C++ are used very often for printing outputs
and taking inputs respectively.

These two are the most basic methods of taking input and printing output in C++. To
use cin and cout in C++ one must include the header file iostream in the program.
I. Standard output stream (cout): Usually the standard output device is the display
screen. The C++ cout statement is the instance of the ostream class. It is used to
produce output on the standard output device which is usually the display
screen.
The data needed to be displayed on the screen is inserted in the standard output
stream (cout) using the insertion operator(<<).
// using cout
#include <iostream>
using namespace std;
int main()
{
cout << "Welcome to CPP";
return 0;
}
Output: Welcome to CPP
In the above program the insertion operator(<<) inserts the value of the string
variable sample followed by the string "Welcome to CPP"; in the standard output
stream cout which is then displayed on screen.

II. standard input stream (cin): Usually the input device in a computer is the
keyboard. C++ cin statement is the instance of the class istream and is used to
read input from the standard input device which is usually a keyboard.
The extraction operator(>>) is used along with the object cin for reading inputs.
The extraction operator extracts the data from the object cin which is entered
using the keboard.

#include <iostream>
using namespace std;

int main()
{
int age;
cout << "Enter your age:";
cin >> age;
cout << "\nYour age is: " << age;
return 0;
}
Output: Enter your age: 18
Your age is: 18
1. Simple C++ program to add two numbers

// sum of 2 nos
#include <iostream>
using namespace std;

int main()
{
int num1, num2;
cout<<"Enter first integer number: ";
cin>>num1;
cout<<"Enter second integer number: ";
cin>>num2;
cout<<"Sum of entered numbers is: "<<num1+num2;
return 0;
}
Output:
Enter first integer number: 10
Enter second integer number: 20
Sum of entered numbers is: 30

The above program is same like C program.


2. A simple program in CPP with a class

1. Class→ variables + methods 2. main ()→ object 3.call methods

// class program to find sum of 2 nos

#include <iostream>

using namespace std;

class Add // defining class like structure


{
public:

int num1, num2;

void sum() // defining method like c function


{
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
cout<<"sum= "<<num1+num2;
}
};

int main()
{

Add obj; // creating object to Add class like struct variable

obj.sum(); // calling method by Add class object → like c function calling

return 0;
}

Output:
Enter first number: 21
Enter second number: 19
Sum=40

You might also like