Object Oriented Programming (O.O.P)





(1). write an inline function to find out the average of two numbers.

#include<iostream.h>
#include<conio.h>
class avg
{
            float a,b;
            public:
            void getdata()
            {
                        cout<<"\n\t enter a:-";
                        cin>>a;
                        cout<<"\n\t enter b:-";
                        cin>>b;
            }
            inline float putdata()
            {
                        return ((a+b)/2);
            }
};
Int main()
{
            clrscr();
            avg a1;
            a1.getdata();
            cout<<"\n\t average="<<a1.putdata();
return 0;
}
















(2). W.A.P. to read two matrix of size m*n from the keyboard
and perform arithmetic operation like Addition, Subtraction, multiplication and Division. Display the output in proper matrix format.

#include<iostream.h>       
#include<conio.h>
class matrix
{
            int row,col,**p,**p1,r,c;
            public:
            void get()
            {
                        cout<<"\n\t enter row:-";
                        cin>>row;
                        cout<<"\n\t enter column:-";
                        cin>>col;
            }
            void mat();
            void put();
            void getdata()
            {
                        cout<<"\n\t enter row:-";
                        cin>>r;
                        cout<<"\n\t enter column:-";
                        cin>>c;



}
            void mat1();
            void putdata();
            void add();
            void sub();
            void mul();
            void div();
};
void matrix :: mat()
{
            int i,j;
            p=new int*[row];
            for(i=0;i<row;i++)
            {
                        p=new int*[col];
            }
            for(i=0;i<row;i++)
            {
                        for(j=0;j<col;j++)
                        {
                                    cout<<"\n\t enter data:-";
                                    cin>>p[i][j];
                        }
            }
}
void matrix :: put();


            int i,j;   
           for(i=0;i<row;i++)
            {
                        for(j=0;j<col;j++)
                        {
                                    cout<<" "<<p[i][j];
                        }
                        cout<<"\n";
            }
}
void matrix :: mat1()
{
            int i,j;
            p1=new int*[r];
            for(i=0;i<r;i++)
            {
                        p1=new int*[c];
            }
            for(i=0;i<r;i++)
            {
                        for(j=0;j<c;j++)
                        {
                                    cout<<"\n\t enter data:-";
                                    cin>>p1[i][j];
                        }

}
}
void matrix :: putdata()
{
            int i,j;
            for(i=0;i<r;i++)
            {
                        for(j=0;j<c;j++)
                        {
                                    cout<<" "<<p1[i][j];
                        }
                        cout<<"\n";
            }
}
void matrix :: add()
{
            int i,j;
            if(row==r && col==c)
            {
                        for(i=0;i<r;i++)
                        {
                                    for(j=0;j<c;j++)
                                    {
                                                cout<<" "<<p[i][j]+p1[i][j];
                                    }
                        cout<<"\n";
                       

}
            }
}
void matrix :: sub()
{
            int i,j;
            if(row==r && col==c)
            {
                        for(i=0;i<r;i++)
                        {
                                    for(j=0;j<c;j++)
                                    {
                                                cout<<" "<<p[i][j]-p1[i][j];
                                    }
                        cout<<"\n";
                        }
            }
}
void matrix :: mul()
{
            int i,j;
            if(row==r && col==c)
            {
                        for(i=0;i<r;i++)
                        {
                                    for(j=0;j<c;j++)


                                    {


                                                cout<<" "<<p[i][j]*p1[i][j];
                                    }
                        cout<<"\n";
                        }
}
}void matrix :: div()
{
            int i,j;
            if(row==r && col==c)
            {
                        for(i=0;i<r;i++)
                        {
                                    for(j=0;j<c;j++)
                                    {
                                                cout<<" "<<p[i][j]/p1[i][j];
                                    }
                        cout<<"\n";
                        }
            }
};
Int main()
{
            clrscr();


            matrix m;
            cout<<"\n\t matrix 1...\n";
            m.get();
            m.mat();
            m.put();
            cout<<"\n\t matrix 2...\n";
            m.getdata();
            m.mat1();
            m.putdata();
            cout<<"\n\t addition....\n\n";
            m.add();
            cout<<"\n\t subtraction...\n\n";
            m.sub();
            cout<<"\n\t multiplication...\n\n";
            m.mul();
            cout<<"\n\t division.....\n\n";
            m.div();
            return 0;
}



(3). W.A.P. to find area of rectangle, triangle and square using the concept of function overloading.

#include<iostream.h>
#include<conio.h>
class area1
{
             public:
             float area(float a,float b);
             int area(int l,int b,int h);
             long area(long l);
};
float area1 :: area(float a,float b)
{
            return 0.5*a*b;
}
int area1 ::area(int l,int b,int h)
{
            return l*b*h;
}
long area1 :: area(long l)
{
            return l*l;
};
Int main()

{
           
clrscr();
            area1 a;
            cout<<"\n\t area of triangle="<<a.area(12.10,10.5);
            cout<<"\n\t area of rectangle="<<a.area(10,8,6);
            cout<<"\n\t area of square="<<a.area(14l);
            return 0;
}









 (4). W.A.P.  to sorting elements in ascending and descending order.

#include<iostream.h>
#include<conio.h>
class sort
{
            int i,j,a[100],no,temp;
            public:

            void getdata()
            {
                        cout<<"\n\t enter no:-";
                        cin>>no;
                        for(i=0;i<no;i++)
                        {
                                    cout<<"\n\t a["<<i<<"]=";
                                    cin>>a[i];
                        }
            }
            void asce();
            void desc();
};
void sort :: asce()
{
            cout<<"\n\t ascending order.....\n";
            for(i=0;i<no;i++)
           
{


                        for(j=0;j<=i;j++)
                        {
                                      if(a[i]<a[j])
                                      {
                                                            temp=a[i];
                                                            a[i]=a[j];
                                                            a[j]=temp;
                                      }
                        }
            }
            for(i=0;i<no;i++)
            {
                        cout<<"\n\t "<<a[i];
            }
}
void sort :: desc()
{
            cout<<"\n\t descending order.....\n";
            for(i=0;i<no;i++)
            {
                        for(j=0;j<=i;j++)
                        {
                                      if(a[i]>a[j])
                                      {
           
                                                            temp=a[i];
                                                            a[i]=a[j];


                                                            a[j]=temp;
                                      }
                        }
            }
            for(i=0;i<no;i++)
            {
                        cout<<"\n\t "<<a[i];
            }
};
Int main()
{
            clrscr();
            sort s;
            s.getdata();
            s.asce();
            s.desc();
            return 0;
}
















(5). W.A.P. a program to find the Fibonacci series for any no using class.
#include<iostream.h>
#include<conio.h>
class fibo        
{
            int i,a,b,c,no;
            public:
            fibo()
            {
                        a=1;
                        b=0;
                        c=1;
            }
            void getdata();
            void putdata();
};
void fibo :: getdata()
{
            cout<<"\n\t enter no:-";
            cin>>no;
}
void fibo :: putdata()
{
            for(i=1;i<=no;i++)
            {


                        cout<<"\n\t"<<c;
a=b;
                        b=c;
                        c=a+b;
            }
};
Int main()
{
            clrscr();
            fibo f;
            f.getdata();
            f.putdata();
            return 0;
}




(6). create two classes DM and DB which store of the  value of distances. DM stores distance in feet and inches. W.A.P. that can add one object of DM with another object of DB. use a friend to carry out the addition operation. the object that stores the result may be a DM object or DB object,depending on the units in which the result is required. the display should be in the format of feet and inches or meters and centimeters. depending on the object of display.

#include<iostream.h>
#include<conio.h>
class db;
class dm
{
            int meter ,centem;
            public:
            void getdm()
            {
                        cout<<"\n\t enter meter :";
                        cin>>meter;
                        cout<<"\n\t enter centimeter:";
                        cin>>centem;
            }
            void showdm()
            {
                        cout<<"\n\t===distance in meter and centimeter===:";
                        cout<<"\n\n\t "<<meter<<" meter and "<<centem<<" centimeter ";
           


}          
friend void setdata(dm,db) ;
};
class db
{
            int feet,inch;
            public:
            void getdb()
            {
                        cout<<"\n\tenter feet:";
                        cin>>feet;
                        cout<<"\n\t enter inch:";
                        cin>>inch;
            }
            void showdb()
            {
                        cout<<"\n\t===distance in feet and inches===:";
                        cout<<"\n\n\t "<< feet <<" feet and "<< inch <<" inches ";
            }
            friend void setdata(dm,db);
};
void setdata(dm m ,db b)
{
            int cent;
            cent=m.centem+(100*m.meter);
            b.feet=cent/30;
           

b.inch=((cent%30)/2.5);         
cout<<"\n\n\t"<< b.feet <<" feet and "<< b.inch <<" inches";
};
int main()
{
            clrscr();
            dm d1;
            db d2;
            d1.getdm();
            d2.getdb();
            d1.showdm();
            d2.showdb();
            cout<<"\n\n\t====addition of distance=====:";
            setdata(d1,d2);
            return 0;
}


(7). W.A.P to perform addition of two values of different classes.
#include<iostream.h>
#include<conio.h>
class abc;
class xyz
{
            int a;
            public:
            void getdata()
            {
            cout<<"\n\t enter a:-";
                        cin>>a;
            }
            friend void add(xyz x1,abc a1);
};
class abc
{
            int b;
            public:
            void getdata()
           {
                        cout<<"\n\t enter a:-";
                        cin>>b;
            }
            friend void add(xyz x1,abc a1);

};
void add(xyz x1,abc a1)
{
            int sum;
            sum=x1.a+a1.b;
            cout<<"\n\t sum="<<sum;
};
Int main()
{
            clrscr();
            xyz xx;
            abc aa;
            xx.getdata();
            aa.getdata();
            add(xx,aa);
            return 0;
  }

8). W.A.P which display the concept of copy constructor and constructor overloading.

#include<iostream.h>
#include<conio.h>
class code
{
            int id,id1,id2;
            public:
            code(int a)
            {
                        id=a;
            }
            code(int b,int c)
            {
                        id1=b;
                        id2=c;
            }
            code(code &x1,code &x2)
            {
                        id=x2.id;
                        id1=x1.id1;
                        id2=x1.id2;
            }
            void display()
            {

                        cout<<"\n\t id="<<id;

                        cout<<"\n\t id1="<<id1;
                        cout<<"\n\t id2="<<id2;
            }
};
Int main()
{
            clrscr();
            code A(100);
            code B(200,300);
            code D(B,A);
            D.display();
            return 0;
}
9). WAP display total marks and percentage of student using multilevel inheritance.

#include<iostream.h>
#include<conio.h>
class student  
{
            protected:
            int roll_no;
            char name[20];
            public:
            void get()
            {
                        cout<<"\n\t enter roll no:-";
                        cin>>roll_no;
                        cout<<"\n\t enter name:-";
                        cin>>name;
            }
};
class test:public student
{
            protected:
            int sub1,sub2,sub3,sub4,sub5,i;
            float p,total;
            public:


           
void get_mark()         
{
                        cout<<"\n\t enter mark 1=";
                        cin>>sub1;
                        cout<<"\n\t enter mark 2=";
                        cin>>sub2;
                        cout<<"\n\t enter mark 3=";
                        cin>>sub3;
                        cout<<"\n\t enter mark 4=";
                        cin>>sub4;
                        cout<<"\n\t enter mark 5=";
                        cin>>sub5;
                        total=sub1+sub2+sub3+sub4+sub5;
                        p=total/5;
            }
};
class result:public test
{
            public:
            void display()
            {
                        get();
                        get_mark();
            }
            float final()
            {
                       

                        cout<<"\n\t roll no="<<roll_no;
cout<<"\n\t name="<<name;
                        cout<<"\n\t total="<<total;
                        cout<<"\n\t per="<<p;
                        return 0;
            }
};
int main()
{
            clrscr();
            result r;
            r.display();
            r.final();
            return 0;
}






















10). W.A.P. to overload ++ operator (prefix &postfix)and - (unary minus) operator.

#include<iostream.h>
#include<conio.h>
class overload
{
            int a,b,c;
            public:
            void getdata()
            {
                       cout<<"\n\t enter a=";
                        cin>>a;
                        cout<<"\n\t enter b=";
                        cin>>b;
                        cout<<"\n\t enter c=";
                        cin>>c;
            }
            void operator -()
            {
                        a=-a;
                        b=-b;
                        c=-c;
            }
            void display()


          {
                        cout<<"\n\t a="<<a;
                        cout<<"\n\t b="<<b;
                        cout<<"\n\t c="<<c;
            }
            friend overload operator ++(overload oo);
};
overload operator ++(overload oo)
{
            overload oo1;
            oo1.a=++oo.a;
            oo1.b=++oo.b;
            oo1.c=oo.c++;
            return oo1;
};


Int main()
{
            overload o1,o2;
            o1.getdata();
            o2=operator ++(o1);
            cout<<"\n\t ++ operator is overload...\n";
            o2.display();
            -o1;
            cout<<"\n\t - operator is overload...\n";
            o1.display();
            return 0;        
}





















(11). W.A.P. to compare and concatenate string using == and  + operator overloading.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
            char *p;
            int len;
            public:
            string()
            {
                        p=0;
                        len=0;
            }
            string(char *s)
            {


                        len=strlen(s);
                        p=new char[len+1];
                        strcpy(p,s);
            }
            friend string operator +(string &s,string &t);
            friend int operator ==(string &s,string &t);
            friend void show(string s);
};
string operator +(string &s,string &t)
{
            string te;
            te.len=s.len+t.len;
            te.p=new char[te.len+1];
            strcpy(te.p,s.p);
            strcat(te.p,t.p);
            return te;
}
int operator ==(string &s,string &t)

{
            int m,n;
            m=strlen(s.p);
            n=strlen(t.p);
            if(m==n)
            {
                        return 1;
            }
            else
            {
                        return 0;
            }
}
void show(string s)
{
            cout<<"\n\t string = "<<s.p;
};
Int main()

{
            char *s;
            clrscr();
            cout<<"\n\t enter first string=";
            cin>>s;
            string s1(s);
            cout<<"\n\t enter second string=";
            cin>>s;
            string s2(s);
            string s3;
            s3=operator +(s1,s2);
            cout<<"\n\t s1.....";
            show(s1);
            cout<<"\n\t s2.....";
            show(s2);
            cout<<"\n\t s3.....";
            show(s3);
            if(s1==s2)

            {
                        cout<<"\n\t both string is equal length..";
            }
            else
            {
                        cout<<"\n\t both string is not equal length..";
            }
            Return 0;

}

12). WAP to overloading += operator.

#include<iostream.h>
#include<conio.h>
class overload
{
            int a,b,c;
            public:
            overload(int aa,int bb,int cc)
            {
                        a=aa;
                        b=bb;
                        c=cc;
            }
            void operator +=(overload oo)
            {
                        a=a+=oo.a;
                        b=b+=oo.b;
                        c=c+=oo.c;
            }
            void display()
            {
                        cout<<"\n\t a="<<a;
                        cout<<"\n\t b="<<b;
                        cout<<"\n\t c="<<c;
            }
};
Int main()
{
            int aa,bb,cc;
            clrscr();
            cout<<"\n\t enter a=";
            cin>>aa;
            cout<<"\n\t enter b=";
            cin>>bb;
            cout<<"\n\t enter c=";
            cin>>cc;
            overload o1(aa,bb,cc);
            o1.display();
            overload o2(2,2,2);
            o2.operator+=(o1);
            cout<<"\n\t += operator is overload...\n";
            o2.display();
            return 0;
}
13). W.A.P to create a class with at least two data member and Overload  >> and <<  Operators.

#include<iostream.h>
#include<conio.h>
class abc
{
            int a[3];
            public:
            friend istream & operator >>(istream &d,abc &b);
            friend ostream & operator <<(ostream &out,abc &b);
};
istream & operator >>(istream &d,abc &b)
{
             for(int i=0;i<3;i++)
             {
                        cout<<"\n\t enter m:-";
                        d>>b.a[i];
             }
             return d;
}
ostream & operator <<(ostream &out, abc &b)
{
            out<<"("<<b.a[0];
            for(int i=1;i<3;i++)
            {


                        out<<","<<b.a[i];
            }
            out<<")";
            return out;
};
Int main()
{
            clrscr();
            abc m;
            cin>>m;
            cout<<"\n\t m="<<m;
            return 0;

}
14). WAP that consist of two classes time 12 and time 24. The first maintains a .
       Time  In 12 hours basis. Where as the other one maintains the time in 24
       hours basis.  Provide conversion function to carry out conversion from             
       object of one type to  Another.

#include<iostream.h>
#include<conio.h>
class time12;
class time24
{
            int h;
            public:
            void get_time()
            {
                        cout<<"\n\t enter h=";
                        cin>>h;
            }
            void cal()
            {
                        if(h<12)
                        {
                                    h=h+12;
                                    cout<<"\n\t time="<<h<<"pm";
                        }
                        else
                        {
                                    cout<<"\n\t time="<<h<<"am";

                        }
            }
};
class time12
{
            int h1;
            public:
            void get_time()
            {
                        cout<<"\n\t enter h=";
                        cin>>h1;
            }
            void cal()
            {
                        if(h1>12)
                        {
                                    h1=h1-12;
                                    cout<<"\n\t time="<<h1<<"pm";
                        }
                        else
                        {
                                    cout<<"\n\t time="<<h1<<"am";
                        }
            }
};
Int main()
{

            clrscr();
            time12 t;
            time24 t1;
            t.get_time();
            t.cal();
            t1.get_time();
            t1.cal();
            return 0;
}


16). Write a binary file contains information of products pno, pname, qty, price. WAP to copy the contains of this file to another file where qty>10.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<iomanip.h>
int main()
{
  int e_code[3]={1,2,3};
  int e_sal[3]={7000,5000 ,3000};
  char e_name[3][10]={"sagar","satish","sandip"};
  ofstream fout;
  fout.open("d:/binary");
  fout.write((char *) & e_code, sizeof(e_code));
  fout.write((char *) & e_name, sizeof(e_name));
  fout.write((char *) & e_sal, sizeof(e_sal));
  fout.close();
  ifstream fin;
  fin.open("d:/binary");
  fin.read((char *) & e_code,sizeof(e_code));
fin.read((char *) & e_name,sizeof(e_name));
  fin.read((char *) & e_sal,sizeof(e_sal));
  cout<<"*********************************************"<<endl;
  for(int i=0;i<3;i++)
  {
    cout<<setw(5)<<e_code[i];
    cout<<setw(15)<<e_name[i];
    cout<<setw(5)<<e_sal[i]<<endl;
  }
  cout<<"*********************************************"<<endl;
  for(int i=0;i<3;i++)
  {
     if(e_sal[i]>5000)
     {
            fout.open("d:/binary1");
            fout.write((char *) & e_code, sizeof(e_code));
            fout.write((char *) & e_name, sizeof(e_name));
            fout.write((char *) & e_sal, sizeof(e_sal));
     }
  }
    cout<<"\n
    fout.close();
 fin.close();
  getch();
  return 0;
}

Share this

Related Posts

Comment Here