0% found this document useful (0 votes)
8 views5 pages

Oop Unit V Unit 5 Notes

This document provides notes on Object-Oriented Programming in C++, focusing on file handling using the fstream library, exception handling, and namespaces. It explains how to read and write files, handle runtime errors using try/catch blocks, and organize classes with namespaces. Code examples illustrate the concepts discussed throughout the notes.

Uploaded by

seema.chaudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Oop Unit V Unit 5 Notes

This document provides notes on Object-Oriented Programming in C++, focusing on file handling using the fstream library, exception handling, and namespaces. It explains how to read and write files, handle runtime errors using try/catch blocks, and organize classes with namespaces. Code examples illustrate the concepts discussed throughout the notes.

Uploaded by

seema.chaudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

lOMoARcPSD|45350057

Oop UNIT v - unit 5 notes

object oriented programming using C++ (Chaudhary Charan Singh


University)
lOMoARcPSD|45350057

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:

Data Type Description

fstream It is used to create files, write information to files, and read information from files.

ifstream It is used to read information from files.

ofstream It is used to create files and write information to the 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");

// Write to the file


MyWriteFile << "Files can be tricky, but it is fun enough!";

// Close the file


MyWriteFile.close();

// Create a text string, which is used to output the text file


string myText;

// Read from the text file


ifstream MyReadFile("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:

Writing to a text file:


Please Enter your name: Nakul Jain
Please Enter your age: 22
Reading from a text file: Nakul Jain
22
lOMoARcPSD|45350057

C++ Exception Handling

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.

C++ Exception Handling Keywords

In C++, we use 3 keywords to perform exception handling:

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.

For accessing the class of a namespace, we need to use namespace name::classname. We


can use using keyword so that we don't have to use complete name all the time.

// A C++ program to demonstrate use of class


// in a namespace
#include<iostream>
using namespace std;

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;
}

You might also like