Continue to Site

# How to change to OOP style??

Status
Not open for further replies.

#### Ashieboy

##### Member level 1
Code:
#include<iostream>
#include<cmath>
using namespace std;

float f (float x)
{
float fx1;
fx1 = pow(x,3) - pow(x,2) + (1.4*x) + 7.8;
return (fx1);
}

main()
{
float x1,x2,x3;
int count = 0;
int iter;

cout <<"Enter x1 = ";
cin >> x1;
cout <<"Enter x2 = ";
cin >> x2;
cout <<"Enter number of iterations = ";
cin >> iter;

//  int fx1 = (x1^3) - (x1^2) + (1.4x1) + 7.8;
//  int fx2 = (x2^3) - (x2^2) + (1.4x1) + 7.8;
do
{
if(count == iter)
{
break;
}

x3 = (x1 + x2)/2;

cout <<"x1 = " << x1 <<" | x2="<< x2 <<" | x3=" << x3 <<" | " << "  f(x1)=" << f(x1) << " |  f(x2)=" << f(x2) << " |  f(x3)=" << f(x3) << endl << endl;

//float temp1 = f(x1);
//float temp2 = f(x3);
if( f(x1) * f(x3) < 0 )
{
x2 = x3;
}
else
{
x1 = x3;
}
count++;
}
while ( abs(x1 - x2) < 0.0000001 || f(x3) == 0 );

}

was trying to do it in OOP style... can anyone link me to a tutorial on it??

Last edited by a moderator:

You could implement a class with a static methods, which performs the desired calculation and formatted output, although in this case it's unclear to what advantage such an endeavor would yield.

There are numerous C++ tutorials available online, as well as a variety of MOOCs.

BigDog

Code:
#include<iostream>
#include<cmath>
using namespace std;

class Bisection{private:float f, float x;
public :float fx1,x1,x2,x3;
int count, int iter};
getter{
cout <<"Enter x1 = ";
cin >> x1;
cout <<"Enter x2 = ";
cin >> x2;
cout <<"Enter number of iterations = ";
cin >> iter;}

f(x3) << fx1 = pow(x,3) - pow(x,2) + (1.4*x) + 7.8

main()
{
float x1,x2,x3;
int count = 0;
int iter;

do
{
if(count == iter)
{
break;
}
x3 = (x1 + x2)/2;

cout <<"x1 = " << x1 <<" | x2 = "<< x2 <<" | x3 = " << x3 <<" | "
<< "  f(x1) = " << f(x1) << " |  f(x2) = " << f(x2) << " |  f(x3) = "
<< endl << endl;

if( f(x1) * f(x3) < 0 )
{
x2 = x3;
}
else
{
x1 = x3;
}
count++;
}
while ( abs(x1 - x2) < 0.0000001 || f(x3) == 0 );

}

following the net but cant seem to get it right...

You need to review or study a C++ text.

Code C++ - [expand]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
#include<iostream>
#include<cmath>
using namespace std;

class Bisection
{
private:
float f, x;

public:
float fx1,x1,x2,x3;
int count, iter;

public:

float func(float x)
{
float fx1;
fx1 = pow(x,3) - pow(x,2) + (1.4*x) + 7.8;
return (fx1);
}

void getInput()
{
cout <<"Enter x1 = ";
cin >> x1;
cout <<"Enter x2 = ";
cin >> x2;
cout <<"Enter number of iterations = ";
cin >> iter;
}

void calcResult()
{
do
{
if(count == iter)
{
break;
}

x3 = (x1 + x2)/2;

cout << endl << "x1 = " << x1 <<" | x2="<< x2 <<" | x3=" << x3 <<" | " << "  f(x1)=" << func(x1) << " |  f(x2)=" << func(x2) << " |  f(x3)=" << func(x3) << endl << endl;

//float temp1 = f(x1);
//float temp2 = f(x3);
if( func(x1) * func(x3) < 0 )
{
x2 = x3;
}
else
{
x1 = x3;
}
count++;
}while ( abs(x1 - x2) < 0.0000001 || func(x3) == 0 );

}

};

int main(void)
{

Bisection bisect;

bisect.getInput();

bisect.calcResult();

return 0;

}

Compiles without warnings or errors, produces the following when ran:

Enter x1 = 9
Enter x2 = 5
Enter number of iterations = 100

x1 = 9 | x2=5 | x3=7 | f(x1)=668.4 | f(x2)=114.8 | f(x3)=311.6

Hope the above example points you in the right direction.

BigDog

- - - Updated - - -

Alternatively, you could define the func() method as private, if you wish to restrict its use to within the class.

Or you could define the methods as static, which would alleviate the need to instantiate a Bisection object before utilizing the methods.

- - - Updated - - -

And, if you wish to retain the input and output tasks within main(), you can utilize the same class as so:

Code C++ - [expand]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
#include<iostream>
#include<cmath>
using namespace std;

class Bisection
{
private:
float f, x;

public:
float fx1,x1,x2,x3;
int count, iter;

public:

float func(float x)
{
float fx1;
fx1 = pow(x,3) - pow(x,2) + (1.4*x) + 7.8;
return (fx1);
}

public:

void getInput()
{
cout <<"Enter x1 = ";
cin >> x1;
cout <<"Enter x2 = ";
cin >> x2;
cout <<"Enter number of iterations = ";
cin >> iter;
}

void calcResult()
{
do
{
if(count == iter)
{
break;
}

x3 = (x1 + x2)/2;

cout << endl <<"x1 = " << x1 <<" | x2="<< x2 <<" | x3=" << x3 <<" | " << "  f(x1)=" << func(x1) << " |  f(x2)=" << func(x2) << " |  f(x3)=" << func(x3) << endl << endl;

//float temp1 = f(x1);
//float temp2 = f(x3);
if( func(x1) * func(x3) < 0 )
{
x2 = x3;
}
else
{
x1 = x3;
}
count++;
}
while ( abs(x1 - x2) < 0.0000001 || func(x3) == 0 );
}

};

int main(void)
{

Bisection bisect;

cout <<"Enter x1 = ";
cin >> bisect.x1;
cout <<"Enter x2 = ";
cin >> bisect.x2;
cout <<"Enter number of iterations = ";
cin >> bisect.iter;

do
{
if(bisect.count == bisect.iter)
{
break;
}

bisect.x3 = (bisect.x1 + bisect.x2)/2;

cout << endl <<"x1 = " << bisect.x1 <<" | x2="<< bisect.x2 <<" | x3=" << bisect.x3 <<" | " << "  f(x1)=" << bisect.func(bisect.x1) << " |  f(x2)=" << bisect.func(bisect.x2) << " |  f(x3)=" << bisect.func(bisect.x3) << endl << endl;

//float temp1 = f(x1);
//float temp2 = f(x3);
if( bisect.func(bisect.x1) * bisect.func(bisect.x3) < 0 )
{
bisect.x2 = bisect.x3;
}
else
{
bisect.x1 = bisect.x3;
}
bisect.count++;
}
while ( abs(bisect.x1 - bisect.x2) < 0.0000001 || bisect.func(bisect.x3) == 0 );

return 0;

}

Same results, compiled without warnings or errors:

Enter x1 = 9
Enter x2 = 5
Enter number of iterations = 100

x1 = 9 | x2=5 | x3=7 | f(x1)=668.4 | f(x2)=114.8 | f(x3)=311.6

However, in the example above, you will need to ensure the func() remains public so as to provide access to it from within main().

BigDog

Ashieboy

### Ashieboy

Points: 2
Code:
#include<iostream>
#include<cmath>
using namespace std;

class Bisection{private:float f, x;
float fx1,x1,x2,x3;
int count, iter;
public :float func(float x)
{float fx1;
fx1 = pow(x,3) - pow(x,2) + (1.4*x) + 7.8;
return (fx1);}
void getInput(){ cout <<"Enter x1 = ";
cin >> x1;
cout <<"Enter x2 = ";
cin >> x2;
cout <<"Enter number of iterations = ";
cin >> iter;}
void calcResult(){do
{
if(count == iter)
{
break;
}
x3 = (x1 + x2)/2;
cout << endl << "x1 = " << x1 <<" | x2 = "<< x2 <<" | x3 = "
<< x3 <<" | " << "\nf(x1) = " << func(x1) << " |  f(x2) = "
<< func(x2) << " |  f(x3) = " << func(x3) << endl << endl;

//float temp1 = f(x1);
//float temp2 = f(x3);
if( func(x1) * func(x3) < 0 )
{
x2 = x3;
cout<< " The value of the root is = " <<x3<<endl;
}
else
{
x1 = x3;
cout<< " The value of the root is = " <<x3<<endl;
}
count++;}
while ( abs(x1 - x2) < 0.0000001 || func(x3) == 0 );}};

int main(void)
{
cout<<"\t**********************************************************"<<endl;
cout<<"\t\tFinding roots by using Bisection Method "<<endl;
cout<<"\t**********************************************************"<<endl;
cout<<"\t\tThe Question is (x^3)-(4.3x^2)+(1.4x)+7.8 = 0"<<endl;
cout<<"\t**********************************************************"<<endl;
cout<<"\t\tThe lowest iteration number must equal to f(c)"<<endl;
cout<<"\t**********************************************************"<<endl;
Bisection bisect;
bisect.getInput();
bisect.calcResult();
return 0;

}

why the program does not give the real number of root??
it seem like it does not do the if.... else.... command....

Is this a homework or school related project?

There appears to be an issue with your Bisection Root algorithm, as the results produced are consistent with your original code.

I've neither had the time or opportunity to examine it, perhaps later tonight I can reexamine your algorithm and offer some advice.

BigDog

Status
Not open for further replies.