0% found this document useful (0 votes)
10 views2 pages

Practical Slip - Shivam1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Practical Slip - Shivam1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Practical Slip – 1

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:

#include <stdio.h> if (isupper(ch))


#include <conio.h> fputc(tolower(ch), f4.fp);
#include <iostream> else if (islower(ch))
using namespace std; fputc(toupper(ch), f4.fp);
#include <string.h> else
#define MAXSIZE (10) fputc(ch, f4.fp);
class myfile }
{ fclose(fp);
FILE *fp; fclose(f4.fp);
char fn[MAXSIZE]; remove("abc.txt");
public: rename("sy.txt", "abc.txt");
myfile(const char *fname) }
{ myfile myfile::operator+(myfile f2)
strcpy(fn, fname); {
} myfile f3("abc.txt");
myfile operator+(myfile); fp = fopen(fn, "r");
void operator!(); f2.fp = fopen(f2.fn, "r");
void display(); f3.fp = fopen(f3.fn, "w");
}; char ch;
void myfile::display() while ((ch = fgetc(fp)) != EOF)
{ {
fp = fopen(fn, "r"); fputc(ch, f3.fp);
char ch; }
while ((ch = fgetc(fp)) != EOF) fclose(fp);
char ch; while ((ch = fgetc(f2.fp)) != EOF)
fp = fopen(fn, "r"); {
f4.fp = fopen(f4.fn, "w"); fputc(ch, f3.fp);
while ((ch = fgetc(fp)) != EOF) }
{ _fcloseall();
return f3;
}
int main()
myfile f1("xyz.txt");
myfile f2("lmn.txt");
myfile f3("abc.txt");
cout << "first file \n";
f1.display();
cout << "\nsecond file \n";
f2.display();
f3 = f1 + f2;
cout << "\nAfter concatnation file is ";
f3.display();
cout << "\nAfter changes case \n";
f3;
f3.display();
return 0;
}

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
?>

You might also like