Skip to main content

Posts

Showing posts from January, 2021

C++ Exception Handling

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 Classes In C++ standard exceptions are defined in <exception> class that we can use inside our programs. The arrangement of parent-child class hierarchy is shown below: All the exception classes in C++ are derived from std::exception class. Let's see the list of C++ common exception classes. Exception Description std::exception It is an exception and parent class of all standard C++ excep

C++ String

C++ Strings In C++, string is an object of  std::string  class that represents sequence of characters. We can perform many operations on strings such as concatenation, comparison, conversion etc. C++ String Example Let's see the simple example of C++ string. #include <iostream>    using   namespace  std;   int  main( ) {       string s1 =  "Hello" ;              char  ch[] = {  'C' ,  '+' ,  '+' };             string s2 = string(ch);             cout<<s1<<endl;             cout<<s2<<endl;     }   Output: Hello C++ C++ String Compare Example Let's see the simple example of string comparison using strcmp() function. #include <iostream>    #include <cstring>    using   namespace  std;   int  main ()   {      char  key[] =  "mango" ;      char  buffer[50];      do  {        cout<< "What is my favourite fruit? " ;        cin>>buffer;     }  while  (strcmp (key,buffer) != 0);