Oop Unit V Unit 5 Notes
Oop Unit V Unit 5 Notes
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.
C++ getline()
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.
#include <iostream.h>
#include <fstream.h>
#include <string.h>
int main () {
// Create a text file
ofstream MyWriteFile("filename.txt");
// Use a while loop together with the getline() function to read the file line by line
while (getline (MyReadFile, myText)) {
// Output the text from the file
cout << myText;
}
// Close the file
MyReadFile.close();
}
lOMoARcPSD|45350057
#include <fstream.h>
#include <iostream.h>
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:
Exception Handling in C++ is a process to handle runtime errors. We perform exception handling
so the normal flow of the application can be maintained even after runtime errors.
In C++, exception is an event or object which is thrown at runtime. All exceptions are derived
from std::exception class. It is a runtime error which can be handled. If we don't handle the
exception, it prints exception message and terminates the program.
Advantage
It maintains the normal flow of the application. In such case, rest of the code is executed even after
exception.
o try
o catch, and
o throw
C++ try/catch
In C++ programming, exception handling is performed using try/catch statement. The C++ try
block is used to place the code that may occur exception. The catch block is used to handle the
exception.
try {
// Block of code to try
throw exception; // Throw an exception when a problem arise
}
catch () {
// Block of code to handle errors
}
#include <iostream.h>
#include<conio.h>
int main() {
try {
int age = 19;
if (age >= 18) {
cout << "Access granted - you are old enough.";
} else {
throw (age);
} } catch (int myNum) {
cout << "Access denied - You must be at least 18 years old.\n";
cout << "Age is: " << myNum;
}
getch();
return 0;
}
lOMoARcPSD|45350057
C++ Namespaces
Namespaces in C++ are used to organize too many classes so that it can be easy to handle the
application.
namespace ns
{
// A Class in a namespace
class geek
{
public:
void display()
{
cout<<"ns::geek::display()"<<endl;;
}
};
}
int main()
{
// Creating Object of geek Class
ns::geek obj;
obj.display();
return 0;
}