Search This Blog

Showing posts with label practicallist. Show all posts
Showing posts with label practicallist. Show all posts

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;

}

Thursday, 28 April 2016

Derive the two classes son and daughter and demonstrate polymorphism in action

 Practical - 26


#include <iostream>
using namespace std;
class father
{
protected:
     int age;
public:
     father(int a)
     {
        age=a;
     }
    virtual void whoisme()
     {
     cout<<"\nI am father.My age is:"<<age;
     }
};

class son:public father
{
private:
     int ages;
public:
     son(int y,int x):father(x)
     {
     ages=y;
     }
     void whoisme()
     {
     cout<<endl<<"I am son.My age is: "<<ages;
     }
};
class daughter:public father
{
private:
     int aged;
public:
     daughter(int c,int l):father(l)
     {
     aged=c;
     }
     void whoisme()
     {
     cout<<endl<<"I am daughter.My age is: "<<aged;
     }
};
int main()
{
father f(55);
son s(2,24);
daughter d(1,25);
f.whoisme();
s.whoisme();
d.whoisme();
father *p;
p=&s;
p->whoisme();
p=&d;
p->whoisme();
return 0;
}

Create a class called Person, that has member data name. Include member functions getdata() to get data from the user, and putdata() function to display its data. Write a main( ) program that creates an array of pointers, persPtr[100] to Person. In a loop, ask the user for data, and use new to create an object of type Person to hold the data. Put the pointer to the object in the array. When the user has finished entering the data for all the persons, use a for loop and a single statement such as persPtr[I]->putdata( ) to display the data from each object in the array

Practical 27


 #include <iostream>
using namespace std;

class Person
{
    string name;
public:
    void getdata()
    {
        cout<<"\nEnter Name:";
        cin>>name;
    }
    void putdata()
    {
        cout<<"\nYour name :"<<name;
    }
};

int main()
{
    int n;
    Person *persptr[10];
    cout<<"\nHow Many Names:";
    cin>>n;

    for(int i=0;i<n;i++)
    {
        persptr[i] = new Person;
        persptr[i]->getdata();
    }
    for(int i=0;i<n;i++)
    {
        persptr[i]->putdata();
    }

    return 0;
}

Thursday, 21 April 2016

WAP in c++ to convert lowercase to uppercase from a file.

Practical 28



#include <iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
    ifstream f;
f.open("test.txt");
string line;
while (!f.eof())
{
  f >> line;
  for (int j=0; j< line.length(); ++j)
  {
    line[j] = toupper(line[j]);
  }
  cout<<line<<endl;
     }

    return 0;
}

A hospital wants to create a file regarding its indoor patients. The information to store include _ Name of the patient _ Date of admission _ Disease _ Date of discharge Create a base class to store the above information. The member function should include functions to enter information and display a list of all the patients in the database. And apply the search function also.

Practical 29


#include <iostream>
#include<fstream>
#include<stdlib.h>

using namespace std;

class Hospital
{
    string name,disease;
    string a[10];
    int c;
    long int dateadd,discharge;
    public:
        Hospital()
        {

            c=0;
         string line;
            fstream f1;
            f1.open("hosp.txt",ios::in|ios::out|ios::app);
            while(getline(f1,line))
            {
                c++;
            }
        }
    void adddata()
    {
        fstream f1;
        f1.open("hosp.txt",ios::in|ios::out|ios::app);

        cout<<"\nEnter Name of Patient:";
        cin>>name;

        cout<<"\nEnter Date of admission:";
        cin>>dateadd;

        cout<<"\nEnter Disease:";
        cin>>disease;

        cout<<"\nEnter date of discharge:";
        cin>>discharge;

        if(c==0)
        {
            f1<<endl;
        }

        f1<<name<<" "<<dateadd<<" "<<disease<<" "<<discharge<<endl;
        f1.close();
    }

    void patients()
    {
        fstream f1;
        f1.open("hosp.txt",ios::in|ios::out|ios::app);
        f1.clear();
        f1.seekg(0);
        string l,l1;
        while(getline(f1,l1,' '))
        {
            f1>>l;
            cout<<l1;
        }
        f1.close();
    }

    void searchp()
    {
        fstream f1;
        f1.open("hosp.txt",ios::in|ios::out|ios::app);
        f1.clear();
        f1.seekg(0);
        string p,p1;
        int i=0;
        while(getline(f1,p1,'\n'))
        {
            f1>>p;
            a[i]=p;
            f1>>p1;
            i++;
        }
        string s,line;
        cout<<"\nEnter the name you want to search";
        cin>>s;
        for(i=0;i<c-1;i++)
        {
            if(a[i]==s)
            {
                cout<<"\n\nFound "<<s<<endl;
                break;
            }
            if(i==c-2)
            {
                cout<<"\nNot Found";
            }
        }
        f1.close();
    }
};

int main()
{
    Hospital h;
    int ch;
    while(1)
    {
        cout<<"\n1.Enter Information\n2.Display Patients\n3.Search Patient\n5.exit";
        cin>>ch;

        switch(ch)
        {
        case 1:
            h.adddata();
            break;
        case 2:
            h.patients();
            break;
        case 3:
            h.searchp();
            break;
        case 5:
            exit(0);
            break;
        default:
            cout<<"\nEnter proper choice\n";
        }
    }
    return 0;
}

Monday, 18 April 2016

WAP to swap the numbers using the concept of function template

Practical 30

#include <iostream>
using namespace std;

template <class Gajjar> 
 
void swapab(Gajjar &a, Gajjar &b)
{
  Gajjar temp;
  temp = a;
  a = b;
  b = temp;
}

int main()
{
  int i,j; 
  cout<<"\nEnter int i and j:";
  cin>>i>>j;
  cout<<"\nBefore Swapping With Template: int i & j:"<<i<<" "<<j;
  swapab(i,j);
  cout<<"\nAfter swapping with template int i & j:"<<i<<" "<<j;

  float a,b;
  cout<<"\nEnter float a and b:";
  cin>>a>>b;
  cout<<"\nBefore Swapping With Template: float a & b:"<<a<<" "<<b;
  swapab(a,b); 
  cout<<"\nAfter swapping with template float a & b:"<<a<<" "<<b;    
 return 0;
}

WAP to sort 5 float and integer number using bubble short using template function.

Practical 31


#include <iostream>
using namespace std;
template<class Bubble>
void sort(Bubble a[],int n)
{
    int i,j;
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(a[i]>a[j])
            {
                Bubble ele;
                ele = a[i];
                a[i] = a[j];
                a[j] = ele;
            }
        }
    }
}
int main()
{
    int i,a[5]={5,4,3,2,1};
    float b[5]={1.1,1.5,0.5,0.3,0.0};
    cout<<"\n\n\nInt are\t";
    for(i=0;i<5;i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<"\n\n\nFloats are\t";
    for(i=0;i<5;i++)
    {
        cout<<b[i]<<" ";
    }
    sort(a,5);
    sort(b,5);
    cout<<"\n\n\nSorted int are:\t";
    for(i=0;i<5;i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<"\n\n\nSorted float are:\t";
    for(i=0;i<5;i++)
    {
        cout<<b[i]<<" ";
    }
    return 0;
}

output:

sorted ints are : 1 2 3 4 5 
sorted floats are: 0.0 0.3 0.5 1.1 1.5

Wednesday, 13 April 2016

Write a program with the following: A functin to read two double type numbers from keybord. A function to calculate divison of these two numbers. A try block to throw an exception when a wrong type of data is keyed in . A try block to detect and throw an exception if the condition “divide by zero ” occurs. Appropriate catch block to handle the exception thrown.

 Practical 32




#include <iostream>

using namespace std;

class gajjar
{
    double a,b;
    public:
    void read()
    {
        cout<<"\nEnter two double type numbers:";
        cin>>a>>b;
    }
    void div()
    {
        try{

            if(cin.fail())
                throw "Bad input!";
            if( b == 0 )
            throw 0;

            cout<<"\nAns is "<<a/b;
        }
        catch(const int n)
        {
            cout << "\nDivision by " << n << " not allowed\n";
        }
        catch(const char* Str)
        {
            cout<< Str;
        }
    }
};

int main()
{
    gajjar k;
    k.read();
    k.div();
    return 0;
}

Wednesday, 24 February 2016

Implement a string class containing the following functions. overloaded + operator function to carry out the concatenation of strings. Overloaded = ( assignment ) operator function to carry out string copy. Function to display the length of a string. Function to overload comparison operator (= = ) for two strings.

Practical 22



#include<iostream.h>
#include<stdio.h>
#include<string.h>
#define MAX 50
class string
{
char str[MAX];
public:

 void operator+=(string s1);
void operator==(string s1);
void operator=(string s1);

string()
{
      strcpy(str,” “);
}
     string(char s[])
{
     strcpy(str,s);
}

};

void string::operator+(string s1)
{
      if(strlen(str)+strlen(s1.str)<MAX)
     {
          strcat(str,s1.str);
         cout<<”\n\nConcatinated string is: “<<str;
     }
    else
   {
     cout<<”\n\nSorry the array size is exceeded.”;
    }
}

void string::operator==(string s1)
{
      if(strcmp(str,s1.str)==0)
             cout<<”\n\nStrings are equal.”;
      else
             cout<<”\n\nStrings are not equal.”;
}

void string::operator=(string s1)
{
      strcpy(str,s1.str);
      cout<<”\n\nCopy of string is: “<<str;
}

int main()
{
string s1,s2;
int choice;
char ans;

do
{
cout<<”\n1.Concatination of string\n2.Equal\n3.Copy string\n;
cout<<”\n\nEnter your choice: “;
cin>>choice;
switch(choice)
{
case 1:
cout<<”\n\nEnter the first string: “;
cin>>s1;
cout<<”\n\nEnter the second string: “;
cin>>s2;
s1+s2;
break;


case 2:
cout<<”\n\nEnter the first string: “;
cin>>s1;
cout<<”\n\nEnter the second string: “;
cin>>s2;
s1==s2;
break;

case 3:
cout<<”\n\nEnter the string: “;
cin>>s1;
s2=s1;
break;

default:
cout<<”\n\nPlease enter correct choice.”;
break;
}
cout<<”\n\n\nDo you want to continue?(y/n): “;
cin>>ans;
}while(ans==’y’ || ans==’Y');

}



output:


1.Concatination of string
2.Equal
3.Copy string
Enter your choice: 1

Enter the first string : karan
Enter the second string : gajjar

concatinated string is  :  karangajjar

Create a Rational class as follows, Class rational{ p ublic: rational ( ) { }; private : int num; //numerator int den; //denominator }; Implement the +, /,*, - operators for the rational class.

Practical 21



#include<iostream>
using namespace std;
class rational
{
    int num,den;
    public:
    rational(int a,int b){num=a;den=b;}
    friend void operator+(rational a,rational b);
    friend void operator-(rational a,rational b);
    friend void operator*(rational a,rational b);
    friend void operator/(rational a,rational b);
};
void operator+(rational a,rational b)
{
    int x,y;
    x=a.num*b.den+a.den*b.num;
    y=a.den*b.den;
    cout<<x<<"/"<<y<<endl;
}
void operator-(rational a,rational b)
{
    int x,y;
    x=a.num*b.den - a.den*b.num;
    y=a.den*b.den;
    cout<<x<<"/"<<y<<endl;
}
void operator*(rational a,rational b)
{
    int x,y;
    x=a.num*b.num;
    y=a.den*b.den;
    cout<<x<<"/"<<y<<endl;
}
void operator/(rational a,rational b)
{
    int x,y;
    x=a.num*b.den;
    y=a.den*b.num;
    cout<<x<<"/"<<y<<endl;
}
int main()
{
    rational  a(6,8),b(8,6);
    a+b;
    a-b;
    a*b;
    a/b;
    return 0;
}


Create a class SPACE having three member data x(int),y(int),z(int).overload the unary ‘ - ‘ operator for the class SPACE.

Practical 20


#include<iostream>
using namespace std;

class space
{
    int a,b,c;
    public:
    void setdata(int x,int y,int z)
    {
        a=x;
        b=y;
        c=z;
    }
    void display()
    {
        cout<< a <<" ";
        cout<< b <<" ";
        cout<< c <<endl;
    }
    void operator-();
};

void space::operator-()
{
    a=-a;
    b=-b;
    c=-c;
}

int main()
{
    space s;
    s.setdata(10,-20,73);
    cout<<"S : ";
    s.display();
    -s;
    cout<<"S : ";
    s.display();
    return 0;
}




output:

S:
10  -20  73

S:
-10  20  -73

Write a program to create a copy constructor. A constructor should be created, then a second constructor should be created which should have values of the previous constructor.

Practical 19


 #include<iostream>
using namespace std;

class Gajjar
{
    int id;
    public:
    Gajjar()
    {}
    Gajjar(int a)
    {
        id = a;
    }
    Gajjar(Gajjar &x)
    {
        id = x.id;
    }
    void display()
    {
        cout<<id;
    }
};

int main()
{
    Gajjar A(100);
    Gajjar B(A);
    GajjarC=A;
    GajjarD;
    D=A;
    cout<<"\n Id of A: ";
    A.display();
    cout<<"\n Id of B: ";
    B.display();
    cout<<"\n Id of C: ";
    C.display();
    cout<<"\n Id of D: ";
    D.display();
    return 0;
}


output:

A : 100
B : 100
C : 100
D : 100