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

all function and operator has been overloaded through friend function and it is able to produce output of all these

operator

code is below:-

#include <iostream> 
using namespace std; 
  
class operation 
{ 
private: 
    int num;
public: 
    operation(int r = 0) 
    {  num = r; } 
    friend ostream & operator << (ostream &out, const operation &c); 
    friend istream & operator >> (istream &in,  operation &c); 
    friend operation operator + (operation const &, operation const &); 
    friend operation operator - (operation const &, operation const &); 
    friend operation operator > (operation const &, operation const &); 
    friend operation operator < (operation const &, operation const &);
    void print()
{
cout<<num<<endl;
} 
}; 
  
ostream & operator << (ostream &out, const operation &c) 
{   out<<"Entered number is : "<<c.num<<endl;
    return out; 
} 
  
istream & operator >> (istream &in,  operation &c) 
{ 
    cout << "Enter Number \n"; 
    in >> c.num; 
    return in; 
} 
  operation operator + (operation const &c1, operation const &c2) 
{ 
     return operation(c1.num + c2.num);
} 
   operation operator - (operation const &c1, operation const &c2) 
{ 
     return operation(c1.num - c2.num);
} 

    operation operator > (operation const &c1, operation const &c2) 
{ 
      if(c1.num>c2.num) return c1.num;
     return c2.num;
}   
  operation operator < (operation const &c1, operation const &c2) 
{ 
      if(c1.num<c2.num) return c1.num;
     return c2.num;
}   

int main() 
{ 
   operation c1; 
   operation c2;
   operation c3;
   cin >> c1;  
   cout << c1; 
   cin>>c2;
   cout<<c2;
   c3=c1+c2;
   cout<<"\n"<<endl;
  cout<<"sum is : ";c3.print();cout<<"\n";
  c3=c1-c2;
   cout<<"Difference  is : ";c3.print();cout<<"\n";
  c3=c1>c2;
    cout<<"Greater  is : ";c3.print();cout<<"\n";
   c3=c1<c2;
   cout<<"Smaller  is : ";c3.print();cout<<"\n";
   return 0; 
}

output is mentioned below

output 2

Explanation :- all declared method operator overloaded function are declared as friend function because this function can access directly private and protected data and methods


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...
Write the following questions as queries in RELATIONAL ALGEBRA. Use only the operators discussed in class...
Write the following questions as queries in RELATIONAL ALGEBRA. Use only the operators discussed in class (select, project, Cartesian product, join, union, intersection, set difference and renaming). The following database schema is given: ATHLETE(name,age,height,weight,country) RACE(id,location,date,time-start,distance) COMPETES(aname,rid,time,position) where ATHLETE has information about runners (their name, age, height, weight, and nationality); RACE has information about races (id, location, date when it’s held, time it starts, and distance ran); and COMPETES keeps track of which runners run on with race, the time it...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT