Search This Blog

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;
}

Monday 4 April 2016

Distance Vector Processing

Explained Logic here! You can set it with input from user and here is the problem
we have to set new routing table for j !




#include<stdio.h>

int main()
{
int A[12]={0,12,25,40,14,23,18,17,21,9,24,29};
int B[12]={24,36,18,27,7,20,31,20,0,11,22,33};
int C[12]={20,31,19,8,30,19,6,0,14,7,22,9};
int D[12]={21,28,36,24,22,40,31,19,22,10,0,9};

char str[12];
//void sum(,;
int q,t[4],z,s,zz=0;
int j[13];
int a=8,i=10,h=12,k=6;
int min;
for(z=0;z<12;z++)
{
j[9]=0;
t[0]=A[z]+a;
t[1]=B[z]+i;
t[2]=C[z]+h;
t[3]=D[z]+k;
min=t[0];
for(q=0;q<4;q++)
{
if(t[q]<min){
min=t[q];
s=q;
}
}
if(s==0)
{
str[zz]='A';
zz++;
}else if(s==1)
{
str[zz]='I';
zz++;
}else if(s==2)
{
str[zz]='H';
zz++;
}else if(s==3)
{
str[z]='K';
zz++;
}
str[9]='-';
j[z]=min;
}
for(q=0;q<12;q++)
{
printf("%d  %c\n",j[q],str[q]);
}
return 0;
}