Category Archives: Library

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 »

47. Hierarchical Inheritance

Hierarchical inheritance is defined as the process of deriving more than one class from a base class. Syntax of Hierarchical inheritance: class A  {      // body of the class A.  }    class B : public A   {      // body of class B.  }  class C : public A  {      // body of class C.  }   class D : public A  {      // body of class D.  }    Let’s see a simple example: #include <iostream>  using namespace std;  class Shape                 // Declaration of base class.  {      public:      int a;      int b;      void get_data(int n,int m)      {          a= n;          b = m;      }  };  class Rectangle : public Shape  // inheriting Shape class  {      public:      int rect_area()      {          int result = a*b;          return result;      }  };  class Triangle : public Shape    // inheriting Shape class  {      public:      int triangle_area()      {          float result = 0.5*a*b;          return result;      }  };  int main()  {      Rectangle r;      Triangle t;      int length,breadth,base,height;      std::cout << “Enter the length and breadth of a rectangle: ” << std::endl;      cin>>length>>breadth;      r.get_data(length,breadth);      int m = r.rect_area();      std::cout << “Area of the rectangle is : ” <<m<< std::endl;      std::cout << “Enter the base and height of the triangle: ” << std::endl;      cin>>base>>height;      t.get_data(base,height);      float n = t.triangle_area();      std::cout <<“Area of the triangle is : ”  << n<<std::endl;      return 0;  }   Output: Enter the length and breadth of a rectangle: 23 20 Area of the rectangle is : 460 Enter the base and height of the triangle: 2… Read More »

49. Hybrid Inheritance

Hybrid inheritance is a combination of more than one type of inheritance. Let’s see a simple example: #include <iostream>  using namespace std;  class A  {      protected:      int a;      public:      void get_a()      {         std::cout << “Enter the value of ‘a’ : ” << std::endl;         cin>>a;      }  };    class B : public A   {      protected:      int b;      public:      void get_b()      {          std::cout << “Enter the value of ‘b’ : ” << std::endl;         cin>>b;      }  };  class C   {      protected:      int c;      public:      void get_c()      {          std::cout << “Enter the value of c is : ” << std::endl;          cin>>c;      }  };    class D : public B, public C  {      protected:      int d;      public:      void mul()      {           get_a();           get_b();           get_c();           std::cout << “Multiplication of a,b,c is : ” <<a*b*c<< std::endl;      }  };  int main()  {      D d;      d.mul();      return 0;  }   Output: Enter the value of ‘a’ : 10 Enter the value of ‘b’ : 20 Enter the value of c is : 30 Multiplication of a,b,c is : 6000

46. Multiple Inheritance

Multiple inheritance is the process of deriving a new class that inherits the attributes from two or more classes. Syntax of the Derived class: class D : visibility B-1, visibility B-2, ?  {     // Body of the class;  }    Let’s see a simple example of multiple inheritance. #include <iostream>  using namespace std;  class A  {      protected:       int a;      public:      void get_a(int n)      {          a = n;      }  };    class B  {      protected:      int b;      public:      void get_b(int n)      {          b = n;      }  };  class C : public A,public B  {     public:      void display()      {          std::cout << “The value of a is : ” <<a<< std::endl;          std::cout << “The value of b is : ” <<b<< std::endl;          cout<<“Addition of a and b is : “<<a+b;      }  };  int main()  {     C c;     c.get_a(10);     c.get_b(20);     c.display();      return 0;  }   Output: The value of a is : 10 The value of b is : 20 Addition of a and b is :… Read More »

48. Multilevel Inheritance

Multilevel inheritance is a process of deriving a class from another derived class. Multi Level Inheritance Example When one class inherits another class which is further inherited by another class, it is known as multi level inheritance in C++. Inheritance is transitive so the last derived class acquires all the members of all its base classes.… Read More »