Here is the answer for your question
in C++ Programming Language.
Kindly upvote if you find
the answer helpful.
NOTE : I have tested the
class with three data types, int, float and char.
###########################################################################
CODE :
#include<iostream>
using namespace std;
template <typename T>
class Number{
public:
Number(T n){
this->number
= n;
}
T operator+(const Number
&n){
return
this->number + n.number;
}
T operator-(const Number
&n){
return
this->number - n.number;
}
bool operator<(const Number
&n){
return
this->number < n.number;
}
bool operator>(const Number
&n){
return
this->number > n.number;
}
friend ostream
&operator<<(ostream &out,const Number &n){
out <<
n.number;
return
out;
}
friend istream
&operator>>(istream &in,Number &n){
in >>
n.number;
return in;
}
private:
T number;
};
int main(){
//TESTING WITH INT VALUES
Number<int> n1(50);
Number<int> n2(10);
cout << "n1. " << n1 <<
endl;
cout << "n2. " << n2 <<
endl;
cout << "Sum of n1 and n2: " << n1+n2
<< endl;
cout << "Difference of n1 and n2: " <<
n1-n2 << endl;
if(n1 > n2)
cout << n1 << " is
greater than " << n2 << endl;
if(n1 < n2)
cout << n1 << " is
lesser than " << n2 << endl;
cout << "Enter a number: ";
cin >> n1;
cout << "You have entered: " << n1
<< endl;
cout <<
"===============================================" <<
endl;
//TESTING WITH FLOAT VALUES
Number<float> f1(52.3);
Number<float> f2(34.5);
cout << "f1. " << f1 <<
endl;
cout << "f2. " << f2 <<
endl;
cout << "Sum of f1 and f2: " << f1+f2
<< endl;
cout << "Difference of f1 and n2: " <<
f1-f2 << endl;
if(f1 > f2)
cout << f1 << " is
greater than " << f2 << endl;
if(f1 < f2)
cout << f1 << " is
lesser than " << f2 << endl;
cout << "Enter a number: ";
cin >> f1;
cout << "You have entered: " << f1
<< endl;
cout <<
"===============================================" <<
endl;
//TESING WITH CHARACTERS
Number<char> c1('C');
Number<char> c2('D');
cout << "c1. " << c1 <<
endl;
cout << "c2. " << c2 <<
endl;
if(c1 > c2)
cout << c1 << " is
greater than " << c2 << endl;
if(c1 < c2)
cout << c1 << " is
lesser than " << c2 << endl;
cout << "Enter a character: ";
cin >> c1;
cout << "You have entered: " << c1
<< endl;
return 0;
}
|
####################################################################
SCREENSHOTS :
Please see the screenshots of the code below for the
indentations of the code.
#########################################################################
OUTPUT :
Any doubts regarding this can be explained with pleasure
:)