Search This Blog

Sunday, 1 May 2016

Write a program to merge content of two files to third file.

Practical - 36

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    ifstream i1, i2;
    ofstream o;
    char ch;
    i1.open("test1.txt");
    i2.open("test2.txt");
    if(i1==NULL || i2==NULL)
    {
        cout<<"error\n";
    }
    o.open("test3.txt");
    if(!o)
    {
        cout<<"error in file";
    }
    while(i1.eof()==0)
    {
        i1>>ch;
        o<<ch;
    }
    while(i2.eof()==0)
    {
        i2>>ch;
        o<<ch;
    }
    cout<<"The two files were merged into test3.txt";
    i1.close();
    i2.close();
    o.close();
return 0;
}

Write program to illustrate different seek() and tell() options

seekg(), tellg(), seekp(), tellp()

 

 

Write a simple Program for Exception Handling to raise Divide by zero exception.

Practcal - 38


#include <iostream>
using namespace std;

double division(int a, int b)
{
   if( b == 0 )
   {
      throw "Division by zero condition!";
   }
   return (a/b);
}

int main ()
{
   int x = 50;
   int y = 0;
   double z = 0;

   try {
     z = division(x, y);
     cout << z << endl;
   }catch (const char* msg) {
     cerr << msg << endl;
   }

   return 0;
}

Write a simple Program for Exception Handling with Multiple Catch blocks and also generic handler.

 Practical - 39


#include<iostream>
using namespace std;

void add(int x)
{
    try{
    if(x==0)
    {
        throw 'x';
    }
    else{
        if(x==1)
        throw x;
    else
        throw 5.0;
    }}
    catch(char c)
    {
        cout<<"\nChar exception";
    }
    catch(int i)
    {
        cout<<"\nint exception";
    }
    catch(double z)
    {
        cout<<"\ndouble exception";
    }
    /*
    remove all catch above and un comment this
    catch(...)
    {
        cout<<"\nFound exception generic:";
    }
    */
}

int main()
{
        add(0);
        add(1);
        add(5.5);
    return 0;
}

Write a C++ Program to solve quadratic equation. Raise exception when roots are not real.

Practical - 40

#include <iostream>
#include <cmath>
using namespace std;

int main() {

    float a, b, c, x1, x2, d;
    cout << "Enter coefficients a, b and c: ";
    cin >> a >> b >> c;

    try{
    d = b*b - 4*a*c;

    if (d > 0) {
        x1 = (-b + sqrt(d)) / (2*a);
        x2 = (-b - sqrt(d)) / (2*a);
        cout << "Roots are real and different." << endl;
        cout << "x1 = " << x1 << endl;
        cout << "x2 = " << x2 << endl;
    }

    else if (d == 0) {
        cout << "Roots are real and same." << endl;
        x1 = (-b + sqrt(d)) / (2*a);
        cout << "x1 = x2 =" << x1 << endl;
    }

    else {
            throw "\nRoots are not real";
    }
    }catch(char const *c)
    {
        cout<<c;
    }

    return 0;
}

Output:
Enter co -eff : 3
-4
10

roots are not real

Write a C++ Program that raised object exception. Create student class with enrollment no, CPI, namedata members. Throw exception of typestudent objectfor CPI less than 4. Catch exception and print details of corresponding Student.

Practical - 41


#include<iostream>

using namespace std;

class student{

    int eno,cpi;
    char name[10];

public:
    void getdata()
    {
        cout<<"\nEnter enrollment no:";
        cin>>eno;

        cout<<"\nEnter name:";
        cin>>name;

        c:
        try{
        cout<<"\nEnter Cpi:";
        cin>>cpi;
        if(cpi<4)
        {
            throw cpi;

        }
        }catch(int i)
        {
            cout<<"\nCpi "<<i<<" not valid";
            goto c;
        }
    }
    void display()
    {
        cout<<"\n"<<eno<<"\t"<<name<<"\t"<<cpi<<endl;
    }
};

int main()
{
    student s[10];
    int n;
    cout<<"\nEnter no of student:";
    cin>>n;
    for(int i=0;i<n;i++)
    {
        s[i].getdata();
    }
    cout<<"\nEno\tName\tCpi";
    for(int i=0;i<n;i++)
    {
        s[i].display();
    }
return 0;
}

type conversion friend function


Practical - 23





#include<iostream>
using namespace std;
class T12
{
      int h,m;
      char c;
      public:
      T12()
      {
            h=0;
            m=0;
            c='\0';
      }
      void getdata()
      {
            cout<<"\n Enter the time according to 12 hour";
            cout<<"\n enter no of hour : ";
            cin>>h;
            cout<<"\n enter no of minitues : ";
            cin>>m;
            cout<<"\n enter a for a.m or p for p.m : ";
            cin>>c;
     
      }
      friend class T24;
     
};
class T24
{
      int ho,mi;
      public:
      T24()
      {
            ho=0;
            mi=0;
      }
      T24(T12 t1)
      {
            if(t1.c == 'p')
            {
                  ho = t1.h + 12;
                  mi = t1.m;
            }
            else
            {
                  ho = t1.h;
                  mi = t1.m;
            }
      }
     
      friend void disp(T24);
};
void disp(T24 t2)
{
      cout<<"\n hour : "<<t2.ho;
      cout<<"\n minitues : "<<t2.mi;
}
int main()
{
     
      T12 t1;
      T24 t2;
      t1.getdata();
      t2=t1;
      disp(t2);
      return 0;
}
 

Wrie a Program to find simple payroll system using single inheritance

Practical - 24


#include<iostream>
using namespace std;
class employee
{
   public:
     int eno;
     char name[20],des[20];
     void get()
     {
              cout<<"Enter the employee number:";
              cin>>eno;
              cout<<"Enter the employee name:";
              cin>>name;
              cout<<"Enter the designation:";
              cin>>des;
     }
};

class salary:public employee
{
     float bp,hra,sb,leave,np;
   public:
     void get1()
     {
              cout<<"Enter the basic pay:";
              cin>>bp;
              cout<<"Enter the Human Resource Allowance:";
              cin>>hra;
              cout<<"Enter the Special Bonus :";
              cin>>sb;
              cout<<"Enter the Leave Deduction rs:";
              cin>>leave;
     }
     void calculate()
     {
              np=bp+hra+sb-leave;
     }
     void display()
     {
              cout<<eno<<"\t"<<name<<"\t"<<des<<"\t"<<bp<<"\t"<<hra<<"\t"<<sb<<"\t"<<leave<<"\t"<<np<<"\n";
     }
};

int main()
{
    int i,n;
    salary s[10];

    cout<<"Enter the number of employee:";
    cin>>n;
    for(i=0;i<n;i++)
    {
              s[i].get();
              s[i].get1();
              s[i].calculate();
    }
    cout<<"\ne_num \t e_name\t des \t bp \t hra \t sb \t leave \t total \n";
    for(i=0;i<n;i++)
    {
              s[i].display();
    }
    return 0;
}

write a c++ program to find student info using multiple inheritance

 Practical - 25


#include<iostream>
using namespace std;

class student
{
    protected:
       int no,m1,m2;
    public:
            void get()
              {
                            cout<<"Enter the Roll no :";
                            cin>>no;
                            cout<<"Enter the two marks :";
                            cin>>m1>>m2;
              }
};
class Practical
{
    protected:
       int pm;
    public:
                void getpm()
              {
                 cout<<"\nEnter the Practical mark :";
                 cin>>pm;

              }
};
class result:public student,public Practical
{
    int tot,avg;
    public:
    void display()
              {
                 tot=(m1+m2+pm);
                 avg=tot/3;
                 cout<<"\nRoll No    : "<<no<<"\nTotal      : "<<tot;
               cout<<"\nAverage    : "<<avg;
              }
};
int main()
{
   result r;
   r.get();
   r.getpm();
   r.display();
   return 0;

}