Category Archives: OOP with C++

59. Error handling during file operations

Sometimes during file operations, errors may also creep in. For example, a file being opened for reading might not exist. Or a file name used for a new file may already exist. Or an attempt could be made to read past the end-of-file. Or such as invalid operation may be performed. There might not be… Read More »

57. File Stream

So far, we have been using the iostream standard library, which provides cin and cout methods for reading from standard input and writing to standard output respectively. This tutorial will teach you how to read and write from a file. This requires another standard C++ library called fstream, which defines three new data types − Sr.No Data Type & Description 1 ofstream… Read More »

58. Reading and writing to files

Writing to a File While doing C++ programming, you write information to a file from your program using the stream insertion operator (<<) just as you use that operator to output information to the screen. The only difference is that you use an ofstream or fstream object instead of the cout object. Reading from a File You read information from a… Read More »

56. Custom Exception class

User-Defined Exceptions The new exception can be defined by overriding and inheriting exception class functionality. Let’s see the simple example of user-defined exception in which std::exception class is used to define the exception. #include <iostream>  #include <exception>  using namespace std;  class MyException : public exception{      public:          const char * what() const throw()          {              return “Attempted to divide by zero!\n”;          }  };  int main()  {      try      {          int x, y;          cout << “Enter the two numbers : \n”;          cin >> x >> y;          if (y == 0)          {              MyException z;              throw z;          }          else          {              cout << “x / y = ” << x/y << endl;          }      }      catch(exception& e)      {          cout << e.what();      }  }   Output: Enter the two numbers : 10 2 x / y = 5 Output: Enter the two numbers : 10 0 Attempted to divide by… Read More »

55. Pre-defined exceptions

Standard Exceptions C++ provides a list of standard exceptions defined in <exception> which we can use in our programs. These are arranged in a parent-child class hierarchy shown below − Here is the small description of each exception mentioned in the above hierarchy − Sr.No Exception & Description 1 std::exceptionAn exception and parent class of all the… Read More »

54. Throw statement

throw − A program throws an exception when a problem shows up. This is done using a throw keyword. Throwing Exceptions Exceptions can be thrown anywhere within a code block using throw statement. The operand of the throw statement determines a type for the exception and can be any expression and the type of the result of the expression determines… Read More »

53. Try and catch block

Catch – a program that utilises an exception handler to catch an Exception. It is added to the part of a program where you need to deal with the error. Try – the try block recognises the code block for which certain exceptions will be enacted. It ought to be followed by one/more catch blocks.  How try-catch… Read More »

52. Benefits of Exception handling

the benefits of exception handling are as follows,  (a) Exception handling can control run tune errors that occur in the program. (b) It can avoid abnormal termination of the program and also shows the behavior of program to users. (c) It can provide a facility to handle exceptions, throws message regarding exception and completes the execution of… Read More »

51. Friend class and function

Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other class. For example, a LinkedList class may be allowed to access private members of Node.  class Node { private: int… Read More »

50. Scope resolution operator(::)

The :: (scope resolution) operator is used to get hidden names due to variable scopes so that you can still use them. The scope resolution operator can be used as both unary and binary. In C++, scope resolution operator is ::. It is used for following purposes. 1) To access a global variable when there is… Read More »