Write a C++ program for the following specification: Create a class Fuzzy with two
data members: x of type integer and memx of type float. memx should range from 0 to 1. Implement the operator overloading for the operators: &, I and ~. i. Operator function for & should return the Fuzzy object with minimum memx, after checking the equality of data member x in both objects.
Sample Data:
f1 (10,0.5) f2= (10,0.2)
f1&f2= (10,0.2) ii. Operator function for should return the Fuzzy object with 1-memx..
Sample Data:
f2=(10,0.2) ~f2=(10,0.8)
Implement the appropriate member functions to get the input and print the output.
Note: In & operator overloading, if x is not equal, then return fuzzy object (0,0)
#include <iostream>
using namespace std;
class Fuzzy
{
private:
int x;
float memx;
public:
Fuzzy()
{
this->x = 0;
this->memx = 0.0;
}
Fuzzy(int _x, float _mm)
{
this->x = _x;
this->memx = _mm;
}
int getX()const
{
return this->x;
}
float getMemX()const
{
return this->memx;
}
void Display()
{
cout << "(" << this->getX() << "," << this->getMemX() << ")";
}
friend Fuzzy operator&(const Fuzzy& a, const Fuzzy& b);
Fuzzy& operator~()
{
this->memx = 1 - this->memx;
return *this;
}
};
Fuzzy operator&(const Fuzzy& a, const Fuzzy& b)
{
if (a.getX() == b.getX())
return Fuzzy(a.getX(), min(a.getMemX(), b.getMemX()));
else
return Fuzzy();
}
int main()
{
int _x;
float mx;
cout << "Please enter x data member first object Fuzzy: ";
cin >> _x;
cout << "Please enter memx data member first object Fuzzy:";
cin >> mx;
Fuzzy f1(_x, mx);
cout << "Please enter x data member second object Fuzzy: ";
cin >> _x;
cout << "Please enter memx data member second object Fuzzy:";
cin >> mx;
Fuzzy f2(_x, mx);
Fuzzy fand;
fand = f1 & f2;
~f1;
cout << "~f1= ";
f1.Display();
cout << endl;
cout << "f1&f2=";
fand.Display();
cout << endl;
return 0;
}
Comments
Leave a comment