Exception Handling with Multiple Catch Using C++ Programming
#include
#include
void test(int x)
{
try
{
if(x>0)
throw x;
else
throw 'x';
}
catch(int
x)
{
cout<<"Catch
a integer and that integer is:"<
}
catch(char x)
{
cout<<"Catch a character and that
character is:"<
}
}
void main()
{
clrscr();
cout<<"Testing multiple catches\n:";
test(10);
test(0);
getch();
}
Output
Testing multiple
catches
Catch a integer and
that integer is: 10
Catch a character and
that character is: x
Exception Handling Divide by zero Using C++ Programming
#include
#include
void main()
{
int
a,b,c;
float
d;
clrscr();
cout<<"Enter the value of a:";
cin>>a;
cout<<"Enter the value of b:";
cin>>b;
cout<<"Enter the value of c:";
cin>>c;
try
{
if((a-b)!=0)
{
d=c/(a-b);
cout<<"Result
is:"<
}
else
{
throw(a-b);
}
}
catch(int
i)
{
cout<<"Answer is infinite because a-b
is:"<
}
getch();
}
Output
Enter the value for a: 20
Enter the value for b: 20
Enter the value for c: 40
Answer is infinite because a-b is: 0
Copy Constructor Using C++ Programming
#include
#include
class copy
{
int var,fact;
public:
copy(int temp)
{
var = temp;
}
double calculate()
{
fact=1;
for(int i=1;i<=var;i++)
{
fact = fact * i;
}
return fact;
}
};
void main()
{
clrscr();
int
n;
cout<<"\n\tEnter the Number : ";
cin>>n;
copy obj(n);
copy cpy=obj;
cout<<"\n\t"<
cout<<"\n\t"<
getch();
}
Output
Enter the Number: 5
Factorial is: 120
Factorial is: 120
Constructor Using C++ Programming
#include
#include
class prime
{
int a,k,i;
public:
prime(int x)
{
a=x;
}
void calculate()
{
k=1;
{
for(i=2;i<=a/2;i++)
if(a%i==0)
{
k=0;
break;
}
else
{
k=1;
}
}
}
void show()
{
if(k==1)
cout<< “\n\tA is prime
Number. ";
else
cout<<"\n\tA is Not
prime.";
}
};
void main()
{
clrscr();
int
a;
cout<<"\n\tEnter the Number:";
cin>>a;
prime obj(a);
obj.calculate();
obj.show();
getch();
}
Output
Enter the number: 7
Given number is Prime
Number
Binary Operator Overloading Using C++ Programming
#include
#include
class complex
{
int a,b;
public:
void getvalue()
{
cout<<"Enter the value of Complex Numbers
a,b:";
cin>>a>>b;
}
complex operator+(complex ob)
{
complex t;
t.a=a+ob.a;
t.b=b+ob.b;
return(t);
}
complex operator-(complex ob)
{
complex t;
t.a=a-ob.a;
t.b=b-ob.b;
return(t);
}
void display()
{
}
};
void main()
{
clrscr();
complex obj1,obj2,result,result1;
obj1.getvalue();
obj2.getvalue();
result = obj1+obj2;
result1=obj1-obj2;
cout<<"Input
Values:\n";
obj1.display();
obj2.display();
cout<<"Result:";
result.display();
result1.display();
getch();
}
Output
Enter the value of
Complex Numbers a, b
4
5
Enter the value of
Complex Numbers a, b
2
2
Input Values
4 + 5i
2 + 2i
Result
6 + 7i
2 + 3i
No comments:
Post a Comment