0% found this document useful (0 votes)
84 views4 pages

Exception Handling in C++ Stack Class

The student submitted their assignment on object oriented programming which included modifying a Stack class to handle multiple exceptions and implementing an exception handling Distance class. For the Stack class, they added two custom exception classes and caught those exceptions in main when the stack was full or empty during push and pop operations. For the Distance class, they added a custom inches exception class and threw that exception if inches were greater than or equal to 22 during object initialization or user input. They caught the exception in main.

Uploaded by

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

Exception Handling in C++ Stack Class

The student submitted their assignment on object oriented programming which included modifying a Stack class to handle multiple exceptions and implementing an exception handling Distance class. For the Stack class, they added two custom exception classes and caught those exceptions in main when the stack was full or empty during push and pop operations. For the Distance class, they added a custom inches exception class and threw that exception if inches were greater than or equal to 22 during object initialization or user input. They caught the exception in main.

Uploaded by

Muhammed Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIVERSITY OF HARIPUR

Khyber Pakhtunkhwa, pakistan


2st Semester
Assignment # 04
SUBMITTED TO: MAM MISBAH SIKANDER
Year 2020

NAME ABEEHA ZAINAB


ROLL NO : S20-0154
SECTION ‘B’
SUBJECT OBJECT ORIENTED PROGRAMMING
DEPARTMENT INFORMATION TECHNOLOGY (IT)
Q1). We have studied Exception Handling in class lectures using Stack Class. Modify the
Stack class to handle the multiple exceptions. For guideness consult the book chapter
Templates and Exceptions.
CODE :
#include <iostream>
using namespace std;
const int Max =3;
class stack {
private:
int s [Max];
int top;
public:
class range{ };
class range1{ };
stack()
{
top = -1;
}
void push(int var)
{
if (top>= Max -1)
throw range();
s [++top] =var;
}
int pop()
{
if (top<0)
throw range();
return s[top--];
}
};
int main()
{
stack s;
try {
s.push(99);
s.push(88);
s.push(77);
cout<<"1st number is \t"<<s.pop()<<endl;
cout<<"2nd number is \t"<<s.pop()<<endl;
cout<<"3rd number is \t"<<s.pop()<<endl;
}
catch (stack::range)
{
cout<<"Exception or stack is full \n";
}
catch (stack::range1)
{
cout<<"sStack is Empty \n";
}
cout<<"Arriveal is hear ";
return 0;
}
Output

Q2). We have discuss the Distance class that may leads to exceptions while working its
objects what could be these exceptions and how could you Handle. Implement the Distance
class with Exception Handling.

CODE :
#include<iostream>
using namespace std ;
class Dist
{
private:
int feet;
float inches;
public:
class inches{ };
Dist()
{
feet =0;
inches=0.0;
}
Dist(int ft, float in)
{
if (in >=22.0)
throw inches;
feet = ft;
inches = in;
}
void getdist()
{
cout<<"\nenter feet = " ;cin>>feet ;
cout<<"\nenter inches = " ;cin>>inches;
if (inches >= 22.0)
throw inches;
}
void showdata()
{
cout<<"The feet are "<<feet;
cout<<"The inches are "<<inches;
}};
int main()
{
try {
Dist d1(80,44.9);
Dist d2;
d2.getdist();
cout<<"\n Distance 1 = ";d1.showdata();
cout<<"\n Distance 2 = ";d2.showdata();
}
catch(Dist::inches) //catch exceptions
{
cout << "\nInitialization error: ";
}
cout << endl;
return 0;
}

You might also like