1) mencari persaan garis
#include <iostream.h>
#include <conio.h>
class persgaris {
friend ostream& operator<<(ostream&, persgaris&);
friend istream& operator>>(istream&, persgaris&);
public:
persgaris();
float gradien();
float konstanta();
private:
float x1, y1, x2, y2;
};
persgaris::persgaris(){
cout<<"program persamaan garis dengan 2 titik \n";
}
ostream& operator<<(ostream& out, persgaris& j){
out << "gradien = "<<j.gradien()<<endl;
out << "y = "<<j.gradien()<<"x + "<<j.konstanta()<<endl;
return out;
}
istream& operator>>(istream& in, persgaris& j){
cout << "x1 : "; in >> j.x1;
cout << "y1 : "; in >> j.y1;
cout << "x2 : "; in >> j.x2;
cout << "y2 : "; in >> j.y2;
return in;
}
float persgaris::gradien(){
return ((y2-y1)/(x2-x1));
}
float persgaris::konstanta(){
return (y1-(x1*gradien()));
}
void main(){
persgaris j;
cin >> j;
cout << j;
getch();
}

Tidak ada komentar:
Posting Komentar