Вы находитесь на странице: 1из 12

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.

Solution:
1 #include<iostream.h>
2
class FLOAT
3
{
4 float data;
5 public:
6 FLOAT(){};
7 FLOAT(float d)
{ data=d;}
8 FLOAT operator+(FLOAT f1);
9 FLOAT operator-(FLOAT f2);
10 FLOAT operator*(FLOAT f3);
11 FLOAT operator/(FLOAT f4);
12 void display();
};
13 FLOAT FLOAT::operator+(FLOAT f1)
14 {
15 FLOAT temp;
16 temp.data=data+f1.data;
return (temp);
17 }
18 FLOAT FLOAT::operator-(FLOAT f2)
19 {
20 FLOAT temp;
21 temp.data=data-f2.data;
return temp;
22 }
23 FLOAT FLOAT::operator*(FLOAT f3)
24 {
25 FLOAT temp;
26 temp.data=data*f3.data;
return temp;
27 }
28 FLOAT FLOAT::operator/(FLOAT f4)
29 {
30 FLOAT temp;
temp.data=data/f4.data;
31 return (temp);
32 }
33 void FLOAT:: display()
34 {
35 cout<<data<<"\n";
}
36 int main()
37 {
38 FLOAT F1,F2,F3,F4,F5,F6;
39 F1=FLOAT(2.5);
F2=FLOAT(3.1);
40
F3=F1+F2;
41 F4=F1-F2;
42
43
44
45
46
47
48 F5=F1*F2;
F6=F1/F2;
49 cout<<" F1 = ";
50 F1.display();
51 cout<<" F2 = ";
52 F2.display();
53 cout<<" F1+F2 = ";
F3.display();
54 cout<<" F1-F2 = ";
55 F4.display();
56 cout<<" F1*F2 = ";
57 F5.display();
cout<<" F1/F2= ";
58 F6.display();
59 return 0;
60 }
61
62
63
64
65
66
O U T P U T
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);

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

Solution:
1 #include<iostream.h>
#include<math.h>
2
#define pi 3.1416
3 class polar
4 {
5 float r,a,x,y;
6 public:
polar(){};
7 polar(float r1,float a1);
8 polar operator+(polar r1);
9 void display(void);
10 };
11
12 polar :: polar(float r1,float a1)
{
13 r=r1;
14 a=a1*(pi/180);
15 x=r*cos(a);
16 y=r*sin(a);
}
17
18 polar polar :: operator+(polar r1)
19 {
20 polar R;
21
22 R.x=x+r1.x;
23 R.y=y+r1.y;
R.r=sqrt(R.x*R.x+R.y*R.y);
24
25
26
27
28
29 R.a=atan(R.y/R.x);
30
31 return R;
}
32
33 void polar::display()
34 {
35 cout<<"radius = "<<r<<"\n angle = "<<a*(180/pi)<<"\n";
36 }
37
38 int main()
{
39 polar p1,p2,p3;
40 float r,a;
41 cout<<" Enter radius and angle : ";
42 cin>>r>>a;
p1=polar(r,a);
43
p2=polar(8,45);
44 p3=p1+p2;
45 cout<<" p1 : \n";
46 p1.display();
47 cout<<" p2 : \n ";
p2.display();
48 cout<<" p3 : \n ";
49 p3.display();
50 return 0;
51 }
52
53
54
55
O U T P U T
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.

Solution:
1 #include<iostream.h>
#include<iomanip.h>
2
3
class mat
4 {
5 float **m;
6 int rs,cs;
7 public:
mat(){}
8 void creat(int r,int c);
9 friend istream & operator >>(istream &,mat &);
10 friend ostream & operator <<(ostream &,mat &);
11 mat operator+(mat m2);
12 mat operator-(mat m2);
mat operator*(mat m2);
13 };
14
15 void mat::creat(int r,int c)
16 {
17 rs=r;
cs=c;
18 m=new float *[r];
19 for(int i=0;i<r;i++)
20 m[i]=new float1;
21 }
22
23 istream & operator>>(istream &din, mat &a)
{
24 int r,c;
25 r=a.rs;
26 c=a.cs;
27 for(int i=0;i<r;i++)
28 {
for(int j=0;j<c;j++)
29 {
30 din>>a.m[i][j];
31 }
32 }
return (din);
33 }
34 ostream & operator<<(ostream &dout,mat &a)
35 {
36 int r,c;
37 r=a.rs;
c=a.cs;
38 for(int i=0;i<r;i++)
39 {
40 for(int j=0;j<c;j++)
41 {
dout<<setw(5)<<a.m[i][j];
42
43 }
dout<<"\n";
44
}
45 return (dout);
46 }
47 mat mat::operator+(mat m2)
48 {
mat mt;
49 mt.creat(rs,cs);
50 for(int i=0;i<rs;i++)
51 {
52 for(int j=0;j<cs;j++)
53 {
mt.m[i][j]=m[i][j]+m2.m[i][j];
54 }
55 }
56 return mt;
57 }
58
mat mat::operator-(mat m2)
59 {
60 mat mt;
61 mt.creat(rs,cs);
62 for(int i=0;i<rs;i++)
63 {
for(int j=0;j<cs;j++)
64 {
65 mt.m[i][j]=m[i][j]-m2.m[i][j];
66 }
67 }
68 return mt;
}
69
70 mat mat::operator*(mat m2)
71 {
72 mat mt;
73 mt.creat(rs,m2.cs);
74
for(int i=0;i<rs;i++)
75 {
76 for(int j=0;j<m2.cs;j++)
77 {
78 mt.m[i][j]=0;
79 for(int k=0;k<m2.rs;k++)
mt.m[i][j]+=m[i][k]*m2.m[k][j];
80 }
81 }
82
83 return mt;
84 }
int main()
85
{
86 mat m1,m2,m3,m4,m5;
87 int r1,c1,r2,c2;
88 cout<<" Enter first matrix size : ";
cin>>r1>>c1;
89 m1.creat(r1,c1);
cout<<"m1 = ";
90
cin>>m1;
91 cout<<" Enter second matrix size : ";
92 cin>>r2>>c2;
93 m2.creat(r2,c2);
94 cout<<"m2 = ";
cin>>m2;
95 cout<<" m1:"<<endl;
96 cout<<m1;
97 cout<<" m2: "<<endl;
98 cout<<m2;
99 cout<<endl<<endl;
if(r1==r2 && c1==c2)
100 {
101 m3.creat(r1,c1);
102 m3=m1+m2;
103 cout<<" m1 + m2: "<<endl;
cout<<m3<<endl;
104 m4.creat(r1,c1);
105
106 m4=m1-m2;
107 cout<<" m1 - m2:"<<endl;
108 cout<<m4<<endl<<endl;
109
110 }
else
111 cout<<" Summation & substraction are not possible n"<<endl
112 <<"Two matrices must be same size for summation & substraction
113 "<<endl<<endl;
114 if(c1==r2)
115 {
m5=m1*m2;
116 cout<<" m1 x m2: "<<endl;
117 cout<<m5;
118 }
119 else
cout<<" Multiplication is not possible "<<endl
120 <<" column of first matrix must be equal to the row of second matrix ";
121 return 0;
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
O U T P U T
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.

Solution:
1 #include<iostream.h>
2
3
4
#include<string.h>
5
#include<stdio.h>
6
7 class string
8 {
9 char str[1000];
10 public:
void input(){gets(str);}
11 int operator==(string s2);
12 };
13 int string::operator==(string s2)
14 {
15 int t= strcmp(str,s2.str);
if(t==0)
16 t=1;
17 else
18 t=0;
19 return t;
}
20
21
int main()
22 {
23
24 char st1[1000],st2[1000];
25 string s1,s2;
26 cout<<" Enter 1st string : ";
s1.input();
27 cout<<" enter 2nd string : ";
28 s2.input();
29
30 if(s1==s2)
31 cout<<" Two strings are equal ";
32 else
cout<<" Two string are not equal ";
33 return 0;
34 }
35
36
37
O U T P U T
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.
Solution:
1 #include<iostream.h>
#include<math.h>
2
#define pi 3.1416
3 class conversion_point
4 {
5 float x,y,r,theta;
6 public:
void set_xy();
7 void set_r_theta();
8 void show_xy();
9 void show_r_theta();
10 void conversion(int t);
11 };
void conversion_point::set_xy()
12 {
13 cout<<"Enter the value of x & y : ";
14 cin>>x>>y;
15 }
void conversion_point::set_r_theta()
16 {
17 cout<<"Enter the value of r & theta :";
18 cin>>r>>theta;
19 theta=(pi/180)*theta;
20 }
21
void conversion_point::show_xy()
22 {
23 cout<<" CERTECIAN FORM :\n"
24 <<" x = "<<x<<"\n"
25 <<" y = "<<y<<"\n";
26 }
void conversion_point::show_r_theta()
27 {
28 cout<<" POLAR FORM :\n"
29 <<" r = "<<r<<"\n"
30 <<" theta = "<<(180/pi)*theta<<" degree \n";
}
31
32 void conversion_point::conversion(int t)
33 {
34 if(t==1)
35 {
36 r=sqrt(x*x+y*y);
37
if(x!=0)
38 {
39 theta=atan(y/x);
40 show_r_theta();
41 }
42
else
43
{
44
45
46
47
48
49
50
51 cout<<" POLAR FORM :\n"
<<" r = "<<r<<"\n"
52 <<" theta = 90 degree\n";
53 }
54
55 }
56 else if(t==2)
{
57
x=r*cos(theta);
58 y=r*sin(theta);
59 show_xy();
60 }
61 }
62
int main()
63 {
64 conversion_point santo;
65 int test;
66 cout<<" press 1 to input certecian point \n"
67 <<" press 2 to input polar point \n "
<<" what is your input ? : ";
68 cin>>test;
69 if(test==1)
70 santo.set_xy();
71 else if(test==2)
santo.set_r_theta();
72 santo.conversion(test);
73
74 return 0;
75 }
76
77
78
79
80
81
82
O U T P U T
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.3400 73 degree

Вам также может понравиться