class in c + + function to categorize the variables, class is almost the same as the struct, the difference is the class name can be changed while in stuctr can not.
#include
#include
#include
class Persamaan{
public:
Persamaan();
void hitung();
private:
double a,b,c;
double diskriminan,x1,x2,re,im;
};
int main()
{
Persamaan x;x.hitung();
getch();
return 0;
}
Persamaan::Persamaan(){
cout << "Persamaan Kuadrat" << endl;
cout << "a : ";
cout << "b : ";
cout << "c : ";
}
void Persamaan::hitung (){
// hitung diskriminan
diskriminan=b*b-4*a*c;
if(diskriminan > 0)
{
x1 = (-b + sqrt(diskriminan))/(2*a);
x2 = (-b - sqrt(diskriminan))/(2*a);
cout << "Akar real : " << endl;
cout << "x1 : " << x1 << endl;
cout << "x2 : " << x2 << endl;
else
if(diskriminan ==0)
{
x1=-b/(2*a);
x2=x1;
cout << "Akar kembar : " << endl;
else //diskriminan <0
{
re= -b/(2*a);
im= sqrt(fabs(diskriminan))/(2*a);
cout << "Akar kompleks : " << endl;
cout << "x1 : " << re <<" + " << im << endl;
cout << "x2 : " << re <<" - " << im << endl;
}
Tidak ada komentar:
Posting Komentar