Professional Documents
Culture Documents
University of Haripur: Khyber Pakhtunkhwa, Pakistan
University of Haripur: Khyber Pakhtunkhwa, Pakistan
University of Haripur: Khyber Pakhtunkhwa, Pakistan
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;
}