University of Haripur: Khyber Pakhtunkhwa, Pakistan

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

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