Question

In: Computer Science

Write a template class Number with the following features Overload following operators for the template class...

Write a template class Number with the following features
Overload following operators for the template class
+
-
<
>
Overload << and >> operators for the ostream and istream against this class.
Write a main function to demonstrate the functionality of each operator.

Solutions

Expert Solution

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 :)


Related Solutions

Write a template class Number with the following features Overload following operators for the template class...
Write a template class Number with the following features Overload following operators for the template class + - < > Overload << and >> operators for the ostream and istream against this class. Write a main function to demonstrate the functionality of each operator.
1.) Modify your Student class to overload the following operators: ==, !=, <, >, <=, >=...
1.) Modify your Student class to overload the following operators: ==, !=, <, >, <=, >= as member operators. The comparison operators will compare students by last name first name and id number. Also, overload << and >> as friend operators in the Student class. Overload << and >> operators in Roster class as well to output Rosters in a nice format as well as input Roster. Provide a function sort in a Roster class as a private function to...
This chapter uses the class rectangleType to illustrate how to overload the operators +, *, ==,...
This chapter uses the class rectangleType to illustrate how to overload the operators +, *, ==, !=, >>, and <<. In this exercise, first redefine the class rectangleType by declaring the instance variables as protected and then overload additional operators as defined in parts a to c. 1.Overload the pre- and post-increment and decrement operators to increment and decrement, respectively, the length and width of a rectangle by one unit. (Note that after decrementing the length and width, they must...
C++ Program Overload the following operators to work with the Rational class and add test cases...
C++ Program Overload the following operators to work with the Rational class and add test cases in the driver program. Make sure your driver program now tests each of these symbols for your Rational Class. + – * / == != < <= > >= << (stream insertion operator, use the toString function) >> (stream extraction operator) Make sure you test all 12 operators Example Run (Bold is input), everything else is a print statement using the overloaded operators Enter...
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators...
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators in the code main.cpp //This program shows how to use the class rectangleType. #include <iostream> #include "rectangleType.h" using namespace std; int main() { rectangleType rectangle1(23, 45); //Line 1 rectangleType rectangle2(12, 10); //Line 2 rectangleType rectangle3; //Line 3 rectangleType rectangle4; //Line 4 cout << "Line 5: rectangle1: "; //Line 5 rectangle1.print(); //Line 6 cout << endl; //Line 7 cout << "Line 8: rectangle2: "; //Line...
Write the following questions as queries in SQL. Use only the operators discussed in class (no...
Write the following questions as queries in SQL. Use only the operators discussed in class (no outer joins) Consider the following database schema: INGREDIENT(ingredient-id,name,price-ounce) RECIPE(recipe-id,name,country,time) USES(rid,iid,quantity) where INGREDIENT lists ingredient information (id, name, and the price per ounce); RECIPE lists recipe information (id, name, country of origin, and time it takes to cook it); and USES tells us which ingredients (and how much of each) a recipe uses. The primary key of each table is underlined; rid is a foreign...
C++ Please write a exmaple of class template RedBlackTree.h.(It must be template!). I do not mind...
C++ Please write a exmaple of class template RedBlackTree.h.(It must be template!). I do not mind about details but please include the basic functions that are necessary for a RedBlackTree.
Complete the following: Extend the newString class (attached) to include the following: Overload the operator +...
Complete the following: Extend the newString class (attached) to include the following: Overload the operator + to perform string concatenation. Overload the operator += to work as shown here: s1 = "Hello " s2 = "there" s1 += s2 // Should assign "Hello there" to s1 Add a function length to return the length of the string. Write a test program. //myString.h (header file) //Header file myString.h    #ifndef H_myString #define H_myString #include <iostream> using namespace std; class newString {...
Modify the FeetInches class so that it overloads the following operators: <= >= != Demonstrate the...
Modify the FeetInches class so that it overloads the following operators: <= >= != Demonstrate the class's capabilities in a simple program. this is what needs to be modified // Specification file for the FeetInches class #ifndef FEETINCHES_H #define FEETINCHES_H #include <iostream> using namespace std; class FeetInches; // Forward Declaration // Function Prototypes for Overloaded Stream Operators ostream &operator << (ostream &, const FeetInches &); istream &operator >> (istream &, FeetInches &); // The FeetInches class holds distances or measurements...
Write your own version of a class template that will create a static stack of any...
Write your own version of a class template that will create a static stack of any data type. Demonstrate the class with a driver program. please make a version to copy.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT