You are on page 1of 21

[OBJECT ORIENTED

PROGRAMING]
[20F-0114_BCS-2C]
Question no.1
#include<iostream>
using namespace std;
int divide(int x, int y) {
int result = 0;
try
{
if (y==0)
{
throw y;
}
else
{
result = x / y;
cout << "The result of division is : " ;
return result;
}
}
catch (int y)
{
cout << "Can't divide by zero" << endl;
}
cout << endl;
}
float divide(float x, float y) {
int result = 0;
try
{
if (y == 0)
{
throw y;
}
else
{
result = x / y;
cout << "The result of division is : ";
return result;
}
}
catch (float y)
{
cout << "Can't divide by zero" << endl;
}
cout << endl;
}
string divide(string x, string y) {
int result = 0;
try
{
if (y == "0")
{
throw "abc";
}
else
{
return "Denominrator should string\n";
}
}
catch (string s1)
{
cout << "Can't divide by null" << endl;
}
cout << endl;
}
char divide(char x, char y) {
char result = 'char';
try
{
if (y == '0')
{
throw "abc";
}
else
{
return result;
}
}
catch (char c)
{
cout << "Can't divide by null" << endl;
}
cout << endl;
}
int main() {
int a, b;
float c, d;
string s,s1;
char v,v2;
cout << "Enter the numinator in integer : ";
cin >> a;
cout << "Enter denomirator in integer : ";
cin >> b;
cout << divide(a, b) << endl;
cout << "Enter the numinator in float : ";
cin >> c;
cout << "Enter denomirator in float : ";
cin >> d;
cout << divide(c, d) << endl;
cout << "Enter the string :";
cin >> s;
cout << "Enter the string :";
cin >> s1;

cout << divide(s, s1) << endl;


cout << "Enter the char :";
cin >> v;
cout << "Enter the char :";
cin >> v2;
cout << divide(v, v2) << endl;
}
Question no.2
#include<iostream>
#include<iomanip>
#include<ctime>
using namespace std;

int add(int a, int b, int c, int d) {


try {
if (b == 0 && d == 0)
throw b, d;
return((a / b) + (c / d));
}
catch (int ) {
cout << "Denomiater must be a non zero element\n";
}

}
int subtract(int a, int b, int c, int d) {
try {
if (b == 0 && d == 0)
throw b, d;
return((a / b) - (c / d));
}
catch (int ) {
cout << "Denomiater must be a non zero element\n";
}
}
int multiply(int a, int b, int c, int d) {
try {
if (b == 0 && d == 0)
throw b, d;
return((a / b) * (c / d));
}
catch (int ) {
cout << "Denomiater must be a non zero element\n";
}
}
int divide(int a, int b, int c, int d) {
try {
if (b == 0 && d == 0)
throw b, d;
return((a / b) / (c / d));
}
catch (int ) {
cout << "Denomiater must be a non zero element\n";
}
}
void main() {
int a, b, c, d;
cout << "Enter first numinator : ";
cin >> a;
cout << "Enter first denominator : ";
cin >> b;
cout << "Enter second numinator : ";
cin >> c;
cout << "Enter second denominator : ";
cin >> d;
char choice;
cout << "Which operator you want to operat :" << endl;
cout << "Press + for addition" << endl;
cout << "Press - for subtraction" << endl;
cout << "Press * for multiplication" << endl;
cout << "Press / for division" << endl;
cin >> choice;

switch (choice) {
case '+':
cout<<"Addition of two rational numbers is :"<< add(a, b, c, d);
break;

case '-':
cout<<"subtraction of two rational numbers is : "<<subtract(a, b, c, d);
break;
case '/':
cout<<"division of two rational numbers is : "<<divide(a, b, c, d);
break;
case'*' :
cout<<"multiplication of two rational numbers is : "<<multiply(a, b, c, d);
break;

}
cout << endl;
system("pause");

Question no.3
(A)
#include<iostream>
using namespace std;
int main() {
int lowerLimit=50;
try
{
cout << "Entering the try block." << endl;
if (lowerLimit < 100)
throw exception("Lower limit violation.");
cout << "Exiting the try block." << endl;
}
catch (exception eObj)
{
cout << "Exception: " << eObj.what() << endl;
}
cout << "After the catch block" << endl;
}

(B)
#include<iostream>
using namespace std;
int main() {
int lowerLimit=150;
try
{
cout << "Entering the try block." << endl;
if (lowerLimit < 100)
throw exception("Lower limit violation.");
cout << "Exiting the try block." << endl;
}
catch (exception eObj)
{
cout << "Exception: " << eObj.what() << endl;
}
cout << "After the catch block" << endl;
}

Question no.4
#include<iostream>
#include<iomanip>
using namespace std;
int main() {
int len, len1;
float ft, ft1;
cout << "Enter the lengths in feets : ";
cin >> len;
cout << "Enter the length in inches :";
cin >> len1;

try {

cout << "Try Block " << endl;


if (len < 0 || len1 < 0)
{
throw (len, len1);
}
}
catch (int x) {
cout << "catch block" << endl;

}
ft = len * 30.48;
ft1 = len1 * 2.54;
float total = 0;
cout << "length of feet in centimeters : " << ft << endl;
cout << "length of inches in centimeters : " << ft1 << endl;
total = ft1 + ft;
cout << "Total length in centimeters " << total << endl;
system("pause");
}

Question no.5
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class invalidHr : public exception

//Implement the member function

public:

virtual const char* what() const throw()

//return the message

return "The value of hr must be between 0 and 12.";

};
class invalidMin : public exception
{

//Implement the member function

public:

virtual const char* what() const throw()

//return the message

return "The value of minutes must be between 0 and 59.";

};
class invalidSec : public exception

//Implement the member function

public:

virtual const char* what() const throw()

//return the message

return "The value of seconds must be between 0 and 59.";

};
int getHours();

int getMinutes();
int getSeconds();

void print24HourTime(int hr, int min, int sec, string str);


int main()

//Declare variables

int hours;

int minutes;

int seconds;

string str;

//call methods to get the hours,minutes and seconds

hours = getHours();

minutes = getMinutes();

seconds = getSeconds();

//Prompt and read AM or PM

cout << "Enter AM or PM: ";

cin >> str;

cout << endl;

//Print the output line

cout << "24 hour clock time: ";

//call the methods to print the time

print24HourTime(hours, minutes, seconds, str);


system("pause");

return 0;

//Implement the method hetHours()

int getHours()

//declare bool flag

bool done = false;

//declare hours

int hr = 0;

do

try

cout << "Enter hours: ";

cin >> hr;

cout << endl;

if (hr < 0 || hr > 12)

throw invalidHr();

done = true;
}

catch (invalidHr hrObj)

cout << hrObj.what() << endl;

} while (!done);

//return hours

return hr;

int getMinutes()

bool done = false;

int min = 0;

do

try

cout << "Enter minutes: ";


cin >> min;

cout << endl;

if (min < 0 || min > 59)

throw invalidMin();

done = true;

catch (invalidMin minObj)

cout << minObj.what() << endl;

} while (!done);

return min;

int getSeconds()

bool done = false;

int sec = 0;

do

try
{

cout << "Enter seconds: ";

cin >> sec;

cout << endl;

if (sec < 0 || sec > 59)

throw invalidSec();

done = true;

catch (invalidSec secObj)

cout << secObj.what() << endl;

} while (!done);

return sec;

//Implement the methods to print the hours minutes and seconds

void print24HourTime(int hr, int min, int sec, string str)

if (str == "AM")

{
if (hr == 12)

cout << 0;

else

cout << hr;

cout << ":" << min << ":" << sec << endl;

else if (str == "PM")

if (hr == 12)

cout << hr;

else

cout << hr + 12;

cout << ":" << min << ":" << sec << endl;

}
}
Question no.6
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class invalidDay {
string msg;
public:

invalidDay() {

msg = "Day input is wrong";


}

void showException()

cout << msg << endl;

};

class invalidMonth

string msg;

public:

invalidMonth() //constructor.

msg = "Month input is wrong";


}

void showException() //define function.

cout << msg << endl;

};

class leapYear //define class

string msg; //define variable as private.

public:

leapYear() //define constructor

msg = "year input is wrong";

void showException() //define function.

cout << msg << endl;

};

void read_date(int& day, int& month, int& year);


void read_date(int& d, int& m, int& y) //defination of function.

{
cout << "Enter day:";
cin >> d;
if (d <= 0 || d > 31)
throw invalidDay(); //Exception.
cout << "Enter month:";
cin >> m;
if (m <= 0 || m >= 13)
throw invalidMonth();
cout << "Enter year: ";
cin >> y;
if (y % 400 == 0 || (y != 100 && y % 4 == 0))
if (d >= 30)
throw leapYear();
}

int main()
{

int day, month, year;

string months[12] = {
"January","February","March","April","May","June","July","August","September",
"October","November","December" };

try {

read_date(day, month, year);

cout << "Date of Birth " << months[month - 1] << " " << day << "," << year;

catch (invalidDay d)
{

d.showException();

catch (invalidMonth m) {

m.showException();
}

catch (leapYear l) {

l.showException();
}
cout << endl;
system("pause");
return 0;

You might also like