C++ Programs

* W.A.P to print "Hello World".

#include<iostream.h>
#include<conio.h>
class hello
{
          clrscr();
          cout<<"Hello World";
          getch();
}

* W.A.P to convert farenhit to celcious.

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main()
{
    int f;
    float c;
    clrscr();
    cout<<"\n enter the farenhit temp";
    cin>>f;

    c=(f-32)/1.8;

    cout<<"\n \n the celcious temp is :="<<c;
    getch();

}

 * W.A.P to calculate power of another number.


#include<conio.h>
#include<iostream.h>
#include<stdio.h>
class power1
{
    public:

    void power(double,int);
};
    void power1::power(double m,int n)
    {
        int i;
        double ans=1;

        for(i=1;i<=n;i++)
        {
            ans=ans*m;
        }

        cout<<"\nthe ANS IS="<<ans;
    }
void main()
{

    double m;
    int n;
    clrscr();
    power1 p;


    cout<<"\n enter the M";
    cin>>m;

    cout<<"\n enter the N";
    cin>>n;
    if(n<0)
    {
        n=2;
    }


    p.power(m,n);
    getch();

}

* W.A.P to swap two values.


#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void swap(int &n1,int &n2);
void main()
{
    int n1,n2;
    clrscr();

    cout<<"Enter No1 :";
    cin>>n1;
    cout<<"\nEnter No2 :";
    cin>>n2;

    cout<<"BEFORE THE SWAPING \n";
    cout<<"\nNO1= :"<<n1;
    cout<<"\nNO2= :"<<n2;
    swap(n1,n2);
    getch();
}
void swap(int &n1,int &n2)
{
    int t;
    t=n1;
    n1=n2;
    n2=t;
    cout<<"\n\n AFTER THE SWAPPING\n";
    cout<<"\nNO1="<<n1<<"\n NO2="<<n2;
}

* Demonstrate the use of static variables in a class by using it to count the number of
objects created in the program

#include<iostream.h>
#include<conio.h>
class item
{
    static int count;
    int number;
    public:
        void getdata(int a)
        {
            number=a;
            count++;
        }
        void getcount(void)
        {
            cout<<"Count:";
            cout<<count<<endl;
        }
};
int item::count;
void main()
{
    item a,b,c;
    clrscr();

    a.getcount();
    b.getcount();
    c.getcount();

    a.getdata(100);
    b.getdata(200);
    c.getdata(300);

    a.getcount();
    b.getcount();
    c.getcount();
    getch();
}

* Write a program that calculates the value of m raised to power n for both int and double
data types. (Use the concept of function overloading)


#include<conio.h>
#include<math.h>
#include<iostream.h>
#define MAXI 10

int power(int,int);
double power(double,double);

int main()
{
    clrscr();
    int im,in,res;
    double dm,dn,dres;
    cout<<endl<<"\tEnter integer value of m : ";
    cin>>im;
    cout<<endl<<"\tEnter integer value of n raised to m: ";
    cin>>in;
    res=power(im,in);
    cout<<endl<<"\t"<<im<<" raised to power "<<in<<" is "<<res;
    cout<<endl<<"\tEnter double value of m : ";
    cin>>dm;
    cout<<endl<<"\tEnter double value of n raised to m: ";
    cin>>dn;
    dres=power(dm,dn);
    cout<<endl<<"\t"<<dm<<" raised to power "<<dn<<" is "<<dres;
    getch();
    return 0;
}

int power(int m,int n)
{
    return(pow(m,n));
}

double power(double m,double n)
{
    return(pow(m,n));
}

* Write a class to represent a vector. Include member functions to perform the following
    1. To create the vector
    2. To modify the value of a given element
    3. To multiply by a scalar value
    4. To display the vector in the form(10,20,30...)


#include<iostream.h>
#include<math.h>
#include<conio.h>
const size=5;
class vector
{
    int *v;
    public:
    vector();
    vector(int[size]);
    void modify(int,int);
    friend vector operator *(vector,int);
    friend vector operator *(int,vector);
    void display();
};

vector :: vector()
{
    v=new int[size];
    for(int i=0;i<5;i++)
    {
        v[i]=0;
    }
}

vector :: vector(int x[size])
{
    v=new int[size];
    for(int i=0;i<5;i++)
    {
        v[i]=x[i];
    }
}

void vector :: modify(int x,int y)
{
    for(int i=0;i<size;i++)
    {
        if(v[i]==x)
        {
            v[i]=y;
            cout<<"Modify successfully"<<endl;
            return;
        }
    }
    cout<<x<<" is not present in vector"<<endl;
}

vector operator *(vector X,int s)
{
    vector Y;
    for(int i=0;i<size;i++)
    {
        Y.v[i]=X.v[i]*s;
    }
    return(Y);
}

vector operator *(int s,vector X)
{
    vector Y;
    Y=X*s;
    return(Y);
}

void vector :: display()
{
    cout<<"(";
    for(int i=0;i<size;i++)
    {
        cout<<v[i]<<",";
    }
    cout<<"\b...)";
}

void main()
{
    clrscr();
    int x[]={10,20,30,40,50};
    vector v=x,Y,Z;
    v.display();
    cout<<endl;
    v.modify(100,10);
    Y = v * 10;
    Z = 20 * v;

    v.display();cout<<endl;
    Y.display();cout<<endl;
    Z.display();cout<<endl;

    getch();
}

*  Define a class Car. Add data members as Make, Color, Size, and Cost. Write member
functions for reading values and printing values of car. Define one more class as
CarCollection. CarCollection contains array of cars. CarCollection class should
contain member functions as Add, delete, modify and replace. Collection is to be
defined as friend of Car class. Use Exception Handling techniques to handle errors.



#include <iostream.h>
#include <conio.h>
#include <string.h>

static int count;
class car
{
    int id;
    int make;
    char color[10];
    int size;
    double cost;
public:
    void getdata()
    {
        id=count + 1;
        cout << "Enter production year=";
        cin >> make;
        cout << "Enter color of car=";
        cin >> color;
        cout << "Enter sheeting capasity of car=";
        cin >> size;
        cout << "Enter cost of car=";
        cin >> cost;
    }
    void display();
    friend collection;
};
void car :: display()
{
    cout <<"\n\n\nInformation of car=";
    cout << "\nCar id is= " << id;
    cout << "\nCar production year= " << make;
    cout << "\nCar color=" << color;
    cout << "\nCar sheeting capasity= " << size;
    cout << "\nCar cost=" << cost;
}

class collection
{
    car *c;
    int ch;
public:
    collection()
    {
          cout << "Enter how many car u want to add=";
          cin >> ch;
          c=new car[ch];
    }
    void add()
    {
          if(count==ch)
            cout << "\n\nU cant enter more car.";
          else
          {
            c[count].getdata();
            count++;
          }
    }
    void delete1();
    void modify();
    void replace();
    void show();
};

void collection :: delete1()
{
       int rep;
       int flag=0;
       cout << "\nEnter which car u want to delete=";
       cin >> rep;
       int i;
       for(i=0;i<count;i++)
       {
      if(rep==c[i].id)
      {
        c[i].id=0;
        c[i].make=0;
        strcpy(c[i].color,NULL);
        c[i].size=0;
        c[i].cost=0;
        flag=1;
        cout << "\nCar successful deleted.";
      }
       }
       if(flag==0)
        cout << "\n No car found of this ID.";
}

void collection :: modify()
{
    int id;
    int flag=0;
    cout << "Enter id which u want to modify= ";
    cin >> id;
    for(int i=0;i<count;i++)
    {
         if(c[i].id==id)
         {
          int ch;
          cout << "\n1..production year.";
          cout << "\n2..color of car.";
          cout << "\n3..capasity.";
          cout << "\n4..cost.";
          cout << "\nWhat u want to modify= ";
          cin >> ch;
          flag=1;
          switch(ch)
          {
          case 1:  cout << "Enter production year=";
               cin >> c[i].make;
               break;
          case 2:  cout << "Enter color of car=";
               cin >> c[i].color;
               break;
          case 3:  cout << "Enter sheeting capasity of car=";
               cin >> c[i].size;
               break;
          case 4:  cout << "Enter cost of car=";
               cin >> c[i].cost;
               break;
          default: cout << "\nInvalid option.";
          }
          cout << "\nSuccessful modified";
         }
    }
    if(flag==0)
         cout << "\nNo such car of this ID.";
}

void collection :: replace()
{
    int rep;
    int flag=0;
    cout << "\nEnter which car u want to replace=";
    cin >> rep;
    int i;
    for(i=0;i<count;i++)
    {
       if(rep==c[i].id)
       {
          cout << "Enter production year=";
          cin >> c[i].make;
          cout << "Enter color of car=";
          cin >> c[i].color;
          cout << "Enter sheeting capasity of car=";
          cin >> c[i].size;
          cout << "Enter cost of car=";
          cin >> c[i].cost;
          flag=1;
          cout << "\nCar successful replaced.";
       }
    }
    if(flag==0)
       cout << "\nNo such car of thid ID.";
}

void collection :: show()
{
    int flag=0;
    for(int i=0;i<count;i++)
    {
         if(c[i].id!=0 && c[i].color!=NULL)
         {
         c[i].display();
         flag=1;
         }
    }
    if(flag==0)
    {
        cout << "\nThere are no such car.";
    }
}

void main()
{
       int ch;
       clrscr();
       collection cc;
       do
       {
      clrscr();
      cout << "\n1...Add car.";
      cout << "\n2...Delete car.";
      cout << "\n3...Modify car.";
      cout << "\n4...Replace car.";
      cout << "\n5...Display.";
      cout << "\n6...Exit.";
      cout << "\nEnter choice what u want to do= ";
      cin >> ch;
      switch(ch)
      {
      case 1:    cc.add();
             getch();
             break;
      case 2:    cc.delete1();
             getch();
             break;
      case 3:    cc.modify();
             getch();
             break;
      case 4:    cc.replace();
             getch();
             break;
      case 5:    cc.show();
             getch();
             break;
      case 6:    return;
      default:   cout << "\nInvalid option.";
             getch();
      }
       }while(ch);
}


}                                                                            
                                                                               


* W.A.P to print area of triangle.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class shape
{
    int l;
    int b;
    float r;

    public:
    void area(int,int,float);
    void area(int,int);
    void area(float);

    void putdata();
};
void main()
{
    int l;
    int b;
    float r;

    clrscr();
    shape sh;

    cout<<"enter the length:-";
    cin>>l;

    cout<<"\n enter the bridth:-";
    cin>>b;

    cout<<"\n enter the radious";
    cin>>r;

    sh.area(l,b,0.5);
    sh.area(l,b);
    sh.area(r);
    getch();
}

void shape::area(int la,int lb,float lr )
{

        l=la;
        b=lb;

    cout<<"\n THE AREA OF TRIANGLE:-";
    cout<<lr*l*b;



}
void shape::area(int la,int lb)
{


    l=la;
    b=lb;
    cout<<"\n\nTHE AREA OF RECTANGLE:-";
    cout<<l*b;
}

void shape::area(float fr)
{
    r=fr;

    cout<<"\n\n THE AREA OF CIRCLE:-";
    cout<<3.14*r*r;
}


* W.A.P to use of constructor.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class emp
{
    float a,b;
    int m,count;

    public:
    emp()
    {
        a=0;
        b=0;
        count++;
    }
    emp(float x, float y)
    {
        a=x;
        b=y;
        count++;
    }

    emp(float x)
    {
        a=x;
        count++;

    }

    void show()
    {
        cout<<"the value of a:"<<a;
        cout<<"\n\nthe value of b:"<<b;

    }


};
void main()
{
    clrscr();

    int count=0;
    emp e,e1,e2;
    e=emp();                 //default constructer
    e1=emp(12.34,13.33);     //parameter constructer
    e2=emp(14.45,0);
    e.show();
    cout<<"\n\n";
    cout<<"--------------------------------\n\n";
    e1.show();
    cout<<"\n\n";
       cout<<"--------------------------------\n\n";
    e2.show();
    getch();
}


* Overload all the four arithmetic operators to operate on a vector class and also the
overload the * operator to multiply scalar values to the vector class. Overload the >>
operator to input a vector and the << operator to display the vector in the form
(10,20,.....). Also overload the [] operator to access the indivisual member of the
vector. Use Dynamic memory allocation to achieve the solution. Write appropriate
constructor and destructure for the class.


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

const size=3;
class vector
{
    int v[size];
    public:
    vector();
    vector(int * x);
    friend vector operator+(vector,vector);
    friend vector operator-(vector,vector);
    friend vector operator*(vector,vector);
    friend vector operator*(int,vector);
    friend vector operator/(vector,vector);
    friend istream &operator>>(istream &,vector &);
    friend ostream &operator<<(ostream &,vector &);
};

vector :: vector()
{
    for(int i=0;i<size;i++)
        v[i]=0;
}

vector :: vector(int * x)
{
    for(int i=0;i<size;i++)
        v[i]=x[i];
}

//vector addition
vector operator+(vector a,vector b)
{
    vector c;
    for(int i=0;i<size;i++)
        c.v[i] = a.v[i] + b.v[i];
    return c;
}

//vector subtraction
vector operator-(vector a,vector b)
{
    vector c;
    for(int i=0;i<size;i++)
        c.v[i] = a.v[i] - b.v[i];
    return c;
}

//vector Multiplication
vector operator*(vector a,vector b)
{
    vector c;
    for(int i=0;i<size;i++)
        c.v[i] = a.v[i] * b.v[i];
    return c;
}
//vector Multiplication by scalar
vector operator*(int x,vector a)
{
    vector c;
    for(int i=0;i<size;i++)
        c.v[i] = a.v[i] * x;
    return c;
}

//vector Division
vector operator/(vector a,vector b)
{
    vector c;
    for(int i=0;i<size;i++)
        c.v[i] = a.v[i] / b.v[i];
    return c;
}

//Creating vector
istream &operator >>(istream &din,vector &b)
{
    int i;
    cout<<endl<<"\tEnter elements for the vector : "<<endl<<endl;
    for(i=0;i<size;i++)
        {
        cout<<"\tEnter element for "<<i+1<<" : ";
        din>>b.v[i];
        }
    return(din);
}

ostream &operator <<(ostream &dout,vector &b)
{
    int i;
    dout<<"\tContents of vector are : "<<endl<<endl;
    for(i=0;i<size;i++)
        dout<<"\t"<<b.v[i];
    getch();
    return(dout);
}

int main()
{
    clrscr();
    int choice,pos,temp,scalar;
    vector V1,V2,V3;

    do
    {
        cout<<"\t********************************************"<<endl;
        cout<<"                        M E N U               "<<endl;
        cout<<"\t********************************************"<<endl;
        cout<<"\t1.   Vector Addition"<<endl;
        cout<<"\t2.   Vector Subtraction"<<endl;
        cout<<"\t3.   Vector Multiplication"<<endl;
        cout<<"\t4.   Vector Multiplication by a scalar"<<endl;
        cout<<"\t5.   Vector Division"<<endl;
        cout<<"\t6.   Exit"<<endl;
        cout<<"\t********************************************"<<endl;
        cout<<endl;
        cout<<"\tEnter your Choice : ";
        cin>>choice;

        switch(choice)
        {
            case 1:
                //Vector Addition
                cout<<endl<<"\tVector 1..."<<endl;
                cin>>V1;
                cout<<endl<<"\tVector 2..."<<endl;
                cin>>V2;
                V3 = V1 + V2;
                cout<<V3;
                break;
            case 2:
                //Vector Subtraction
                cout<<endl<<"\tVector 1..."<<endl;
                cin>>V1;
                cout<<endl<<"\tVector 2..."<<endl;
                cin>>V2;
                V3 = V1 - V2;
                cout<<V3;
                break;
            case 3:
                //Vector Multiplication
                cout<<endl<<"\tVector 1..."<<endl;
                cin>>V1;
                cout<<endl<<"\tVector 2..."<<endl;
                cin>>V2;
                V3 = V1 * V2;
                cout<<V3;
                break;
            case 4:
                //Vector Multiplication by scalar
                cout<<endl<<"\tVector 1..."<<endl;
                cin>>V1;
                cout<<"\tEnter scalar value to multiply : ";
                cin>>scalar;
                V3 = scalar * V1;
                cout<<V3;
                break;
            case 5:
                //Vector Division
                cout<<endl<<"\tVector 1..."<<endl;
                cin>>V1;
                cout<<endl<<"\tVector 2..."<<endl;
                cin>>V2;
                V3 = V1 / V2;
                cout<<V3;
                break;
            case 6:

                getch();
                exit(0);
            default:
                cout<<"\n\tEnter proper choice...";
                getch();
                break;
        }
        clrscr();
    }
    while(choice!=8);
    getch();
return 0;
}

* Write a menu driven program that can perform the following functions on strings. (Use
    overloaded operators where possible). (Do not use predefined string class )
    1. Compare two strings for equality (== operator)
    2. Check whether first string is smaller than the second (<= operator)
    3. Copy the string to another
    4. Extract a character from the string (Overload [])
    5. Reverse the string
    6. Concatenate two strings (+ operator)

#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

class word
{
    char str[20];
    public:
    void getdata();
    void display();
    int operator ==(word);
    int operator <=(word);
    void copy(word);
    char operator[](int);
    void reverse();
    void operator +=(word);
};

void word :: getdata()
{
    cin>>str;
}

void word :: display()
{
    cout<<endl<<"\t"<<str<<endl;
}

int word :: operator ==(word s)
{
    if(strcmp(str,s.str)==0)
        return(1);
    return(0);
}

int word :: operator <=(word s)
{
    if(strcmp(str,s.str)<0)
        return(1);
    return(0);
}

void word :: copy (word s)
{
    strcpy(str,s.str);
}

char word :: operator [](int x)
{
    return(str[x-1]);
}

void word :: reverse()
{
    strrev(str);
}

void word :: operator +=(word s)
{
    strcat(str,s.str);
}

void main()
{
    clrscr();
    word a,b;
    char c;
    int x,choice;

    do
    {
        cout<<"\t********************************************"<<endl;
        cout<<"\t                   M E N U                  "<<endl;
        cout<<"\t********************************************"<<endl;
        cout<<"\t1.   Equality(==)"<<endl;
        cout<<"\t2.   Smaller(<=)"<<endl;
        cout<<"\t3.   Copy"<<endl;
        cout<<"\t4.   Extract([])"<<endl;
        cout<<"\t5.   Reverse"<<endl;
        cout<<"\t6.   Concatenate"<<endl;
        cout<<"\t7.   Exit"<<endl;
        cout<<"\t********************************************"<<endl;
        cout<<endl;
        cout<<"\tEnter your Choice : ";
        cin>>choice;

        switch(choice)
        {
            case 1:
                cout<<endl<<"\tEnter first string :";
                a.getdata();
                cout<<endl<<"\tEnter second string :";
                b.getdata();

                if(a==b)
                    cout<<endl<<"\tStrings are equal\n";
                else
                    cout<<endl<<"\tStrings are not same\n";
                break;
            case 2:
                cout<<endl<<"\tEnter first string :";
                a.getdata();
                cout<<endl<<"\tEnter second string :";
                b.getdata();
                if(a<=b)
    cout<<endl<<"\tString first is lesser then second"<<endl;
                else
    cout<<endl<<"\tString second is lesser then first"<<endl;
                break;
            case 3:
                cout<<endl<<"\tEnter string :";
                b.getdata();
                a.copy(b);
                cout<<endl<<"\tThe copied string is ";
                a.display();
                break;
            case 4:
                cout<<endl<<"\tEnter string :";
                a.getdata();
        cout<<endl<<"\tEnter position of character to extract : ";
                cin>>x;
                c = a[x];
                cout<<endl<<"\tThe extracted string is : "<<c;
                break;
            case 5:
                cout<<endl<<"\tEnter string :";
                b.getdata();
                b.reverse();
                b.display();
                break;
            case 6:
                cout<<endl<<"\tEnter first string :";
                a.getdata();
                cout<<endl<<"\tEnter second string :";
                b.getdata();
                a+=b;
                a.display();
                break;
            case 7:
                cout<<endl<<"\tCreated by Jayesh P. Mehta...";
                getch();
                exit(1);
            default:
                cout<<endl<<"\tEnter proper choice...";
                getch();
                break;
        }
        getch();
        clrscr();
    }
    while(choice!=8);
    getch();
}
     
* Overload subscript operator [] for a array class.

#include <iostream.h>
#include <conio.h>
class subscript
{
    int a[5];
    public:
    subscript()
    {
        a[0]=1;
        a[1]=2;
        a[2]=3;
        a[3]=4;
        a[4]=5;
      }
     int operator [](int index)
     {
        int temp;
        temp=a[index];
        return temp;
    }
};

int main()
{
    subscript s;
    clrscr();
    int temp[5];
    cout << "Array are under below.";
    for(int i=0;i<5;i++)
    {
        temp[i]=s[i];
        cout<<endl<<temp[i];
    }
    getch();
    return 0;
}

* Overload function call operator () to allow the more common form of double array
subscripting. Instead of saying x[row][column] for an array of objects, overload the
function call operator to allow the alternate form x(row, column)

#include <iostream.h>
#include <conio.h>
class test
{
    int a[2][2];
    public:
        test();
        int operator ()(int r,int c)
        {
            return a[r][c];
        }
};

test :: test()
{
    cout << "\n\nEnter the value of 2D array.\n\n";
    for(int i=0;i<2;i++)
    {
        for(int j=0;j<2;j++)
        {
            cout << "Enter" << i+1 << " row and " << j+1 <<" Column=";
            cin >> a[i][j];
        }
    }
}
int main()
{
    clrscr();
    test t;
    int x[2][2];
    for(int row=0;row<2;row++)
    {
        for(int column=0;column<2;column++)
        {
            x[row][column]=t(row,column);
        }
    }
    cout << "\nDisplay 2D Array.\n\n";
    for(int i=0;i<2;i++)
    {
        for(int j=0;j<2;j++)
        {
        cout << x[i][j] << "   ";
        }
    cout << endl << endl;
    }
     getch();
     return 0;
}

* Define a singly linked list class, which is a made up objects of node class. Provide
addition, deletion of nodes, with operator overloading.


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class node
{
    int no;
    node *next;
    public:
        void insert(int);
        void delet(int);
        void display();

}*start=NULL;
void node :: insert(int x)
{
    node *tmp;
    tmp=(node *)malloc(sizeof(node *));
    tmp->no=x;
    if(start==NULL)
    {
        tmp->next=NULL;
        start=tmp;

    }
    else
    {
        tmp->next=start;
        start=tmp;
    }
}
void node :: delet(int data)
{
    int i=0;
    node *old,*tmp;
    tmp=start;
    old=start;
    while(tmp!=NULL)
    {
        if(tmp->no==data)
        {
            old->next=tmp->next;
            free(tmp);
            return;
        }
        else
        {
            old=tmp;
            tmp=tmp->next;
            i++;
        }

    }
    cout<<"Record Not Found";
}

void node :: display()
{
    node *t;
    t=a;
    while(t!=NULL)
    {
        cout<<t->no<<endl;
        t=t->next;
    }
}


void main()
{
    int ch,x,y;
    node l;
    clrscr();
    while(1)
    {
        cout<<"\n\t*********Menu************";
        cout<<"\n\t1.Insert an element in queue.";
        cout<<"\n\t2.Remove an element in queue.";
        cout<<"\n\t3.Dispaly.";
        cout<<"\n\t4.Exit";
        cout<<"\n\t*************************";
        cout<<"\nEnter choice:";
        cin>>ch;
        switch(ch)
        {
            case 1:
                cout<<"\nEnter element:";
                cin>>x;
                l.insert(x);
                break;
               case 2:
                cout<<"\nEnter element to remove:";
                cin>>y;
                l.delet(y);
                break;
            case 3:
                l.display();
                break;
            case 4:
                exit(0);
            default:
                cout<<"Invalid Choice";
                break;

        }
    }
}

* Define a matrix class, which allows addition, subtraction, multiplication with another
matrix, multiplication with a scalar value, and inverse of a matrix( Use operator
overloading). Use dynamic constructors and destructors for allocation and de
allocation of memory.



#include<stdlib.h>
#include<conio.h>
#include<iostream.h>
#include<math.h>

int size;

class matrix
{
    int row,col;
    public:
        float **mat;

        matrix()
        {
            row=0;
            col=0;
        }
        matrix(int r,int c);

        void create();
        void display();
        matrix operator +(matrix);
        matrix &operator +(int);
        matrix operator -(matrix);
        matrix &operator -(int);
        matrix operator *(matrix);
        matrix &operator *(int);
        matrix &operator /(int);
        matrix &transpose(matrix);
        matrix &inverse();
        int determinant();
        matrix &update(int,int,int);
};

matrix::matrix(int r,int c):row(r),col(c)
{
        //Allocating memory to matrix
    mat=new float *[row];
    for(int i=0;i<row;i++)
        mat[i]=new float[col];

    //Initializing matrix to zero

    for(i=0;i<row;i++)
        for(int j=0;j<col;j++)
                mat[i][j]=0;
}

//Creating Matrix
void matrix :: create()
{
    int i,j;
    cout<<"\tEnter elements for the Matrix : "<<endl<<endl;
    for(i=0;i<row;i++)
        for(j=0;j<col;j++)
        {
            cout<<"\tEnter element for "<<i+1<<" row and "<<j+1<<" : ";
            cin>>mat[i][j];
        }
}

//Displaying Matrix
void matrix :: display()
{
    int i,j;
    cout.precision(2);
    cout<<"\tContents of Matrix are : "<<endl<<endl;
    for(i=0;i<row;i++)
        {
            for(j=0;j<col;j++)
                cout<<"\t"<<mat[i][j];
            cout<<endl;
        }
    getch();
}

//Matrix addition
matrix matrix :: operator +(matrix b)
{
    matrix temp(size,size);
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
            temp.mat[i][j] = mat[i][j] + b.mat[i][j];
    return(temp);
}

//Matrix addition by scalar
matrix &matrix :: operator +(int x)
{
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
            mat[i][j] += x;
    return *this;
}

//Matrix subtraction
matrix matrix :: operator -(matrix b)
{
    matrix temp(size,size);
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
            temp.mat[i][j] = mat[i][j] - b.mat[i][j];
    return(temp);
}

//Matrix subtraction by scalar
matrix &matrix :: operator -(int x)
{
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
            this->mat[i][j] -= x;
    return *this;
}

//Matrix Multiplication
matrix matrix :: operator *(matrix b)
{
    matrix temp(size,size);
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
        {
            temp.mat[i][j]=0;
            for(int k=0;k<col;k++)
                temp.mat[i][j] += mat[i][k] * b.mat[k][j];
        }
    return(temp);
}

//Matrix Multiplication by scalar
matrix &matrix :: operator *(int x)
{
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
            this->mat[i][j] *= x;
    return *this;
}

//Matrix Division
matrix &matrix :: operator /(int x)
{
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
            mat[i][j] /= x;
    return *this;
}

//Matrix Transpose
matrix &matrix :: transpose(matrix a)
{
    for(int i=0;i<row;i++)
        for(int j=0;j<col;j++)
            this->mat[i][j] = a.mat[j][i];
    return *this;
}

//Matrix Inverse
matrix &matrix :: inverse()
{
    int i;
    for(int k=0 ; k<row;k++)
    {
        for(int j=0;j<col;j++)
        {
            if(j!=k)
            {
                mat[k][j]=mat[k][j]/mat[k][k];
            }
    }
    mat[k][k]=1.0/mat[k][k];
        for(int i=0;i<row;i++)
        {
            if(i!=k)
            {
                for(j=0;j<col;j++)
                {
                    if(j!=k)
                    {
                        mat[i][j]=mat[i][j]-(mat[i][k]*mat[k][j]);
                    }
                }
            }
        }
        for(i=0;i<col;i++)
        {
            if(i!=k)
            {
                mat[i][k]=-mat[i][k]*mat[k][k];
            }
        }
    }
    return *this;
}

//Matrix determinant
int matrix :: determinant()
{
    int indx[20]={0};
    double d,pi,c1,c[20],pil,itmp,k,pj,ss;
    int i,j,msgn;
    for(i=0;i<row;i++)
        indx[i]=i;
    for(i=0;i<row;i++)
    {
        c1=0;
        for(j=0;j<row;++j)
        {
            if(fabs(mat[i][j]>c1))
                c1=fabs(mat[i][j]);
        }
        c[i]=c1;
    }

    for(j=0;j<row-1;++j)
    {
        pil=0;
        for(i=j;i<row;++i)
        {
            pi=fabs(mat[indx[i]][j])/c[indx[i]];
            if(pi>pil)
            {
                pil=pi;
                k=i;
            }
        }
            itmp=indx[j];
        indx[j]=indx[k];
        indx[k]=itmp;
        for(i=j+1;i<row;++i)
        {
            pj=mat[indx[i]][j]/mat[indx[j]][j];
            mat[indx[i]][j]=pj;

            for(k=j+1;k<row;++k)
            mat[indx[i]][k]=mat[indx[i]][k]-pj*mat[indx[j]][k];
        }
    }
    d=1.0;
    for(i=0;i<row;i++)
        d=d*mat[indx[i]][i];

    msgn=1;

    for(i=0;i<row;++i)
    {
        if(i!=indx[i])
        {
            msgn=-msgn;
            j=indx[i];
            indx[i]=indx[j];
            indx[j]=j;
        }
    }

    d=msgn*d;
    return(d);
}

//Updating element
matrix &matrix :: update(int i,int j,int x)
{
    this->mat[i][j]=x;
    return *this;
}

void main()
{
    clrscr();
    int choice,row,col,scalar,temp;
    cout<<"\tEnter the size of matrix : ";
    cin>>size;
    cout<<endl;
    matrix A(size,size),B(size,size),C(size,size);

    cout<<"\tThis are the operations for square matrix only"<<endl<<endl;

    do
    {
        cout<<"\t********************************************"<<endl;
        cout<<"                        M E N U               "<<endl;
        cout<<"\t********************************************"<<endl;
        cout<<"\t1.   Matrix Addition"<<endl;
        cout<<"\t2.   Matrix Addition by a scalar"<<endl;
        cout<<"\t3.   Matrix Subtraction"<<endl;
        cout<<"\t4.   Matrix Subtraction by a scalar"<<endl;
        cout<<"\t5.   Matrix Multiplication"<<endl;
        cout<<"\t6.   Matrix Multiplication by a scalar"<<endl;
        cout<<"\t7.   Matrix Division by a scalar"<<endl;
        cout<<"\t8.   Matrix Transpose"<<endl;
        cout<<"\t9.   Matrix Inverse"<<endl;
        cout<<"\t10.  Matrix Determinant"<<endl;
        cout<<"\t11.  Matrix Modification"<<endl;
        cout<<"\t12.  Exit"<<endl;
        cout<<"\t********************************************"<<endl;
        cout<<endl;
        cout<<"\tEnter your Choice : ";
        cin>>choice;

        switch(choice)
        {
            case 1:
                //Matrix Addition
                A.create();
                B.create();
                C=A+B;
                C.display();
                break;
            case 2:
                //Matrix Addition by scalar
                A.create();
                cout<<"\tEnter scalar value to be added : ";
                cin>>scalar;
                (A+scalar).display();
                break;
            case 3:
                //Matrix Subtraction
                A.create();
                B.create();
                (A-B).display();
                break;
            case 4:
                //Matrix Subtraction by scalar
                A.create();
                cout<<"\tEnter scalar value to subtract : ";
                cin>>scalar;
                (A-scalar).display();
                break;
            case 5:
                //Matrix Multiplication
                A.create();
                B.create();
                C=A*B;
                C.display();
                break;
            case 6:
                //Matrix Multiplication by scalar
                A.create();
                cout<<"\tEnter scalar value to multiply : ";
                cin>>scalar;
                (A*scalar).display();
                break;
            case 7:
                //Matrix Division by scalar
                A.create();
                cout<<"\tEnter scalar value to divide : ";
                cin>>scalar;
                (A/scalar).display();
                break;
            case 8:
                //Matrix Transpose
                A.create();
                B.transpose(A).display();
                break;
            case 9:
                A.create();
                A.inverse().display();
                break;
            case 10:
                //Matrix Determinant
                A.create();
                temp=A.determinant();
                cout<<"\tThe determinant is : "<<temp;
                getch();
                break;
            case 11:
                A.create();
                int r1,c1;
                cout<<"\tEnter postion of row to modify : ";
                cin>>r1;
                cout<<"\tEnter postion of column to modify : ";
                cin>>c1;
                cout<<"\tEnter the new element : ";
                cin>>temp;
                A.update(r1-1,c1-1,temp).display();
                break;
            case 12:
               
                getch();
                exit(0);
            default:
                cout<<"\n\tEnter proper choice...";
                getch();
                break;
        }
        clrscr();
    }
    while(choice!=11);
    getch();
}

*/
(19) Define a class Result which contains the result of an MCA written test. It should
take merit list from a file and display on the screen such that at a time only ten
candidates information is printed on the screen. The headings should be displayed
using a manipulator. All the information should be aligned with the headings.
*/
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<iomanip.h>
#include<stdlib.h>
class Result
{
   protected:
    int rollno;
    float spi;
   public:
    void readdata()
    {
        cout<<"Enter Roll Number:";
        cin>>rollno;
        cout<<"Enter SPI Number:";
        cin>>spi;
    }
    void writedata()
    {
        cout<<endl<<setw(12)<<rollno<<setw(20)<<spi<<endl;
    }
};
void main()
{
    Result rs[50];
    int n;
    fstream  inout;
    clrscr();
    inout.open("Result",ios::in | ios::out);
    cout<<"Enter no. of Students for Merit..(must be less than 50)\n";
    cin>>n;
    for(int i=0;i<n;i++)
    {
        rs[i].readdata();
        inout.write((char *)& rs[i],sizeof(rs[i]));
    }
    inout.seekg(0);
    clrscr();
    cout<<"\n"<<setw(20)<<" Merit list \n\n";
    cout<<setw(12)<<"RollNumber"<<setw(20)<<"SPINumber";
    if(n>10)
    {
        for(int i=0;i<10;i++)
        {
            inout.read((char *) & rs[i],sizeof(rs[i]));
            rs[i].writedata();
        }
    }
    else
    {
        for(int i=0;i<n;i++)
        {
            inout.read((char *) & rs[i],sizeof(rs[i]));
            rs[i].writedata();
        }
    }
    inout.close();
    getch();
}

* Write a C++ program that displays a student object read from a file backwards.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<iomanip.h>
class employee
{
    public:
    char emp_name[15],emp_dept[15];
    int emp_id;
    void getdata()
    {
        cout<<"Enter Employee Name : ";
        cin>>emp_name;
        cout<<"Enter Employee no. : ";
        cin>>emp_id;
        cout<<"Enter Department Name : ";
        cin>>emp_dept;
    }
    void display()
    {
        cout<<endl<<setw(10)<<emp_name;
        cout<<setw(10)<<emp_id;
        cout<<setw(10)<<emp_dept;
    }
};
employee emp;
fstream out;
void rfile();
void wfile();
void genlist();
void main()
{
    int ins;
    clrscr();
    out.open("file.doc",ios::in |ios::out|ios::app);
    if(!out)
    {
        cout<<endl<<"File not Created ";
    }
    else
    {
        wfile();
        genlist();
        rfile();
    }
}
void wfile()
{
    emp.getdata();
    out.write((char *)&emp,sizeof(emp));
}
void rfile()
{
    int r=0,s=sizeof(emp),m;
    cout<<endl<<"Records Display in reverse order of file Employee";
    out.seekg(0,ios::end);
    m=out.tellg();
    r=m-s;
    while(r>=0)
    {
        out.seekg(r,ios::beg);
        out.read((char * )& emp, sizeof(emp));
        emp.display();
        r=r-s;
    }
    getch();
}
void genlist()
{
    int r=0,s=sizeof(emp),m;
    cout<<endl<<"Records in file Employee";
    out.seekg(0,ios::end);
    m=out.tellg();
    while(r<m)
    {
        out.seekg(r,ios::beg);
        out.read((char * )& emp, sizeof(emp));
        emp.display();
        r=r+s;
    }
    getch();
}

* Use an Employee Class to write records of employee to a file. Include a menu that
will allow the user to select any of the following features
    a. Add a new record.
    b. Modify an existing record.
    c. Retrieve and display an entire record for a given name.
    d. Generate a complete list of all names, addresses and telephone numbers.
    e. End of the computation.


#include<conio.h>
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>
int flag,temp,temp1,temp2,temp3;
class file
{
     public:
    long int no;
    char name[20];
    void putdata(char n[] ,long int num);
    void getdata1(long int num);
    void getdata2(char n[]);
    void update(char nam[]);
    void write(void);
};
void file ::putdata (char n[],long int num)
{
    strcpy(name,n);
    no=num;
}
void file ::getdata1(long int num)
{
    if(no==num)
    {
        temp=1;
        cout<<endl<<"\tThe name of the person is "<<name;
    }
}
void file ::getdata2(char nam[])
{
    int c;
    c=strcmp(name,nam);
    if(c==0)
    {
        temp1=1;
        cout<<endl<<"\tThe telephone number of the person is "<<no;
    }
}
void file ::update(char nam[])
{
    int c;
    c=strcmp(name,nam);
    if(c==0)
    {
        temp2=1;
        cout<<endl<<"\tEnter the new telephone number of the person: ";
        cin>>no;
    }
}
void file ::write()
{
    fstream outf;
    if(flag==0)
    {
        flag=1;
        outf.open("f:\\data.txt",ios::in);
    }
    outf<<name;
    outf<<no;
    outf.close();
}
void main()
{
    clrscr();
    char nam[30],c;
    long int num,x,ch;
    file f[3];
    fstream outf,inf;
    inf.open("f:\\data.txt",ios::in);
    int i=0;
    while(inf)
    {
        inf>>nam;
        inf>>num;
        if(inf.eof()!=0)
            break;
        f[i].putdata(nam,num);
        i++;
    }
    inf.close();
    while(ch!=4)
    {
        clrscr();
        cout<<endl<<"\t****************************************************";
        cout<<endl<<"\t                   M E N U";
        cout<<endl<<"\t****************************************************";
        cout<<endl<<"\t1 :Enter the telephone number of the person"<<
        endl<<"\t2 :Enter the name of the person to find the number"<<
        endl<<"\t3 :Updation"<<
        endl<<"\t4 :Exit";
        cout<<endl<<"\t****************************************************";
        cout<<endl;
        cout<<endl<<"\tEnter your choice:";
        cin>>ch;
        switch(ch)
        {
            case 1:
                cout<<endl<<"\tEnter the telephone number:";
                long int number;
                cin>>number;
                for(int i=0;i<3;i++)
                    if(temp==0)
                        f[i].getdata1(number);
                break;
            case 2:
                cout<<endl<<"\tEnter the name:";
                char nam[20];
                cin>>nam;
                for(i=0;i<3;i++)
                    if(temp1==0)
                        f[i].getdata2(nam);
                break;
            case 3:
                cout<<endl<<"\tEnter the name:";
                char nam1[20];
                cin>>nam1;
                for(i=0;i<3;i++)
                    if(temp2==0)
                        f[i].update(nam1);
                for(i=0;i<3;i++)
                    f[i].write();
                break;
            case 4:
                exit(1);
            default:
                cout<<endl<<"\t\n\tBye";
                exit(1);
        }
        getch();
    }
}

* Write a program that returns the size in bytes of a file entered on the command line.

#include <iostream.h>
#include <sys/types.h>
#include <sys/stat.h>
//#include <unistd.h>

int main( int argc, char** argv )
{
    if (argc != 2)
    {
        cerr << "usage:\n" "  filesize FILENAME\n\n";
        return 1;
    }

    struct stat filestatus;
    stat( argv[ 1 ], &filestatus );

    cout << filestatus.st_size << " bytes\n";

    return 0;
}

* Write a program that swaps each character pair in a text file. For example, if the
file contains "1234", then after the program in run, the file will contain "2143".

#include<iostream.h>
#include<fstream.h>
#include<string.h>
int Get_Size( char *path );
void main()
{

    ofstream outf("c:\\no.txt");
    cout<<"enter the 4 digit no:";
    char no[4];
    cin>>no;
    int len=strlen(no);
    char tmp;
    if(len==4)
    {
        tmp=no[0];
        no[0]=no[1];
        no[1]=tmp;
        tmp=no[2];
        no[2]=no[3];
        no[3]=tmp;
    }
    else
        cout<<"Invalid Number";


    outf<<no;
    outf.close();
    ifstream inf("c:\\no.txt");
    cout<<"no:"<<no<<"\n";
    inf.close();
}

* Write a template function called find(). This function searches an array for an object.
It returns either the index of the matching object (if one is found) or -1 if no match
is found.

#include<iostream.h>
#include<conio.h>

template <class T>
int find(T a[],int s, T x)
{
    for(int i=0;i<s;i++)
    {
        if(a[i]==x)
        {
            return(i);
        }
    }
    return -1;
}
void main()
{
    int a[]={10,20,30};
    char str[]={'a','b','c'};
    int f;
    clrscr();
    if((f=find(a,3,20))!=-1)
    {
        cout<<"found at position "<<f<<endl;
    }
    else
    {
        cout<<"not found"<<endl;
    }
    if((f=find(str,3,'x'))!=-1)
    {
        cout<<"found at position "<<f<<endl;
    }
    else
    {
        cout<<"not found"<<endl;
    }
}

* Write a object oriented program to implement a generic Stack. Incorporate all the
possible operation on Stack in the program. Rework stack class so that stack
overflows are handled as exceptions.

#include<iostream.h>
#include<conio.h>

class stack
{
    int s[10];
    int top,size;
    public:
    stack()
    {
        top=-1;
        cout<<"Enter the size of stack : ";
        cin>>size;
    }
    void push();
    void pop();
    void peep();
    void change();
    void display();


};

void stack :: push()
{
  // try
   {
    if(top>=size-1)
    {
        cout<<endl<<"Stack overflow"<<endl;
        return;
//        throw except;
    }
    top++;
    int x;
    cout<<endl<<"Enter the element you want to insert : ";
    cin>>x;
    s[top]=x;
    return;
   }
 /*  catch(stack except)
   {
    cout<<endl<<"Stack overflow"<<endl;
   } */
}

void stack :: pop()
{
    if(top<0)
    {
        cout<<endl<<"Stack underflow"<<endl;
        return;
    }
    top--;
    cout<<endl<<"deleted element is : "<<s[top+1];
    return;
}

void stack :: peep()
{
    int i;
    cout<<endl<<"Enter the position of the element from top : ";
    cin>>i;
    if(top-i+1<=0)
    {
        cout<<endl<<"Stack underflow"<<endl;
        return;
    }
    cout<<endl<<"element at "<<i<<"th position is  : "<<s[top-i+1]<<endl;;
    return;
}

void stack :: change()
{
    int i;
    cout<<endl<<"Enter the position of the element from top : ";
    cin>>i;
    if(top-i+1<=0)
    {
        cout<<"Stack underflow"<<endl;
        return;
    }
    int x;
    cout<<endl<<"Enter the new value : ";
    cin>>x;
    s[top-i+1]=x;
    return;
}

void stack :: display()
{
    for(int i=top;i>=0;i--)
    {
        cout<<s[i]<<"\t";
    }
    cout<<endl;
}

void main()
{
    clrscr();
    stack s;
    s.push();
    s.push();
    s.push();
    s.push();
    s.push();
    s.push();
    s.push();
    s.push();
    s.display();
    s.pop();
    s.display();
    s.push();
    s.push();
    s.push();
    s.display();
    s.peep();
    s.change();
    s.display();
    getch();
}


* Write a program to create template class called "Safearray". Rules for this class are as follows :
    (1) Size is equal to 100
    (2) Index from 0 to size -1
    (3) If array is sought outside bound the program aborts
    (4) Function safeput() is used to assign value to an array element.
    (5) Function safeget() is used to return the array element.
Make necessary provisions so the program terminates
gracefully when unsafe action is attempted.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
template <class array>
class safearray
{
    public:
    array a[100];
    int index;
    safearray()
    {
        index=-1;
    }
    void safeput(array val)
    {
        if(index==99)
        {
            cout<<endl<<"Array is Full";
            exit(0);
        }
        else
        {
            index++;
            a[index]=val;
        }
    }
    array safeget()
    {
        if(index==-1)
        {
            return NULL;
        }
        else
        {
            array val=a[index];
            index--;
            return val;
        }
    }
};
void main()
{
    clrscr();
    safearray <int> sa;
    int ch,num;
    cout<<endl<<"Values going store in Array"<<endl;
    for(int  i=0,j=1,k=1;i<100;i++,j++)
    {
        cout<<"  "<<((i+1)*10);
        sa.safeput((i+1)*10);
        if(j==(k*10))
        {
            cout<<endl;
            k++;
        }
    }
    cout<<endl<<"Values Retrrived from Array"<<endl;
    for(int  ji=0,jj=1,jk=1;i<100;ji++,jj++)
    {
        cout<<"  "<<((ji+1)*10);
        sa.safeget();
        if(j==(jk*10))
        {
            cout<<endl;
            jk++;
        }
    }
    getch();
}

* Design a manipulator to provide the following output specifications for printing
float values
    (i) 15 column width
    (ii) Right justified
    (iii) 2 digits precision
    (iv) Filling unused spaces with +.
    (v) Showing trailing zeros.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
ostream & fc(ostream & o)
{
    o.width(15);
    o.precision(2);
    o.fill('+');
    o.setf(ios::right,ios::adjustfield);
    o.setf(ios::showpoint);
    return o;
}
void main()
{
    float x;
    clrscr();
    cout<<"Enter the Number : ";
    cin>>x;
    cout<<x<<"is : "<<fc<<x;
    getch();
}

* Create an input manipulator called skipchar() that reads and ignores the ten
characters from the input stream. Create an output manipulator called sethex()
that sets output to hexadecimal and turns on the uppercase and showbase flags.
Also create an output manipulator called rest() that undoes the charges made by
sethex().Write a driver program to test the manipulators.
#include<iostream.h>
#include<conio.h>
#include<string.h>
ostream & skip_10_Char(ostream & out)
{  char s1[50],s2[50];
  out<<"\nEnter String : ";
  cin>>s1;
  int j=0;
  for(int i=10;s1[i]!='\0';i++)
  {    s2[j]=s1[i];     j++;    }
  s2[j]='\0';
  out<<"\nOriginal String :"<<s1;
  out<<"\nString with TEN Charecters Skipped : "<<s2;
  return out;
}
ostream & setHex(ostream & out)
{  out.setf(ios::hex,ios::basefield);
  out.setf(ios::showbase);
  out.setf(ios::uppercase);
  return out;   }
ostream & reSetHex(ostream & out)
{   out.unsetf(ios::hex);
  out.unsetf(ios::showbase);
  out.unsetf(ios::uppercase);
  return out;
}
void main()
{    int i;
    clrscr();
    cout<<skip_10_Char;
    cout<<"\nEnter Value :";
    cin>>i;
    cout<<"\nVulue in Hexadecimal : "<<setHex<<i;
    cout<<"\nOriginal Value : "<<reSetHex<<i;
    getch();
}

* Write a C++ program to demonstrate creation of user defined manipulator
    (i) unparameterised
    (ii) with one parameter
    (iii) with two parameters
    (iv) with three parameters


#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<string.h>
ostream & nline(ostream & o)
{
    o<<"\n";
    return o;
}
ostream & fs(ostream & ost,int slen)
{
    for(int k=0;k<(80-slen)/2;k++)
        ost<<" ";
    return ost;
}
OMANIP (int)middle(int slen)
{
    return OMANIP(int)(fs,slen);
}
struct period
{
    int width,pre;
};
IOMANIPdeclare(period);
ostream & fc(ostream & ost,period peri)
{
    ost.width(peri.width);
    ost.precision(peri.pre);
    ost.setf(ios::right,ios::adjustfield);
    ost.setf(ios::showpoint);
    ost.setf(ios::fixed);
    return ost;
}
OMANIP (period) pda(int width,int pre)
{
    period peri;
    peri.width=width;
    peri.pre=pre;
    return OMANIP(period)(fc,peri);
}
struct des
{
    int width,pre;
    char c;
};
IOMANIPdeclare(des);
ostream & fc(ostream & ost,des d)
{
    ost.width(d.width);
    ost.precision(d.pre);
    ost.fill(d.c);
    ost.setf(ios::right,ios::adjustfield);
    ost.setf(ios::showpoint);
    ost.setf(ios::fixed);
    return ost;
}
OMANIP (des) displays(int width,int pre,char c)
{
    des d;
    d.width=width;
    d.pre=pre;
    d.c=c;
    return OMANIP (des) (fc,d);
}
void main()
{
    clrscr();
    char *heading="Display of Data of Employee Pay";
    char *name[3]={"Nirav","Varun","Shivam"};
    int basic[3]={33333,22222,11111};
    cout<<nline<<nline;
    cout<<middle(strlen(heading))<<heading;
    cout<<nline;
    cout<<middle(strlen(heading));
    cout<<"--------------------------------------";
    cout<<nline;
    cout<<"Name    Basic      DA       Net Salary";
    cout<<nline;
    cout<<"---------------------------------------";
    for(int i=0;i<3;i++)
    {
        cout<<nline;
        cout<<name[i]<<"\t"<<basic[i]<<"\t";
        float da=(basic[i]*0.2);
        cout<<pda(5,2)<<da;
        cout<<"\t"<<displays(10,2,'0');
        cout<<(basic[i]+da);
    }
    getch();
}

* W.A.P  using function in namespace scope

#include<iostream.h>

namespace Functions
{
    int divide(int x,int y) //defination
    {
        return(x/y);
    }
    int prod(int x,int y); //decelaration only
}
int Functions::prod(int x,int y)//qualified
{
    return (x*y);
}
int main()
{
    using namespace Functions;
    cout<<"Division:"<<divide(20,10)<<endl;
    cout<<"Multiplication"<<prod(20,10)<<endl;
    return 0;
}

* W.A.P using function in namespace scope

#include<iostream.h>

namespace Functions
{
    int divide(int x,int y) //defination
    {
        return(x/y);
    }
    int prod(int x,int y); //decelaration only
}
int Functions::prod(int x,int y)//qualified
{
    return (x*y);
}
int main()
{
    using namespace Functions;
    cout<<"Division:"<<divide(20,10)<<endl;
    cout<<"Multiplication"<<prod(20,10)<<endl;
    return 0;
}











































No comments:

Post a Comment