Write a PHP calculator class which will accept two values as arguments, then add them, subtract them, multiply them together, or divide them on request
- برمجة بي اتش بي
- 2021-09-09
- mhanasmh00489829403
الأجوبة
<?php
class MyCalculator {
private $_fval, $_sval;
public function __construct( $fval, $sval ) {
$this->_fval = $fval;
$this->_sval = $sval;
}
public function add() {
return $this->_fval + $this->_sval;
}
public function subtract() {
return $this->_fval - $this->_sval;
}
public function multiply() {
return $this->_fval * $this->_sval;
}
public function divide() {
return $this->_fval / $this->_sval;
}
}
$mycalc = new MyCalculator(12, 6);
echo $mycalc-> add()."\n"; // Displays 18
echo $mycalc-> multiply()."\n"; // Displays 72
echo $mycalc-> subtract()."\n"; // Displays 6
echo $mycalc-> divide()."\n"; // Displays 2
?>
Sample Output:
18 72 6 2
أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال