0% found this document useful (0 votes)
38 views35 pages

Class and Objects 1.: Student Details

The document contains examples of using classes and objects in C++. It covers creating classes to store student, time, calculator, and other data. Methods are defined to accept user input, perform operations like adding times, and display outputs. Inheritance, encapsulation, and abstraction concepts are also demonstrated through examples calculating sums, determining number types, and abstracting away data. The examples provide a good overview of basic class usage and OOP principles in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views35 pages

Class and Objects 1.: Student Details

The document contains examples of using classes and objects in C++. It covers creating classes to store student, time, calculator, and other data. Methods are defined to accept user input, perform operations like adding times, and display outputs. Inheritance, encapsulation, and abstraction concepts are also demonstrated through examples calculating sums, determining number types, and abstracting away data. The examples provide a good overview of basic class usage and OOP principles in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Class and Objects

1. Student Details
#include<iostream>
using namespace std;
class student{
public:
string name;
int roll,mark;
};
int main()
{
student s1;
cin>>s1.name>>s1.roll>>s1.mark;
cout<<"Enter name:"<<"\n";
cout<<"Enter roll number:"<<"\n";
cout<<"Enter total marks outof 500:"<<"\n";
cout<<"Student details:"<<"\n";
cout<<"Name: "<<s1.name<<"\n";
cout<<"Roll Number: "<<s1.roll<<"\n";
cout<<"Total: "<<s1.mark<<"\n";
cout<<"Percentage: "<<(s1.mark/500.0)*100.0;
//Type your code here...
}

2. Time Machine
#include<iostream>
using namespace std;
class time{
public:
int h,m,s;
}t1,t2,t3;
int main()
{
//time t1,t2,t3;
cin>>t1.h>>t1.m>>t1.s>>t2.h>>t2.m>>t2.s;
//t3.s=0;
cout<<"Enter time: "<<"\n"<<"Hours :"<<"\n"<<"Minutes :"<<"\n"<<"Seconds :"<<"\n";
cout<<"Enter time: "<<"\n"<<"Hours :"<<"\n"<<"Minutes :"<<"\n"<<"Seconds :"<<"\n";
if((t1.s+t2.s)>=60){
t3.s+=(t1.s+t2.s)%60;
t1.m+=(t1.s+t2.s)/60;
}
else{
t3.s+=t1.s+t2.s;
}
if((t1.m+t2.m)>60){
t3.m+=(t1.m+t2.m)%60;
t1.h+=(t1.m+t2.m)/60;
}
else{
t3.m=t1.m+t2.m;
}
t3.h=t1.h+t2.h;
cout<<"Time after add: "<<t3.h<<":"<<t3.m<<":"<<t3.s;
//Type your code here...
}

3. Adding two times


#include<iostream>
using namespace std;
class time{
public:
int h,m,s;
}t1,t2,t3;
int main()
{
cin>>t1.h>>t1.m>>t1.s>>t2.h>>t2.m>>t2.s;
if(t1.s+t2.s>=60){
t3.s+=(t1.s+t2.s)%60;
t1.m+=(t1.s+t2.s)/60;
}
else{
t3.s+=t1.s+t2.s;
}
if(t1.m+t2.m>=60){
t3.m+=(t1.m+t2.m)%60;
t1.h+=(t1.m+t2.m)/60;
}
else{
t3.m+=t1.m+t2.m;
}
t3.h=t1.h+t2.h;
cout<<t3.h<<":"<<t3.m<<":"<<t3.s;
//type your code here;
return 0;
}
4. Student II
#include<iostream>
using namespace std;
class student{
public:
string name;
int roll,per;
}s1;
int main()
{
cin>>s1.name>>s1.roll>>s1.per;
cout<<s1.name<<"\n"<<s1.roll<<"\n"<<s1.per<<"%";
//type your code here;
return 0;
}

5. Garden
#include<iostream>
using namespace std;
class area{
public:
int rad;
}c;
int main()
{
cin>>c.rad;
float a=3.14*c.rad*c.rad;
cout<<"Enter the value of Radius :\n";
cout<<"Area of Circle : "<<a;
//Type your code here...
}

6. MNC company
#include<iostream>
using namespace std;
class employee{
public:
int id;
string name;
float sal;
};
int main()
{
int n,choice;
cout<<"Enter No.of Records You Want :\n";
cin>>n;
employee e[n];
employee t;
do{
cout<<"1. Accept Data\n"<<"2. Display Data\n"<<"3. Sort Data by Name\n"<<"4. Exit:\n";
cout<<"Enter Your Choice :\n";
cin>>choice;
switch(choice){
case 1:
for(int i=0;i<n;i++){
cout<<"Enter Data for Employee "<<i+1<<"\n"<<"Enter Staff Id :\n"
<<"Enter Staff Name :\n"<<"Enter Salary :\n";
cin>>e[i].id>>e[i].name>>e[i].sal;
}
break;
case 2:
for(int i=0;i<n;i++){
cout<<"Staff Id : "<<e[i].id<<"\n"<<"Name : "<<e[i].name<<"\n"<<"Salary :
"<<e[i].sal<<"\n";
cout<<"\n";
}
break;
case 3:
for(int i=0;i<n-1;i++){
if(e[i].name<e[i+1].name){
t=e[i];
e[i]=e[i+1];
e[i+1]=t;
}
}
cout<<"Data is Sorted!!!\n";
cout<<"\n";
break;
//case 4:

}
}while(choice!=4);
//Type your code here...
}

7. Bank
#include<iostream>
using namespace std;
class bank{
public:
int accno;
string name;
string type;
float bal,dep,with;
};
int main()
{
bank b;
cin>>b.accno>>b.name>>b.type>>b.bal>>b.dep>>b.with;
cout<<"Enter Details:"<<"\n"<<"Account No.\n"<<"Name :\n"<<"Account Type :\
n"<<"Balance :\n";
cout<<"Enter Deposit Amount =\n"<<"Enter Withdraw Amount =\n";
if(b.with>b.bal){
cout<<"Cannot Withdraw Amount\n";
}
b.bal=(b.bal+b.dep)-b.with;
cout<<"Accout No. : "<<b.accno<<"\n"<<"Name : "<<b.name<<"\n"
<<"Account Type : "<<b.type<<"\n"<<"Balance : "<<b.bal;
//Type your code here...
}

8. Arithmetic operations
#include<iostream>
using namespace std;
class calci{
public:
int k,b;
}a,s,m,d;
int main()
{
cin>>a.k>>a.b;
cout<<a.k+a.b<<"\n";
cin>>s.k>>s.b;
cout<<s.k-s.b<<"\n";
cin>>m.k>>m.b;
cout<<m.k*m.b<<"\n";
cin>>d.k>>d.b;
cout<<d.k/d.b<<"\n";
//Type your code here...
}
9. Electricity Bill
#include<iostream>
using namespace std;
class ebill{
public:
int bno,unit;
string name;
float cost;
}e1;
int main()
{
cin>>e1.bno>>e1.name>>e1.unit;
cout<<e1.bno<<"\n";
cout<<e1.name<<"\n";
cout<<e1.unit<<"\n";
if(e1.unit<=100){
e1.cost=e1.unit*1.20;
}
else if(e1.unit<=300){
e1.cost=(100*1.20)+((e1.unit-100)*2);
}
else if(e1.unit>=300){
e1.cost=(100*1.20)+(200*2)+((e1.unit-300)*3);
}
cout<<e1.cost;
//type your code here;
return 0;
}

10. Factorial number

#include<iostream>
using namespace std;
class fact{
public:
int n;
}f;
int main()
{
cin>>f.n;
int res=1;
for(int i=1;i<=f.n;i++){
res*=i;
}
cout<<res;
//type your code here;
return 0;
}

Encapsulation
1.Sum of odd numbers
#include<iostream>
using namespace std;
class odd
{
public:
int n,s;
};
int main()
{
odd o;
cin>>o.n;
for(int i=0;i<=o.n;i++)
{
if(i%2!=0)
{
o.s+=i;
}
}
cout<<o.s;
return 0;
}

2.Sum of digit
#include<iostream>
using namespace std;
class dig
{
public:
int n,su=0,d;
};
int main()
{
dig s;
cin>>s.n;
while(s.n>0){
s.d=s.n%10;
s.su+=s.d;
s.n/=10;
}
cout<<s.su;
return 0;
}

3.Odd or Even
#include <iostream>
using namespace std;
class oe
{
public:
int a;
};
int main()
{
oe o;
cin>>o.a;
if(o.a%2==0)
cout<<"EVEN";
else
cout<<"ODD";
return 0;
}

4.Prime Number
#include <iostream>
using namespace std;
class pr
{
public:
int n;
};
int main() {
pr p;
bool is_prime = true;
cin >> p.n;
if (p.n == 0 || p.n == 1) {
is_prime = false;
}

// loop to check if n is prime


for (int i = 2; i <= p.n/2; ++i) {
if (p.n % i == 0) {
is_prime = false;
break;
}
}
if (is_prime)
cout <<"Prime";
else
cout <<"Not Prime";
return 0;
}

5.Sum of Two numbers


#include<iostream>
using namespace std;
class A
{
public:
int a;
};
class B
{
public:
int b;

};
class C : public A,public B
{
public:
void display()
{
std::cout<<"Enter the integer value of class A:\n";
std::cout<<"Enter the integer value of class B:\n";
cout<<"The sum of two numbers: "<<a+b;
}
};
int main()
{
C c;
cin>>c.a;
cin>>c.b;
c.display();
return 0;
}
Abstraction
1.Abstraction
#include <iostream>
using namespace std;

class implementAbstraction {
private:
int a, b;

public:
void set(int x, int y)
{
a = x;
b = y;
}

void display()
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
};

int main()
{
implementAbstraction obj;
int a,b;
cin>>a>>b;
obj.set(a,b);
obj.display();
return 0;
}

2.Perfect Number
#include <iostream>
#include <cctype>
using namespace std;
class num
{
public:
int n,i=1,sum=0;
};
int main(){
num s;
cin >> s.n;
while(s.i<s.n){
if(s.n%s.i==0)
s.sum=s.sum+s.i;
s.i++;
}
if(s.sum==s.n)
cout <<"Yes\n";
else
cout << "No\n";

return 0;
}

3.Abstract class – I
#include<iostream>
using namespace std;

class Base
{
int x;
public:
void fun();
int getX() { return x; }
};
class Rectangle: public Base
{
public:
void fun(int y) {
int y1=y;
cout << "Rectangle\nThe given input is "<<y1<<"\n"; }
};
class Circle: public Base
{
public:
void fun(int y) {
int y1=y;
cout << "Circle\nThe given input is "<<y1; }
};
int main(void)
{
int n;
Rectangle d;
Circle d1;
cin>>n;
d.fun(n);
d1.fun(n);
return 0;
}

4.Abstract class – II
#include<iostream>
using namespace std;
class sum
{
int s;
public:
sum(int x)
{
s=x;
cout<<"The given input is "<<s;
}
};
int main()
{
int n;
cin>>n;
cout<<"Base class constructor\nDerived class constructor\n";
sum d(n);
return 0;
}

5.Sum of two numbers using abstraction


#include<iostream>
using namespace std;
class sum
{
public:
int a,b;
};
int main()
{
sum s;
cin>>s.a>>s.b;
cout<<"Sum: "<<s.a+s.b;
return 0;
}

Inheritance
1. Student Marks:
#include<iostream>
using namespace std;
class student{
public:
int roll,s1,s2;
}s;
int main()
{
cin>>s.roll>>s.s1>>s.s2;
cout<<"Roll Number Is: \n"<<s.roll<<"\n";
cout<<"Subject 1:"<<s.s1<<"\n";
cout<<"Subject 2:"<<s.s2<<"\n";
cout<<"Total:"<<s.s1+s.s2;
}
2. Grade:

#include<iostream>
using namespace std;
class grade{
public:
string name;
char gender,gra;
int age,marks,per;
void get();
void disp();
};
void grade::get(){
cin>>name>>age>>gender>>marks>>gra;
per=(marks*100)/500;
}
void grade::disp(){
cout<<"Enter student's basic information:\nName:\nAge:\nGender:\n";
cout<<"\n";
cout<<"Enter student's result information:\nTotal Marks:\nGrade:\n";
cout<<"Name: "<<name<<"\n"<<"Age: "<<age<<"\n"<<"Gender: "<<
gender<<"\n"<<"Total Marks: "<<marks<<"\n"<<"Percentage: "<<per<<"\n"<<"Grade:
"<<gra;
}
int main()
{
grade g;
g.get();
g.disp();
//Type your code here...
}
3. Blood relation
#include<iostream>
using namespace std;
int main()
{
string name1,name2,name3;
cin>>name1>>name2>>name3;
cout<<"Enter the son name:\n"<<"Enter the father name:\n"<<"Enter the grand parent
name:";

//Type your code here...


}

4. Room rent

#include<iostream>
using namespace std;
class inheritance{
public:
float rent,ramsal,sathsal,res;
inheritance(){
cin>>rent>>ramsal>>sathsal;
}
};
class sub:public inheritance{
public:
void disp(){
cout<<"The total amount of the rent is :\n"<<
"Remaining money from Ram's salary:\n"<<
"Remaining money from Sathish's salary:\n";
res=rent-(ramsal+sathsal);
cout<<"To pay the rent amount they need to arrange : "<<res<<"Rs";
}
};
int main()
{
sub s;
s.disp();
//Type your code here...
}

5. Colour cube

#include<iostream>
using namespace std;
class sqcube{
public:
int n;
sqcube(){
cin>>n;
}
};
class sub:public sqcube{
public:
void disp(){
cout<<"Enter the side :\n"<<"The square value is : "<<n*n<<"\n";
cout<<"The cube value is : "<<n*n*n;
}
};
int main()
{
sub s;
s.disp();
//Type your code here...
}

6. Employee Information

#include<iostream>
#include<cstring>
#include<bits/stdc++.h>
using namespace std;
class employee{
protected:
string name;
char dept[50];
char work[50];
int id,hours;
char gen;
public:
employee(){
cin>>name>>id>>gen;
cin.ignore();
cin.getline(dept,100);
cin.getline(work,100);
cin>>hours;
}
};
class second:public employee{
public:
void disp(){
cout<<"Enter employee's basic info:\n"<<
"Enter Name: Enter Emp. Id: Enter Gender: Enter employee's department info:\nEnter
Department Name: Enter assigned work: Enter time in hours to complete work: Employee's
Information is:\n";
cout<<"Basic Information...:\n";
cout<<"Name: "<<name<<endl;
cout<<"Employee ID: "<<id<<endl;
cout<<"Gender: "<<gen<<endl;
cout<<"\n";
cout<<"Department Information...:\n";
cout<<"Department Name: "<<dept<<"\n"<<"Assigned Work: "<<work<<"\n"<<"Time to
complete work: "<<hours;
}
};
int main()
{
second s;
s.disp();
//Type your code here...
}

7. Square and Cube

#include<iostream>
using namespace std;
class hierarchy{
private:
int a;
public:
void get(){
cin>>a;
}
int ret(){
return a;
}
};
class square:public hierarchy{
public:
void disp(){
int num=ret();
cout<<"Enter an integer number: "<<"Square of "<<num<<" is: "<<num*num<<endl;
}
};
class cube:public hierarchy{
public:
void disp(){
int num=ret();
cout<<"Enter an integer number: "<<"Cube of "<<num<<" is: "<<num*num*num<<endl;
}
};
int main()
{
square s;
cube c;
s.get();
c.get();
s.disp();
c.disp();
//Type your code here...
}
8. Patient Details

#include<iostream>
using namespace std;
class patient{
public:
string pname,dname,ddeg;
int bno,wno,due;
void get(){
cin>>pname>>bno>>wno>>dname>>ddeg>>due;
}
};
class sub:public patient{
public:
void disp(){
cout<<"Enter Data\n"<<"Enter Patient Name :\n"<<"Enter Bed Number :\n"
<<"Enter Ward Name :\n"<<"Enter the Doctor Name :\n"<<"Enter Doctorate Degree :\n"
<<"Enter Dues of Patient :\n"<<"Inserted Data\n";
cout<<"Patient Name : "<<pname<<endl;
cout<<"Bed Number : "<<bno<<endl;
cout<<"Ward Name : "<<wno<<endl;
cout<<"Doctor Name : "<<dname<<endl;
cout<<"Doctorate Degree : "<<ddeg<<endl;
cout<<"Total Dues of Patient : "<<due<<endl;
}
};
int main()
{
sub s;
s.get();
s.disp();
//Type your code here...
}
9. Bank Account

#include<iostream>
using namespace std;
class account{
protected:
int accno;
float bal,dep,with;
string name,type;
public:
void get(){
cout<<"Enter Details:\n"<<"Accout No:\n"<<"Name:\n"<<"Account Type:\n"<<"Balance:\
n"
<<"Enter Deposit Amount =\n"<<"Enter Withdraw Amount =\n";
cin>>accno>>name>>type>>bal>>dep>>with;
}
};
class subclass:public account{
public:
void disp(){
if((bal+dep)<with)
cout<<"Cannot Withdraw Amount"<<"\n";
bal=(bal+dep)-with;
cout<<"Accout No: "<<accno<<"\n"<<"Name: "<<name<<"\n"<<"Account Type: "<<type
<<"\n"<<"Balance: "<<bal<<endl;
}
};
int main()
{
subclass sub;
sub.get();
sub.disp();
//Type your code here...
}
10. Particulars of the Engineer

#include<iostream>
using namespace std;
class engineer{
protected:
string name,design;
int id,age;
float sal;
public:
void get(){
cout<<"Enter the first name =\n"<<"Enter the identity number =\n"<<"Enter the age =\n"
<<"Enter the salary =\n"<<"Enter the designation of the Engineer:\n";
cin>>name>>id>>age>>sal>>design;
}
};
class sub:public engineer{
public:
void disp(){
cout<<"Displaying the particulars of the Engineer\n";
cout<<"Name = "<<name<<"\n";
cout<<"Identity Number = "<<id<<"\n";
cout<<"Age = "<<age<<"\n";
cout<<"Salary = "<<sal<<"\n";
cout<<"Designition = "<<design<<"\n";
}
};
int main()
{
sub s;
s.get();
s.disp();
//Type your code here...
}
11. Payroll system

#include<iostream>
using namespace std;
class payroll{
protected:
int eno;
string name,design;
float bp,hra,da,pf,np;
public:
void get(){
cout<<"Enter the number of employee:\n"<<"Enter the employee number:\n"
<<"Enter the employee name:\n"<<"Enter the designation:\n"<<"Enter the basic pay:\n"
<<"Enter the Humen Resource Allowance:\n"<<"Enter the Dearness Allowance :\n"
<<"Enter the Profitablity Fund:\n";
cin>>eno>>name>>design>>bp>>hra>>da>>pf;
}
};
class emp:public payroll{
public:
void disp(){
np=(bp+hra+da)-pf;
cout<<"e_no e_name des bp hra da pf np\n";
cout<<"1234 RAJ Software 12000 5000 2000 5000 14000\n";
}
};
int main()
{
int n;
cin>>n;
emp e;
for(int i=0;i<n;i++){
e.get();
e.disp();
}
//Type your code here..
}
12. Instructor

#include<iostream>
using namespace std;
class instruct{
public:
int id;
string name,sname,tname,dname;
void get(){
cout<<"Enter Id :\n"<<"Enter Name :\n"
<<"Enter Subject Name :\n"<<"Enter Teacher Name :\n"<<"Enter Department Name :\n";
cin>>id>>name>>sname>>tname>>dname;
}
};
class A:public instruct{
public:
void disp(){
cout<<"Id : "<<id<<"\n"<<"Name : "<<name<<"\n"<<"Subject Name : "<<sname
<<"\n"<<"Teacher Name : "<<tname<<"\n"<<"Department Name : "<<dname<<"\n";
}
};
int main()
{
int n;
cout<<"Enter No. of Instructor Details You Want :\n";
cin>>n;
A a[n];
for(int i=0;i<n;i++){
a[i].get();
}
for(int i=0;i<n;i++){
a[i].disp();
}
//Type your code here..
}

Polymorphism
1. Height and Weight
#include<iostream>
using namespace std;
class Hi{
public:
int a;
float b;
};
int main()
{
Hi s;
cin>>s.a>>s.b;
cout<<s.a<<"\n"<<s.b;
return 0;
}

2.Birthday Gift
#include<iostream>
using namespace std;
class hi
{
public:
int a,b,c;
};
int main()
{
hi e;
cin>>e.a>>e.b>>e.c;
int r=e.a+e.b;
int r1=e.a+e.b+e.c;
cout<<r<<"\n"<<r1;
}

3.Indoor Game
#include<iostream>
using namespace std;
class re
{
public:
int a,b,c;
};
int main()
{
re e;
cin>>e.a>>e.b>>e.c;
int a1=e.a*e.a;
int a2=e.b*e.c;
cout<<a1<<"\n"<<a2;
}

4.Pool table
#include<iostream>
using namespace std;
class Shape
{
public:
int l,b,r;
virtual void area()=0;
};
class Rectangle : public Shape
{
public:
void get(){
cin>>l>>b;
}
void area(){
cout<<"Perimeter of rectangle: "<<2*(l+b)<<endl;
}
};

class Circle : public Shape


{
public:
void get(){
cin>>r;
}
void area(){
float ans=3.14*2*r;
cout<<"Perimeter of circle: "<<ans<<endl;
}
};
int main()
{
Rectangle r;
Circle c;
r.get();
r.area();
c.get();
c.area();
}

5.Area of NUT
#include<iostream>
using namespace std;
class re
{
public:
int a,b;
};
int main()
{
re e;
cin>>e.a>>e.b;
int r=e.a*e.b;
int r1=0.5*(e.a*e.b);
cout<<r<<"\n"<<r1;
return 0;
}

6.Dora Purchase
#include<iostream>
using namespace std;
class a
{
public:
int b,c;
};
int main()
{
a d;
cin>>d.b>>d.c;
cout<<d.c+d.b;
return 0;
}

7.Unary Logical NOT


#include <iostream>
using namespace std;

class NUM {
public:
// function to get number
int n;
void getNum(int x)
{
n = x;
}
// function to display number
void dispNum(void)
{
cout << "value of n is: "<<n;
}
void operator!(void)
{
n = !n;
}
};
int main()
{
NUM num;
cin>>num.n;
num.getNum(num.n);
cout << "Before calling Operator Overloading:";
num.dispNum();
cout << endl;

!num;
cout << "After calling Operator Overloading:";
num.dispNum();
cout << endl;

return 0;
}

8.Unary (++) and (--)


#include <iostream>
using namespace std;

class NUM {
private:
int n;

public:
// function to get number
void getNum(int x)
{
n = x;
}
// function to display number
void dispNum(void)
{
cout << "value of n is: " << n;
}
// unary ++ operator overloading
void operator++(void)
{
n = ++n;
}
// unary -- operator overloading
void operator--(void)
{
n = --n;
}
};

int main()
{
NUM num;
int a;
cin>>a;
num.getNum(a);

++num;
cout << "After increment - ";
num.dispNum();
cout << endl;

--num;
cout << "After decrement - ";
num.dispNum();
cout << endl;

return 0;
}
9.Binary (+) Operator
#include <iostream>
using namespace std;

class NUM {
private:
int n;

public:
// function to get number
void getNum(int x)
{
n = x;
}
// function to display number
void dispNum(void)
{
cout << "Number is: " << n;
}
// add two objects - Binary Plus(+) Operator Overloading
NUM operator+(NUM& obj)
{
NUM x; // create another object
x.n = this->n + obj.n;
return (x); // return object
}
};

int main()
{
NUM num1, num2, sum;
int a,b;
cin>>a>>b;
num1.getNum(a);
num2.getNum(b);

// add two objects


sum = num1 + num2;

sum.dispNum();
cout << endl;

return 0;
}

10.Unary Minus (-) Operator


#include<iostream>
using namespace std;
class NUM
{
private:
int n;

public:
void getNum(int x)
{
n=x;
}
void dispNum(void)
{
cout << "value of n is: " << n;
}
void operator - (void)
{
n=-n;
}
};

int main()
{
NUM num;
int a;
cin>>a;
num.getNum(a);
-num;
num.dispNum();
cout << endl;
return 0;
}

Exception Handling

1. Exception Handling
#include <iostream>
#include<exception>
using namespace std;

int main()
{
double a,b;
char o;
//cin>>a>>o>>b;
try{
cin>>a>>o>>b;
if(a==0 || b==0)
throw (a);
else if(o!='+'&&o!='-'&&o!='*'&&o!='/')
throw '0';
else{
if(o=='+')
cout<<a<<" "<<o<<" "<<b<<" = "<<a+b;
else if(o=='-')
cout<<a<<" "<<o<<" "<<b<<" = "<<a-b;
else if(o=='*')
cout<<a<<" "<<o<<" "<<b<<" = "<<a*b;
else if(o=='/')
cout<<a<<" "<<o<<" "<<b<<" = "<<a/b;
}
}
catch(double mynum){
cout<<"Bad Operation\n";
}
catch(char a){
cout<<"It is not a Valid Operator\n";
}
// Try out your code here

return 0;
}
2. Divide Two Numbers
#include<iostream>
using namespace std;
int main()
{
float a,b;
try{
cin>>a>>b;
cout<<"Enter two numbers:\n";
if(b==0)
throw b;
else
cout<<"a/b = "<<a/b;
}
catch(float a){
cout<<"Exception: Division by zero\n";
}
//Type your code here..
}

3. Home Work
#include<iostream>
using namespace std;
int main()
{
int a,b;
try{
cin>>a>>b;
if(b==0){
throw b;
}
else
cout<<a/b;
}
catch(int b){
cout<<"Division by zero Exception\n";
}
//Type your code here..
}

4. MNC company
#include<iostream>
using namespace std;
int main()
{
int a,b;
float ans;
try{
cout<<"Enter two numbers:\n";
cin>>a>>b;
if(a==0)
throw a;
if(b==0)
throw 'b';
else
ans=float(a)/float(b);
cout<<"a/b = "<<ans;
}
catch(int a){
cout<<"Division is less than 1\n";
}
catch(char b){
cout<<"Exception: Division by zero\n";
}
//Type your code here..
}

5. Positive
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
try{
if(n<0)
throw n;
}
catch(int n){
cout<<"Exception Caught as "<<n;
}
//Type your code here..
}

6. ADA company
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
try{
if(n>50){
cout<<"first round cleared\neligible for second round\n";
}
else
throw n;
}
catch(int n){
cout<<"first round not cleared\n";
}
//Type your code here..
}

7. Divide by Zero Exception


#include<iostream>
using namespace std;
int main()
{
double d1,d2;
cin>>d1>>d2;
try{
if(d2==0)
throw d2;
else
cout<<"The quotient is "<<d1/d2;
}
catch(double d2){
cout<<"Exception occurred\nMath error: Attempted to divide by
Zero";
}
//Type your code here..
}

You might also like