Unit - V - OOPS
Unit - V - OOPS
I/O Devices:
• They enable programs to interact with various I/O devices, including
the keyboard, screen, files, and network sockets
Abstraction:
• Streams provide an abstract way to handle input and output, making
it easier to write portable code that can work with different I/O
devices.
• iostream:
• This library provides standard input/output streams, including:
• cin : Standard input stream (typically keyboard).
• cout: Standard output stream (typically screen).
• fstream:
• This library provides file input/output streams:
• ifstream: Input file stream (for reading from a file).
• ofstream : Output file stream (for writing to a file)
• #include <iostream> // For standard input/output streams
using namespace std;
int main() {
int age;
return 0;
}
C++ Files and Streams
• In C++ programming we are using the iostream standard library, it
provides cin and cout methods for reading from input and writing to
output respectively.
• To read and write from a file we are using the standard C++ library
called fstream. Let us see the data types define in fstream library is:
fstream It is used to create files, write
information to files, and read
information from files.
Welcome to GHRCE.
C++ Classes.
C++ Read and Write Example
#include <fstream>
#include <iostream>
using namespace std;
int main () {
char input[75];
ofstream os;
os.open("testout.txt");
cout <<"Writing to a text file:" << endl;
cout << "Please Enter your name: ";
cin.getline(input, 100);
os << input << endl;
cout << "Please Enter your age: ";
cin >> input;
cin.ignore();
os << input << endl;
os.close();
ifstream is;
string line;
is.open("testout.txt");
cout << "Reading from a text file:" << endl;
while (getline (is,line))
{
cout << line << endl;
}
is.close();
return 0;
}
Output :-
• The cin is an object which is used to take input from the user but does
not allow to take the input in multiple lines. To accept the multiple
lines, we use the getline() function. It is a pre-defined function
defined in a <string.h> header file used to accept a line or a string
from the input stream until the delimiting character is encountered.
Syntax of getline() function:
• There are two ways of representing a function:
• The first way of declaring is to pass three parameters.
• istream& getline( istream& is, string& str, char delim );
• The above syntax contains three parameters, i.e., is, str, and delim.
• Where,
• is: It is an object of the istream class that defines from where to read the
input stream.
• str: It is a string object in which string is stored.
• delim: It is the delimiting character.
• Return value
• This function returns the input stream object, which is passed as a
parameter to the function.
• The second way of declaring is to pass two parameters.
• istream& getline( istream& is, string& str );
• The above syntax contains two parameters, i.e., is and str. This syntax
is almost similar to the above syntax; the only difference is that it
does not have any delimiting character.
• Where,
• is: It is an object of the istream class that defines from where to read
the input stream.
• str: It is a string object in which string is stored.
• Return value
• This function also returns the input stream, which is passed as a
parameter to the function.
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
string name; // variable declaration
cout << "Enter your name :" << endl;
cin>>name;
cout<<"\nHello "<<name;
return 0;
}
Output:
cout<<"\nProfile is :"<<profile;
}
Output :-
• istream and ostream serves the base classes for iostream class. The
class istream is used for input and ostream for the output.
• The istream class: This class is responsible for handling input stream.
It provides number of function for handling chars, strings and objects
such as get, getline, read, ignore, putback etc..
#include <iostream>
using namespace std;
int main()
{
char x;
cout << x;
}
#include <iostream>
using namespace std;
int main()
{
char x;
int main()
{
4 5
Output:
int main() {
// Printing error
cerr << "Welcome to GHRCE! :: cerr\n";
int main() {
int n1, n2;
int main() {
errorLog.close();
return 0;
}
In the file error_log.txt:
This is an error message written to a file.
Exception handling in c++
• In C++, exceptions are runtime anomalies or abnormal conditions that
a program encounters during its execution. The process of handling
these exceptions is called exception handling. Using the exception
handling mechanism, the control from one part of the program where
the exception occurred can be transferred to another part of the
code.
• Grouping of Error Types: In C++, both basic types and objects can be
thrown as exceptions. We can create a hierarchy of exception objects,
group exceptions in namespaces or classes, and categorize them according
to their types.
• C++ program to demonstate the use of try,catch and throw in
exception handling.
#include <iostream>
#include <stdexcept>
using namespace std;
int main()
{ // try block
try { int numerator = 10;
int denominator = 0;
int res; // check if denominator is 0 then throw
runtime
// error.
if (denominator == 0) {
throw runtime_error(
"Division by zero not allowed!"); }
// calculate result if no exception occurs
res = numerator / denominator;
//[printing result after division
cout << "Result after division: " << res << endl;
}
// catch block to catch the thrown exception
catch (const exception& e) {
// print the exception
cout << "Exception " << e.what() << endl;
}
return 0;
}
Output
Exception Division by zero not allowed!
// C++ program to demonstate the use of try,catch and throw in exception handling.
#include <iostream>
using namespace std;
int main() {
int x = -1; // Some code
cout << "Before try \n"; // try block
try {
cout << "Inside try \n";
if (x < 0) { // throwing an exception
throw x;
cout << "After throw (Never executed) \n"; } } // catch block
catch (int x) {
cout << "Exception Caught \n"; }
cout << "After catch (Will be executed) \n";
return 0; }
Output :
Before try
Inside try
Exception Caught
After catch (Will be
executed)
CASE - 1
• There is a special catch block called the ‘catch-all’ block, written as
catch(…), that can be used to catch all types of exceptions.
// C++ program to demonstate the use of catch all in exception handling.
#include <iostream>
using namespace std;
int main()
{ // try block
try { // throw
throw 10;
} // catch block
catch (char* excp) {
cout << "Caught " << excp;
}
// catch all
catch (...) {
cout << "Default Exception\n"; }
return 0; }
Output
Default Exception
CASE - 2
• Implicit type conversion doesn’t happen for primitive types.
//// C++ program to demonstate property 2: Implicit type
/// conversion doesn't happen for primitive types. // in exception handling.
#include <iostream>
using namespace std;
int main()
{
try { throw 'a';
}
catch (int x) {
cout << "Caught " << x;
}
catch (...) {
cout << "Default Exception\n"; }
return 0; }
Output
Default Exception
CASE - 3
• If an exception is thrown and not caught anywhere, the program
terminates abnormally.
• // C++ program to demonstate property 3: If an exception is
• // thrown and not caught anywhere, the program terminates
• // abnormally in exception handling.
#include <iostream>
using namespace std;
int main()
{
try {
throw 'a';
}
catch (int x) {
cout << "Caught ";
}
return 0;
}
Output