Question no.
#include <iostream>
#include <stdexcept>
// For using runtime_error
using namespace std;
// User defined class for handling exception
// Class Exception publicly inherits
// the runtime_error class
class Exception : public runtime_error {
public:
// Defining constructor of class Exception
// that passes a string message to the runtime_error class
Exception()
: runtime_error("Math error: Attempted to divide by Zero\n")
{
}
};
// defining Division function
float Division(float num, float den)
{
// If denominator is Zero
// throw user defined exception of type Exception
if (den == 0)
throw Exception();
// otherwise return the result of division
return (num / den);
} // end Division
int main()
{
float numerator, denominator, result;
numerator = 12.5;
denominator = 0;
// try block calls the Division function
try {
result = Division(numerator, denominator);
// this will not print in this example
cout << "The quotient is " << result << endl;
}
// catch block catches exception if any
// of type Exception
catch (Exception& e) {
// prints that exception has occurred
// calls the what function using object of
// the user defined class called Exception
cout << "Exception occurred" << endl
<< [Link]();
}
} // end main
Question no.1
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000
class Stack {
int top;
public:
int a[MAX]; // Maximum size of Stack
Stack() { top = -1; }
bool push(int x);
int pop();
int peek();
bool isEmpty();
};
bool Stack::push(int x)
{
if (top >= (MAX - 1)) {
cout << "Stack Overflow";
return false;
}
else {
a[++top] = x;
cout << x << " pushed into stack\n";
return true;
}
}
int Stack::pop()
{
if (top < 0) {
cout << "Stack Underflow";
return 0;
}
else {
int x = a[top--];
return x;
}
}
int Stack::peek()
{
if (top < 0) {
cout << "Stack is Empty";
return 0;
}
else {
int x = a[top];
return x;
}
}
bool Stack::isEmpty()
{
return (top < 0);
}
// Driver program to test above functions
int main()
{
class Stack s;
[Link](10);
[Link](20);
[Link](30);
cout << [Link]() << " Popped from stack\n";
return 0;
}