7.1

Crate a class FLOAT that contains one float data member. Overload all the four arithmetic operators so that they operate on the objects of FLOAT.

Answer
#include<iostream>
using namespace std; 
class FLOAT
{
          float data;
           public:
           FLOAT(){};
           FLOAT(float d)
           { data=d;}
           FLOAT operator+(FLOAT f1);
           FLOAT operator-(FLOAT f2);
           FLOAT operator*(FLOAT f3);
           FLOAT operator/(FLOAT f4);
          void display();
};
FLOAT FLOAT::operator+(FLOAT f1)
{
           FLOAT temp;
           temp.data=data+f1.data;
          return (temp);
}
FLOAT FLOAT::operator-(FLOAT f2)
{
          FLOAT temp;
          temp.data=data-f2.data;
          return temp;
}
FLOAT FLOAT::operator*(FLOAT f3)
{
          FLOAT temp;
      temp.data=data*f3.data;
         return temp;
}
FLOAT FLOAT::operator/(FLOAT f4)
{
             FLOAT temp;
             temp.data=data/f4.data;
             return (temp);
}
void FLOAT:: display()
{
             cout<<data<<"\n";
}
int main()
{
                      FLOAT F1,F2,F3,F4,F5,F6;
                      F1=FLOAT(2.5);
                      F2=FLOAT(3.1);
                      F3=F1+F2;
                      F4=F1-F2;
                      F5=F1*F2;
                      F6=F1/F2;
                      cout<<" F1 = ";
                      F1.display();
                     cout<<" F2 = ";
                     F2.display();
                     cout<<" F1+F2 = ";
                     F3.display();
                    cout<<" F1-F2 = ";
                    F4.display();
                    cout<<" F1*F2 = ";
                    F5.display();
                    cout<<" F1/F2= ";
                    F6.display();
     return 0;
}

OUTPUT

F1 = 2.5
F2 = 3.1
F1+F2 = 5.6
F1-F2 = -0.6
F1*F2 = 7.75
F1/F2= 0.806452

7.2

Design a class Polar which describes a point in the plane using polar coordinates radius and angle. A point in polar coordinates is shown below figure 7.3
Use the overload + operator to add two objects of Polar.
Note that we cannot add polar values of two points directly. This requires first the conversion of points into rectangular coordinates, then adding the respective rectangular coordinates and finally converting the result back into polar coordinates. You need to use the following trigonometric formula:
x = r * cos(a);

polar-coordinates

y = r * sin(a);
a = atan(y/x); //arc tangent
r = sqrt(x*x + y*y);

Answer
#include<iostream>
#include<math>
#define pi 3.1416
using namespace std;
class polar
{
                   float r,a,x,y;
                   public:
                   polar(){};
                   polar(float r1,float a1);
                   polar operator+(polar r1);
                   void display(void);
};
 
polar :: polar(float r1,float a1)
{
               r=r1;
              a=a1*(pi/180);
              x=r*cos(a);
              y=r*sin(a);
}
 
polar polar :: operator+(polar r1)
{
              polar R;
 
              R.x=x+r1.x;
              R.y=y+r1.y;
              R.r=sqrt(R.x*R.x+R.y*R.y);
              R.a=atan(R.y/R.x);
 
    return R;
}
 
void polar::display()
{
            cout<<"radius = "<<r<<"\n angle = "<<a*(180/pi)<<"\n";
}
 
int main()
{
  polar p1,p2,p3;
  float r,a;
  cout<<" Enter radius and angle : ";
  cin>>r>>a;
  p1=polar(r,a);
  p2=polar(8,45);
  p3=p1+p2;
  cout<<" p1 : \n";
  p1.display();
  cout<<" p2 : \n ";
  p2.display();
  cout<<" p3 : \n ";
  p3.display();
  return 0;
}

OUTPUT

Enter radius and angle : 10 45
P1:
radius = 10
angle = 44.999998
P2 :
radius = 8
angle = 44.999998
P3 :
radius = 18
angle = 44.999998

7.3

Create a class MAT of size m * n. Define all possible matrix operations for MAT type objects.

Answer
#include<iostream>
#include<iomanip>
using namespace std; 
class mat
{
               float **m;
               int rs,cs;
               public:
                mat(){}
                void creat(int r,int c);
                friend istream & operator >>(istream &,mat &);
                friend ostream & operator <<(ostream &,mat &);
                mat operator+(mat m2);
                mat operator-(mat m2);
                mat operator*(mat m2);
};
 
void mat::creat(int r,int c)
{
            rs=r;
            cs=c;
            m=new float *[r];
            for(int i=0;i<r;i++)
           m[i]=new float1;
}
 
istream &  operator>>(istream &din, mat &a)
{
         int r,c;
         r=a.rs;
         c=a.cs;
         for(int i=0;i<r;i++)
          {
                        for(int j=0;j<c;j++)
                            {
                                  din>>a.m[i][j];
                            }
          }
     return (din);
}
ostream & operator<<(ostream &dout,mat &a)
{
                int r,c;
                r=a.rs;
                c=a.cs;
                        for(int i=0;i<r;i++)
                {
                     for(int j=0;j<c;j++)
                       {
                                           dout<<setw(5)<<a.m[i][j];
                                 }
                                 dout<<"\n";
                }
 return (dout);
}
mat mat::operator+(mat m2)
{
               mat mt;
               mt.creat(rs,cs);
              for(int i=0;i<rs;i++)
               {
                   for(int j=0;j<cs;j++)
                               {
                              mt.m[i][j]=m[i][j]+m2.m[i][j];
                      }
                }
      return mt;
}
 
mat mat::operator-(mat m2)
{
          mat mt;
          mt.creat(rs,cs);
           for(int i=0;i<rs;i++)
            {
                 for(int j=0;j<cs;j++)
                  {
                        mt.m[i][j]=m[i][j]-m2.m[i][j];
                  }
            }
     return mt;
}
 
mat mat::operator*(mat m2)
{
           mat mt;
                  mt.creat(rs,m2.cs);
 
     for(int i=0;i<rs;i++)
          {
                for(int j=0;j<m2.cs;j++)
                 {
                        mt.m[i][j]=0;
                        for(int k=0;k<m2.rs;k++)
                        mt.m[i][j]+=m[i][k]*m2.m[k][j];
                 }
         }
 
      return mt;
  }
int main()
{
         mat m1,m2,m3,m4,m5;
         int r1,c1,r2,c2;
         cout<<" Enter first matrix size : ";
         cin>>r1>>c1;
         m1.creat(r1,c1);
         cout<<"m1 = ";
         cin>>m1;
         cout<<" Enter second matrix size : ";
         cin>>r2>>c2;
         m2.creat(r2,c2);
         cout<<"m2 = ";
         cin>>m2;
         cout<<" m1:"<<endl;
         cout<<m1;
         cout<<" m2: "<<endl;
         cout<<m2;
         cout<<endl<<endl;
        if(r1==r2 && c1==c2)
          {
                  m3.creat(r1,c1);
                            m3=m1+m2;
                  cout<<" m1 + m2: "<<endl;
                  cout<<m3<<endl;
                 m4.creat(r1,c1);
 
                  m4=m1-m2;
                 cout<<" m1 - m2:"<<endl;
                 cout<<m4<<endl<<endl;
 
        }
    else
     cout<<" Summation & substraction are not possible n"<<endl
         <<"Two matrices must be same size for summation &   substraction "<<endl<<endl;
if(c1==r2)
{
             m5=m1*m2;
             cout<<" m1 x m2: "<<endl;
             cout<<m5;
}
else
cout<<" Multiplication is not possible "<<endl
<<" column of first matrix must be equal to the row of second matrix ";
 return 0;
}

OUTPUT

Enter first matrix size : 2  2

m1 =

1     2

3     4

Enter second matrix size : 2    2

m2 =

5     6

7     8

m1 =

1     2

3     4

m2 =

5     6

7     8

m1+m2:

6       8

10     12

m1-m2:

-4       -4

-4      -4

m1 x m2:

19      22

43     50

7.4

Define a class String. Use overload == operator to compare two strings.

Answer
#include<iostream>
#include<string>
#include<stdio>
using namespace std; 
class string
{
                 char str[1000];
                 public:
                 void input(){gets(str);}
                          int operator==(string s2);
};
int string::operator==(string s2)
{
                      int t= strcmp(str,s2.str);
            if(t==0)
                         t=1;
            else
            t=0;
        return t;
}
 
int main()
{
 
        char st1[1000],st2[1000];
        string s1,s2;
         cout<<" Enter 1st string : ";
         s1.input();
         cout<<" enter 2nd string : ";
         s2.input();
 
        if(s1==s2)
        cout<<" Two strings are equal ";
        else
        cout<<" Two string are not equal ";
    return 0;
}

OUTPUT

Enter 1st string : our sweetest songs tel our saddest thought
enter 2nd string : a burning desire lead to success.
Two string are not equal

7.5

Define two classes Polar and Rectangle to represent points in the polar and rectangle systems. Use conversion routines to convert from one system to the other.

Answer
#include<iostream>
#include<math>
#define pi 3.1416
using namespace std;
class conversion_point
{
                float x,y,r,theta;
                 public:
                  void set_xy();
                  void set_r_theta();
                  void show_xy();
                  void show_r_theta();
                  void conversion(int t);
};
        void conversion_point::set_xy()
{
         cout<<"Enter the value of x & y : ";
         cin>>x>>y;
}
         void conversion_point::set_r_theta()
{
           cout<<"Enter the value of r & theta :";
           cin>>r>>theta;
           theta=(pi/180)*theta;
}
 
         void conversion_point::show_xy()
{
             cout<<" CERTECIAN FORM :\n"
                    <<" x = "<<x<<"\n"
                    <<" y = "<<y<<"\n";
}
void conversion_point::show_r_theta()
{
               cout<<" POLAR FORM :\n"
                     <<" r = "<<r<<"\n"
                     <<" theta = "<<(180/pi)*theta<<" degree \n";
}
 
void conversion_point::conversion(int t)
{
           if(t==1)
            {
                       r=sqrt(x*x+y*y);
 
                      if(x!=0)
                       {
                             theta=atan(y/x);
                             show_r_theta();
                       }
 
                    else
                      {
                              cout<<" POLAR FORM :\n"
                                     <<" r = "<<r<<"\n"
                                    <<" theta = 90 degree\n";
                      }
 
             }
       else if(t==2)
          {
                    x=r*cos(theta);
                    y=r*sin(theta);
                   show_xy();
          }
}
 
int main()
{
               conversion_point santo;
                int test;
               cout<<" press 1 to input certecian point \n"
                     <<" press 2 to input polar point \n "
                     <<" what is your input ? : ";
                    cin>>test;
                    if(test==1)
                    santo.set_xy();
                    else if(test==2)
                    santo.set_r_theta();
                    santo.conversion(test);
 
       return 0;
}

OUTPUT

Press 1 to input certecian point
Press 2 to input polar point
what is your input ? 1
Enter the value of x & y : 4 5
POLAR FORM :
r = 6.403124
theta = 51.340073 degree


Next Previous