Write a program to overload unary operator for processing counters it should support both upward and downward counting
Answers
Answer:
C++ program to overload unary pre-increment operator and provide support for assignment operator (=)
Read: operator overloading and its rules
We have already discussed how to overload pre-increment operator in C++? But that program does not support assignment operator i.e. if we want to store the value of incremented object in another object, we cannot.
In this article we will understand, how to overload pre- increment operator and provide support for “=” assignment operator?
For example:
S1 is object of class sample then we use like this: S2 = ++S1;
To implement operator overloading, we need to use ‘operator’ keyword. To assign extra task to operator, we need to implement a function. That will provide facility to write expressions in most natural form.
Consider the program:
using namespace std;
#include <iostream>
class Sample
{
//private data member
private:
int count;
public:
//default constructor
Sample()
{ count = 0;}
//parameterized constructor
Sample(int c)
{ count = c;}
//overloaded operator, returning an object
Sample operator++()
{
Sample temp;
temp.count = ++count;
return temp;
}
//printing the value
void printValue()
{
cout<<"Value of count : "<<count<<endl;
}
};
int main()
{
int i = 0;
//objects declarations
Sample S1(100), S2;
for(i=0; i< 5; i++)
{
//incrementing the object and assigning the value
//in another object
S2 = ++S1;
//printing the value of S1 object
cout<<"S1 :"<<endl;
S1.printValue();
//printing the value of S2 object
cout<<"S2 :"<<endl;
S2.printValue();
}
return 0;
Explanation:
plz give 10 thanks and mark as brainliest.....