-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.quadratic oop.cpp
107 lines (102 loc) · 2.2 KB
/
4.quadratic oop.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//4
#include<iostream>
#include<math.h>
using namespace std;
class quad
{
private:
int a,b,c;
public:
//-------------------------------------------------------------------
quad()
{
a=0;
b=0;
c=0;
}
//-------------------------------------------------------------------
quad(int x,int y, int z)
{
a=x;
b=y;
c=z;
}
//-------------------------------------------------------------------
quad operator + (quad d)
{
quad temp;
temp.a=a+d.a;
temp.b=b+d.b;
temp.c=c+d.c;
return(temp);
}
//-------------------------------------------------------------------
void compute()
{
int x,ans=0;
cout<<"\nEnter the value of x - ";
cin>>x;
ans = (a*x*x) + (b*x) + (c);
cout<<"\nThe ans is - "<<ans;
}
//-------------------------------------------------------------------
void soln()
{
float x1=0,x2=0,z=0,d=0;
z=(b*b)-(4*a*c);
d=sqrt(z);
x1 =((-b)+(d))/(2*a);
x2=((-b)-(d))/(2*a);
cout<<"\nThe two roots are - \nx1="<<x1<<"\nx2="<<x2;
}
//-------------------------------------------------------------------
friend ostream &operator << (ostream&write,quad&t);
friend istream &operator >> (istream&read,quad&m);
};
//-------------------------------------------------------------------
ostream &operator << (ostream&write,quad&t)
{
write<<t.a<<"x^2 + "<<t.b<<"x + "<<t.c<<"\n";
}
//-------------------------------------------------------------------
istream &operator >> (istream&read,quad&m)
{
cout<<"\nEnter values of a,b,c : ";
read>>m.a>>m.b>>m.c;
}
//-------------------------------------------------------------------
main()
{
quad q1;
cout<<q1; //default constructor values print
quad q2(1,2,3);
cout<<q2; //parameterize constructor values print
quad q3,q4,q5(0,0,0);
int n,k;
do
{
cout<<"\nEnter 1.ENTER 2.DISPLAY 3.ADD 4.COMPUTE 5.TWO SOLUTIONS - ";
cin>>n;
switch(n)
{
case 1:
cin>>q3>>q4;
break;
case 2:
cout<<q3<<q4;
break;
case 3:
q5=q3+q4;
cout<<q5;
break;
case 4:
q3.compute();
break;
case 5:
q3.soln();
break;
}
cout<<"\nEnter 1=continue 2=exit - ";
cin>>k;
}while(k!=2);
}