Practical Slip - Shivam1
Practical Slip - Shivam1
A) Write a C++ program to check the maximum and minimum of two integer numbers. (Use Inline function and
Conditional operator)
#include<iostream>
using namespace std;
inline int max (int a, int b) {
return ((a > b) ? a : b);
}
inline int min(int a, int b) {
return ((a < b) ? a : b);
}
int main() {
int a, b;
cout << "Enter 2 numbers" << endl;
cout << "Number 1: ";
cin >> a;
cout << "Number 2: ";
cin >> b;
cout << "The maximum number is: "
<< max(a, b) << endl;
cout << "The minimum number is: "
<< min(a, b) << endl;
return 0;
}
B) Create a C++ class MyFile with data membersfile pointer and filename. Write necessary member functions to
accept and display Files. Overload the following operators: Operator Example Purpose + F3=F1+F2 Concatenate
the contents of file F1 and F2 in F3. ! !F3 Changes the case of alternate characters of file F3.
Ans:
A) Write a PHP script to create a simple calculator that can accept two numbers and perform operations like
add, subtract, and multiplication. (Use the concept of Class)
Ans:
<?php }
class MyCalculator { public function multiply() {
private $_fval, $_sval; return $this->_fval * $this->_sval;
public function __construct( $fval, $sval }
){ public function divide() {
$this->_fval = $fval; return $this->_fval / $this->_sval;
$this->_sval = $sval; }
} }
public function add() { $mycalc = new MyCalculator(12, 6);
return $this->_fval + $this->_sval; echo $mycalc-> add()."\n"; // Displays
} 18
public function subtract() { echo $mycalc-> multiply()."\n"; //
return $this->_fval - $this->_sval; Displays 72
echo $mycalc-> subtract()."\n"; //
Displays 6
echo $mycalc-> divide()."\n"; //
Displays 2
?>