Jumat, 08 Juli 2011

To perform exception handling with multiple catch.

ALGORITHM:
Step 1: Start the program.
Step 2: Declare and define the function test().
Step 3: Within the try block check whether the value is greater than zero or not.
a. if the value greater than zero throw the value and catch the corresponding exception.
b. Otherwise throw the character and catch the corresponding exception.
Step 4: Read the integer and character values for the function test().
Step 5: Stop the program.

PROGRAM:
01#include<iostream.h>
02#include<conio.h>
03void test(int x)
04{
05   try
06   {
07              if(x>0)
08                 throw x;
09        else
10                 throw 'x';
11   }
12
13   catch(int x)
14   {
15              cout<<"Catch a integer and that integer is:"<<x;
16   }
17
18   catch(char x)
19   {
20              cout<<"Catch a character and that character is:"<<x;
21   }
22}
23
24void main()
25{
26   clrscr();
27   cout<<"Testing multiple catches\n:";
28   test(10);
29   test(0);
30   getch();
31}
Output:
Testing multiple catches
Catch a integer and that integer is: 10
Catch a character and that character is: x

To find the mean value of a given number using friend function.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class name as Base with data members and member functions.
STEP 3: The function get() is used to read the 2 inputs from the user.
STEP 4: Declare the friend function mean(base ob) inside the class.
STEP 5: Outside the class to define the friend function and do the following.
STEP 6: Return the mean value (ob.val1+ob.val2)/2 as a float.
STEP 7: Stop the program.

PROGRAM:
01#include<iostream.h>
02#include<conio.h>
03class  base
04{
05    int val1,val2;
06   public:
07    void get()
08    {
09       cout<<"Enter two values:";
10       cin>>val1>>val2;
11    }
12    friend float mean(base ob);
13};
14float mean(base ob)
15{
16   return float(ob.val1+ob.val2)/2;
17}
18void main()
19{
20    clrscr();
21    base obj;
22    obj.get();
23    cout<<"\n Mean value is : "<<mean(obj);
24    getch();
25}  

Output:
Enter two values: 10, 20
Mean Value is: 15

To calculate the area of circle, rectangle and triangle using function overloading. July 8, 2011

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class name as fn with data members and member functions.
STEP 3: Read the choice from the user.
STEP 4: Choice=1 then go to the step 5.
STEP 5: The function area() to find area of circle with one integer argument.
STEP 6: Choice=2 then go to the step 7.
STEP 7: The function area() to find area of rectangle with two integer argument.
STEP 8: Choice=3 then go to the step 9.
STEP 9: The function area() to find area of triangle with three arguments, two as Integer and one as float.
STEP 10: Choice=4 then stop the program.

PROGRAM:
01#include<iostream.h>
02#include<stdlib.h>
03#include<conio.h>
04#define pi 3.14
05class fn
06{
07      public:
08        void area(int); //circle
09        void area(int,int); //rectangle
10        void area(float ,int,int);  //triangle
11};
12 
13void fn::area(int a)
14{
15      cout<<"Area of Circle:"<<pi*a*a;
16}
17void fn::area(int a,int b)
18{
19      cout<<"Area of rectangle:"<<a*b;
20}
21void fn::area(float t,int a,int b)
22{
23      cout<<"Area of triangle:"<<t*a*b;
24}
25 
26void main()
27{
28     int ch;
29     int a,b,r;
30     clrscr();
31     fn obj;
32     cout<<"\n\t\tFunction Overloading";
33     cout<<"\n1.Area of Circle\n2.Area of Rectangle\n3.Area of Triangle\n4.Exit\n:”;
34     cout<<”Enter your Choice:";
35     cin>>ch;
36 
37     switch(ch)
38     {
39              case 1:
40                cout<<"Enter Radious of the Circle:";
41                cin>>r;
42                obj.area(r);
43                break;
44              case 2:
45                cout<<"Enter Sides of the Rectangle:";
46                cin>>a>>b;
47                obj.area(a,b);
48                break;
49              case 3:
50                cout<<"Enter Sides of the Triangle:";
51                cin>>a>>b;
52                obj.area(0.5,a,b);
53                break;
54              case 4:
55                exit(0);
56     }
57getch();
58}      

Output:
Function Overloading
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 2
Enter the Sides of the Rectangle: 5 5
Area of Rectangle is: 25
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 4

To swap the numbers using the concept of function template

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the template class.
STEP 3: Declare and define the functions to swap the values.
STEP 4: Declare and define the functions to get the values.
STEP 5: Read the values and call the corresponding functions.
STEP6: Display the results.
STEP 7: Stop the program.
PROGRAM:
01#include<iostream.h>
02#include<conio.h>
03 
04template<class t>
05 
06void swap(t &x,t &y)
07{
08   t temp=x;
09   x=y;
10   y=temp;
11}
12 
13void fun(int a,int b,float c,float d)
14{
15   cout<<"\na and b before swaping :"<<a<<"\t"<<b;
16   swap(a,b);
17   cout<<"\na and b after swaping  :"<<a<<"\t"<<b;
18   cout<<"\n\nc and d before swaping :"<<c<<"\t"<<d;
19   swap(c,d);
20   cout<<"\nc and d after swaping  :"<<c<<"\t"<<d;
21}
22 
23void main()
24{
25    int a,b;
26    float c,d;
27    clrscr();
28    cout<<"Enter A,B values(integer):";
29    cin>>a>>b;
30    cout<<"Enter C,D values(float):";
31    cin>>c>>d;
32    fun(a,b,c,d);
33    getch();
34}

Output:
Enter A, B values (integer): 10 20
Enter C, D values (float): 2.50 10.80
A and B before swapping: 10 20
A and B after swapping: 20 10
C and D before swapping: 2.50 10.80
C and D after swapping: 10.80 2.50

To write a program to find the multiplication values and the cubic values using inline function. July 8, 2011

ALGORITHM:
Step 1: Start the pogram.
Step 2: Declare the class.
Step 3: Declare and define the inline function for multiplication and cube.
Step 4: Declare the class object and variables.
Step 5: Read two values.
Step 6: Call the multiplication and cubic functions using class objects.
Step 7: Return the values.
Step 8: Display.
Step 9: Stop the program.
PROGRAM:
01#include<iostream.h>
02#include<conio.h>
03 
04class line
05{
06   public:
07              inline float mul(float x,float y)
08              {
09                            return(x*y);
10              }
11              inline float cube(float x)
12              {
13                            return(x*x*x);
14              }
15};
16 
17void main()
18{
19              line obj;
20              float val1,val2;
21              clrscr();
22              cout<<"Enter two values:";
23              cin>>val1>>val2;
24              cout<<"\nMultiplication value is:"<<obj.mul(val1,val2);
25              cout<<"\n\nCube value is          :"<<obj.cube(val1)<<"\t"<<obj.cube(val2);
26              getch();
27}

Output:
Enter two values: 5 7
Multiplication Value is: 35
Cube Value is: 25 and 343

To find out the student details using multiple inheritance

ALGORITHM:
Step 1: Start the program.
Step 2: Declare the base class student.
Step 3: Declare and define the function get() to get the student details.
Step 4: Declare the other class sports.
Step 5: Declare and define the function getsm() to read the sports mark.
Step 6: Create the class statement derived from student and sports.
Step 7: Declare and define the function display() to find out the total and average.
Step 8: Declare the derived class object,call the functions get(),getsm() and display().
Step 9: Stop the program.
PROGRAM:

01#include<iostream.h>
02#include<conio.h>
03 
04class student
05{
06    protected:
07       int rno,m1,m2;
08    public:
09                void get()
10              {
11                            cout<<"Enter the Roll no :";
12                            cin>>rno;
13                            cout<<"Enter the two marks   :";
14                            cin>>m1>>m2;
15              }
16};
17class sports
18{
19    protected:
20       int sm;                   // sm = Sports mark
21    public:
22                void getsm()
23              {
24                 cout<<"\nEnter the sports mark :";
25                 cin>>sm;
26 
27              }
28};
29class statement:public student,public sports
30{
31    int tot,avg;
32    public:
33    void display()
34              {
35                 tot=(m1+m2+sm);
36                 avg=tot/3;
37                 cout<<"\n\n\tRoll No    : "<<rno<<"\n\tTotal      : "<<tot;
38               cout<<"\n\tAverage    : "<<avg;
39              }
40};
41void main()
42{
43   clrscr();
44   statement obj;
45   obj.get();
46   obj.getsm();
47   obj.display();
48   getch();
49}
Output:
Enter the Roll no: 100
Enter two marks
90
80
Enter the Sports Mark: 90
Roll No: 100
Total : 260
Average: 86.66

Program to convert lowercase to uppercase

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the variables.
STEP 3: Read the file name.
STEP 4: open the file to write the contents.
STEP 5: writing the file contents up to reach a particular condition.
STEP6: write the file contents as uppercase.
STEP7: open the file to read the contents.
STEP 8: Stop the program.
PROGRAM:

01#include<fstream.h>
02#include<stdio.h>
03#include<ctype.h>
04#include<string.h>
05#include<iostream.h>
06#include<conio.h>
07void main()
08{
09              char c,u;
10              char fname[10];
11              clrscr();
12              ofstream out;
13              cout<<"Enter File Name:";
14              cin>>fname;
15              out.open(fname);
16              cout<<"Enter the text(Enter # at end)\n";
17  //write contents to file
18              while((c=getchar())!='#')
19              {
20                            u=c-32;
21                            out<<u;
22              }
23              out.close();
24              ifstream in(fname);        //read the contents of file
25              cout<<"\n\n\t\tThe File contains\n\n";
26              while(in.eof()==0)
27              {
28                            in.get(c);
29                            cout<<c;
30              }
31              getch();
32 
33}

Output:
Enter File Name: two.txt
Enter contents to store in file (enter # at end)
oops programming
The File Contains
OOPS PROGRAMMING