0% found this document useful (0 votes)
3 views21 pages

Operator Overloading Jun 2

The document explains operator overloading in C++, detailing how to overload various operators for user-defined classes. It provides examples of classes such as Arithmetic, location, and Box, demonstrating how to implement overloaded operators for arithmetic and relational operations. Additionally, it outlines restrictions on operator overloading and discusses the use of friend functions for non-member operator functions.

Uploaded by

jovan.stankovic
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views21 pages

Operator Overloading Jun 2

The document explains operator overloading in C++, detailing how to overload various operators for user-defined classes. It provides examples of classes such as Arithmetic, location, and Box, demonstrating how to implement overloaded operators for arithmetic and relational operations. Additionally, it outlines restrictions on operator overloading and discusses the use of friend functions for non-member operator functions.

Uploaded by

jovan.stankovic
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Operator Overloading

In C++, you can overload most operators so that they perform special operations relative to the classes
that you create. Some operators are overloaded frequently. For example, the C++ language itself
overloads + and -, so that these operators perform differently depending on the built-in data type (int,
double, float) being used . Also, if you want to use the same operators to perform addition or subtraction
of Complex numbers (a class that we mentioned earlier) you can overload + and – so that they perform
arithmetic for your own data type.

Creating an Overloaded Operator

Operators are overloaded by writing a function definition (with a header and body) as you normally
would, except that the function name now becomes the keyword operator followed by the symbol being
overloaded. For example, the function name operator+ would be used to overload the addition operator
(+).

For example, if you had a simple class called Arithmetic that performs basic algebraic operations if you
wanted to overload + operator so that it works on variables of your own class you would write

#include <iostream>
using namespace std;

class Arithmetic {
public:
Arithmetic(int = 1); // constructor
Arithmetic operator+(Arithmetic);
Arithmetic operator-(Arithmetic);
Arithmetic operator/(Arithmetic);
Arithmetic operator*(Arithmetic);
Arithmetic operator%(Arithmetic);
Arithmetic operator++();
void printNumber (){ cout << num << endl;}
private:
int num;

};

Arithmetic::Arithmetic( int n){


num=n;
}

Arithmetic Arithmetic::operator+(Arithmetic b){


Arithmetic c;
[Link] = num + [Link];
return c;
}

Arithmetic Arithmetic::operator-(Arithmetic b){


Arithmetic c;
[Link] = num - [Link];
return c;
}

Arithmetic Arithmetic::operator*(Arithmetic b){


Arithmetic c;
[Link] = num * [Link];
return c;
}

Arithmetic Arithmetic::operator/(Arithmetic b){


Arithmetic c;
[Link] = num / [Link];
return c;
}

Arithmetic Arithmetic::operator%(Arithmetic b){


Arithmetic c;
[Link] = num % [Link];
return c;
}

Arithmetic Arithmetic::operator++(){
++num;
return *this;
}

int main(){
Arithmetic x(11), y(5), z; //instantiate object a of class
Arithmetic
z = x + y; // note that at this point 2 variables of type
Arithmetic are added
cout << "The sum is: ";
[Link] ();
cout << endl;

z = x - y; // note that at this point 2 variables of type


Arithmetic are subtracted
cout << "The difference is: ";
[Link] ();
cout << endl;

z = x * y; // note that at this point 2 variables of type


Arithmetic are multipled
cout << "The product is: ";
[Link] ();
cout << endl;

z = x / y; // note that at this point 2 variables of type


Arithmetic are divided
cout << "The quotinet is: ";
[Link] ();
cout << endl;

z = x % y; // note that at this point 2 variables of type


Arithmetic are mod divided
cout << "The remainder is: ";
[Link] ();
cout << endl;

cout << endl;


++y;
cout << "y incremented by one: ";
[Link]();
cout << endl;
return 0;}

Example 1. Overload remaining binary arithmetic operators (-, /, *, %). Also, try to overload one of the
unary operators (++ or --)
Also, overload all of the relational operators(>, <, >=, <=, ==, !=)

Shown next is our second example of operator overloading. This program creates a class called location,
which has as data members longitude and latitude and as function members print and operator+

class location{
public:
location(int=0,int=0);
void print()const;
location operator+(location op2);
private:
int longitude;
int latitude; };

location::location(int lg, int lt)


{longitude = lg;
latitude = lt;}

void location::print ()const{


cout << longitude << "\t";
cout << latitude << "\n";}

location location::operator+(location op2){


location temp;
[Link] = [Link] + longitude;
[Link] = [Link] + latitude;
return temp;}

int main(){
location ob1(10, 20), ob2(5, 30);
[Link]();
[Link]();

ob1= ob1 + ob2;


[Link]();

return 0;}

Example 2.
Add three more overloaded operators to our class location :
- location operator-(location op2)
- location operator=(location op2)
- location operator++();

Solution:
#include <iostream>
using namespace std;

class location{
public:
location(int=0,int=0);
void print()const;
location operator+(location op2);
location operator-(location op2);
location operator=(location op2);
location operator++();
private:
int longitude;
int latitude; };

location::location(int lg, int lt)


{longitude = lg;
latitude = lt;}

void location::print ()const{


cout << longitude << "\t";
cout << latitude << "\n";}

location location::operator+(location op2){


location temp;
[Link] = longitude + [Link];
[Link] = latitude + [Link];
return temp;}

location location::operator-(location op2){


location temp;
[Link] = [Link] - longitude;
[Link] = [Link] - latitude;
return temp;}

location location::operator=( location op2){


longitude = [Link];
latitude = [Link] ;
return *this;}
location location::operator++(){
longitude++;
latitude++;
return *this;}

int main(){
location ob1(10, 20), ob2(5, 30), ob3(90,90);

[Link]();
[Link]();
++ob1;
[Link]();

ob2= ++ob1;
[Link]();
[Link]();

ob1=ob2=ob3;
[Link]();
[Link]();

return 0;}

Example 3.
Define a class Box with member functions Volume, which calculates the volume of a box, and
operator>, which compares the volumes of two given boxes. Data members of the class are Height,
Width and Length, all of type double.
#include <iostream>
using namespace std;
class box{
public:
box(double=1.0, double=1.0,double=1.0);
double Volume();
bool operator>(box);
private:
double length;
double width;
double height;
};

box::box(double l, double w, double h){


length = l;
width = w;
height = h;}

double box::Volume(){
return length*width*height;}

bool box::operator >(box abox){


return Volume()>[Link]();}

int main(){
box small(4.0, 2.0, 1.0), medium(5.0, 3.0, 2.9);
if (medium>small) cout << "medium box is bigger"<<endl;
else cout << "small box is bigger"<<endl;
return 0;}
Example 4.
Define a class Binary that performs all of the basic arithmetic operations on the Binary numbers. In
addition, overload some of the relational operators as well (>, <, ==)

Restrictions on Operator Overloading

Most of the C++ operators can be overloaded. These are the operators that you can not overload:
:: The scope resolution operator
?: The conditional operator
. The dot operator
sizeof The size-of operator
.* The de-reference pointer to class member operator

- The precedence of an operator cannot be changed by overloading


- It is not possible to change the number of operands an operator takes
- It is not possible to create new operators
- The meaning of how an operator works on objects of built-in types cannot be changed by operator
overloading
- Overloading an addition operator does not mean that corresponding shorthand operator (+=) is
overloaded

Overloading non member functions


Operator Functions can be Class Members or friend Functions
Operator functions can be member functions or non-member functions of a particular class. Member
functions use this pointer implicitly to obtain their class object arguments.

Both class arguments must be listed explicitly in a non-member function call.


When an operator function is implemented as a member function, the leftmost operand must be a class
object.
If the left operand must be an object of a different class, the operator function must be implemented as a
non-member function.

If you use a non-member function to overload an operator, almost always it is implemented by using a
friend function. Since a friend function is not a member of the class, it does not have a this pointer.
Therefore, an overloaded friend operator function is passed the operands explicitly. This means that a
friend function that overloads a binary operator has two parameters, and a function that overloads a unary
operator has one parameter. When overloading a binary operator, the left operand is passed as the first
parameter and the right operand is passed as the second parameter of the friend operator function.

Let’s overload the subtraction operator from our previous examples, but this time we can try it by using a
friend function.
Example 1. Class Arithmetic
#include <iostream>
using namespace std;

class Arithmetic {
friend Arithmetic operator-(Arithmetic ,Arithmetic);
friend istream &operator>>(istream & , Arithmetic & );
public:
Arithmetic(int = 1); // constructor
Arithmetic operator+(Arithmetic);
void printNumber (){ cout << num << endl;}
private:
int num;

};

//add function definitions

int main(){
Arithmetic x, y, z; //instantiate object a of class Arithmetic
cin>>x;
cin>>y;
z = x + y; // note that at this point 2 variables of type
Arithmetic are added
cout << "The sum is: ";
[Link] ();
cout << endl;
z = x - y;
cout << "The difference is: ";
[Link] ();
cout << endl;

return 0;}
Example 2. Overload remaining binary and unary arithmetic operators of the class Arithmetic using
friend functions. Overload << (cout) operator as well.
#include <iostream>
using namespace std;

class Binary_Arithmetic {
friend ostream &operator<<( ostream &, Binary_Arithmetic &);
friend istream &operator>>(istream &, Binary_Arithmetic &);
public:
Binary_Arithmetic(int = 1); // constructor
Binary_Arithmetic operator+(Binary_Arithmetic);
Binary_Arithmetic operator-(Binary_Arithmetic);
Binary_Arithmetic operator/(Binary_Arithmetic);
Binary_Arithmetic operator*(Binary_Arithmetic);
Binary_Arithmetic operator%(Binary_Arithmetic);
Binary_Arithmetic operator++();
void printBinaryNumber (){ cout << binarynum << endl;}
private:
int dec_to_binary(int, int);
int binary_to_dec(int, int);
int binarynum;
int decimalnum;
};

Binary_Arithmetic::Binary_Arithmetic( int n){


binarynum=n;
decimalnum= binary_to_dec(binarynum, 0);
}

Binary_Arithmetic Binary_Arithmetic::operator+(Binary_Arithmetic b){


Binary_Arithmetic c;
[Link]=decimalnum+[Link];
[Link] = dec_to_binary ([Link], 0);
return c;
}

Binary_Arithmetic Binary_Arithmetic::operator-(Binary_Arithmetic b){


Binary_Arithmetic c;
[Link]=[Link];
[Link] = dec_to_binary ([Link], 0);
return c;
}

Binary_Arithmetic Binary_Arithmetic::operator*(Binary_Arithmetic b){


Binary_Arithmetic c;
[Link]=decimalnum*[Link];
[Link] = dec_to_binary ([Link], 0);
return c;
}

Binary_Arithmetic Binary_Arithmetic::operator/(Binary_Arithmetic b){


Binary_Arithmetic c;
[Link]=decimalnum/[Link];
[Link] = dec_to_binary ([Link], 0);
return c;
}

Binary_Arithmetic Binary_Arithmetic::operator%(Binary_Arithmetic b){


Binary_Arithmetic c;
[Link]=decimalnum%[Link];
[Link] = dec_to_binary ([Link], 0);
return c;
}

Binary_Arithmetic Binary_Arithmetic::operator++(){
++decimalnum;
binarynum = dec_to_binary (decimalnum, 0);
return *this;
}

int Binary_Arithmetic::dec_to_binary(int num, int expo){


if (num<2)
return num*pow(10.0,expo);
else
return num%2*pow(10.0,expo)+dec_to_binary(num/2, expo + 1);
}

int Binary_Arithmetic::binary_to_dec(int n, int c){

if (n==0||n==1)
return pow(2.0,c);
else

{
return n%2*pow(2.0,c)+binary_to_dec(n/10,c+1);
}

istream &operator>>(istream & inputbinary, Binary_Arithmetic & a){

inputbinary>>[Link];

return inputbinary;
}

ostream &operator<<( ostream & outputbinary, Binary_Arithmetic & a){

outputbinary<<[Link];

return outputbinary;
}

int main(){
Binary_Arithmetic x(1100), y(1001), z; //instantiate object a of class
Arithmetic
cout << "x = " << x << endl;
cout << "y = " << y << endl;
z = x + y; // note that at this point 2 variables of type Arithmetic are
added
cout << "The sum is: " << z << endl;

z = x - y; // note that at this point 2 variables of type Arithmetic are


subtracted
cout << "The difference is: " << z << endl;;

z = x * y; // note that at this point 2 variables of type Arithmetic are


multipled
cout << "The product is: " << z << endl;;

z = x / y; // note that at this point 2 variables of type Arithmetic are


divided
cout << "The quotinet is: " << z << endl;;

z = x % y; // note that at this point 2 variables of type Arithmetic are


mod divided
cout << "The remainder is: " << z << endl;;

cout << endl;


++y;
cout << "y incremented by one: ";
cout << y << endl;

return 0;
}

Example 3. Class Location

#include <iostream>
#include <string>
using namespace std;
class location{
public:
location(int=0,int=0);
void print()const;
friend location operator+(location op1, location op2);
private:
int longitude;
int latitude; };

location::location(int lg, int lt)


{longitude = lg;
latitude = lt;}

void location::print ()const{


cout << longitude << "\t";
cout << latitude << "\n";}

location operator+(location op1, location op2){


location temp;
[Link] = [Link] + [Link];
[Link] = [Link] + [Link];
return temp;}

int main(){
location ob1(10, 20), ob2(5, 30);

[Link]();
[Link]();

ob1= ob1 + ob2;


[Link]();

return 0;
}

Example 3a.
Overload -, ++ and --operators by using friend functions:

Example 4.
Use the following class definition for class PhoneNumber to overload Stream-Insertion and Stream-
Extraction operator which will allow input and output of objects of type PhoneNumber.

// Overloading the stream-insertion and stream-extraction operators.


#include <iostream>
#include <iomanip>

using namespace std;

#include <iostream>
#include<algorithm>
#include <string>
#include<cmath>
#include <fstream>
#include <iomanip>
using namespace std;
class PhoneNumber {
friend ostream &operator<<( ostream&, const PhoneNumber & );
friend istream &operator>>( istream&, PhoneNumber & );
private:
char areaCode[ 4 ]; // 3-digit area code and null
char exchange[ 4 ]; // 3-digit exchange and null
char line[ 5 ]; // 4-digit line and null
};
ostream &operator<<( ostream &output, const PhoneNumber &num ) {
output << "(" << [Link] << ") " << [Link] << "-" << [Link];
return output;
}

istream &operator>>( istream &input, PhoneNumber &num ) {


[Link]();
input >> setw( 4 ) >> [Link];
[Link]( 2 );
input >> setw( 4 ) >> [Link];
[Link]();
input >> setw( 5 ) >> [Link];
return input;
}
int main() {
PhoneNumber phone;
cout << "Enter phone number in the form (123) 456-7890:\n";
cin >> phone;
cout << "The phone number entered was: " << phone << endl;

return 0;
}

Try incorporating the ostream & istream as member functions;


Try using PhoneNumber >> cin;

Try incorporating the ostream & istream as member functions;


Try using PhoneNumber >> cin;

Back to 3 classic examples: Complex, Rational and IntegerSet class


Example 1.
Consider the following implementation of the class Complex.
Study the class carefully, then
Modify the class to enable input and output of complex numbers through the overloaded >> and
<< operators (you should remove print function from the class).
Overload the multiplication, addition, subtraction operator to enable the basic algebra of two
complex numbers
Overload the == and != operators to allow comparisons of complex numbers
(textbook solution)
#include <iostream>
#include <fstream>
using namespace std;

class Complex {
friend ostream &operator<<( ostream&, const Complex & );
friend istream &operator>>( istream&, const Complex & );
public:
Complex( double = 0.0, double = 0.0 ); // constructor
Complex operator+( const Complex & ) ; // addition
Complex operator-( const Complex & ) ; // subtraction
Complex operator*( const Complex & ) ; // multiplication
const Complex &operator=( Complex & ); // assignment
bool operator==(Complex &);
bool operator!=(Complex &);

private:
double real; // real part
double imaginary; // imaginary part
};
//Constructor
Complex::Complex( double r, double i )
: real( r ), imaginary( i ) { }

// Overloaded addition operator


Complex Complex::operator+( const Complex &operand2 ) {
return Complex( real + [Link], imaginary + [Link] );
}

// Overloaded subtraction operator


Complex Complex::operator-( const Complex &operand2 ) {
return Complex( real - [Link], imaginary - [Link] ); }

// Overloaded = operator
const Complex& Complex::operator=( Complex &right ) {
real = [Link];
imaginary = [Link];
return * this ; }

Complex Complex::operator*( const Complex &right ) {


return Complex([Link] * real - imaginary *[Link],
[Link]*real + [Link] * imaginary); }

bool Complex::operator==( Complex &right){


if ([Link] == imaginary && [Link] == real){
return true;
}
return false;
}

bool Complex::operator!=( Complex &right){


if ([Link] == imaginary && [Link] == real){
return false;
}
return true;
}

// Display a Complex object in the form: (a, b)


ostream &operator<<( ostream&cout, const Complex & a ) {
cout << '(' << [Link] << '+' << [Link] << "i)";
return cout;
}

istream &operator>>( istream & cin, const Complex &a){


cin >> [Link];
cin >> [Link];
return cin;
}

int main() {
Complex x, y( 4.3, 8.2 ), z( 3.3, 1.1 );

cout << "x: ";


cout<<x;
cout << "\ny: ";
cout<<y
;
cout << "\nz: ";
cout<<z;

x = y + z;
cout << "\n\nx = y + z:\n";
cout<< x;
cout << " = ";
cout<< y;
cout<< " + ";
cout<<z;

x = y - z;
cout << "\n\nx = y - z:\n";
cout<<x;
cout << " = ";
cout<<y;
cout << " - ";
cout<<z;
cout << endl;
cin >> x;
cout<< x;

return 0; }

Example 2.
Create a class called RationalNumber with the following capabilities:
a) Create a constructor that prevents a 0 denominator in a fraction.
b) Overload the addition, subtraction, multiplication and division operators for this class.
c) Overload the relational and equality operators for this class.
d) Overload the stream insertion and the stream extraction operator.

#include <iostream>
using namespace std;

class Rational {
friend ostream &operator<<( ostream &, Rational &);
friend istream &operator>>(istream &, Rational &);
public:
Rational (int n = 0, int d = 1);
Rational operator+(const Rational &);
Rational operator-(const Rational &);
Rational operator*(const Rational &);
Rational operator/(const Rational &);
bool operator<( Rational &);
bool operator>( Rational &);
bool operator==( Rational &);
void printRational ();
private:
int numerator;
int denominator;
};

Rational::Rational (int n, int d) {


numerator = n;
denominator = d;
if ( d <= 0 )
cout << "Enter a positive, non-zero denominator\n";
}

//function addition
Rational Rational::operator+ (const Rational & a) {
return Rational (numerator * [Link] + denominator * [Link],denominator *
[Link]);
}

//function subtraction
Rational Rational::operator- (const Rational & a) {
return Rational (numerator * [Link] - denominator * [Link], denominator *
[Link]);
}

//function multiplication
Rational Rational::operator* (const Rational & a) {
return Rational (numerator * [Link], denominator * [Link]);
}

//function division
Rational Rational::operator/ (const Rational & a) {
return Rational (numerator * [Link], denominator * [Link]);
}

//operator <
bool Rational::operator < ( Rational & a) {
if ( numerator/denominator < [Link]/[Link])
return true;
return false;
}

//operator >
bool Rational::operator > (Rational & a) {
if (numerator/denominator> [Link]/[Link])
return true;
return false;
}

//operator ==
bool Rational::operator == (Rational & a) {
if (numerator/denominator == [Link]/[Link])
return true;
return false;
}

//function printRational
void Rational::printRational () {
cout << numerator << "/" << denominator;
}
ostream &operator<<( ostream & cout, Rational & a ) {
cout << [Link] << "/" << [Link] ;
return cout;
}

istream &operator>>( istream & cin, Rational & a){


cin >> [Link];
cin >> [Link];
return cin;
}

int main () {
Rational x;
Rational y (1, 2);
Rational z (3, 2);

cout << "x = ";


[Link]();
cout << "\n";

cout << "y = ";


[Link]();
cout << "\n";

cout <<"z = ";


[Link]();
cout << "\n\n";

x = y + z;
cout << "y + z = " ;
[Link]();
cout << "\n";

x = y - z;
cout << "y - z = ";
[Link]();
cout << "\n";

x = y * z;
cout << "y * z = ";
[Link]();
cout << "\n";

x = y / z;
cout << " y / z = ";
[Link]();
cout << "\n\n";

if (y < z)
cout << "y is smaller than z\n\n";
else if ( y > z)
cout << "y is larger than z\n\n";
else if ( y == z)
cout << "y is equal to z\n\n";

cout << "Enter a rational number:\n";


cin >> x;
cout << "The rational number you entered was\n";
cout << x;

return 0;
}

Example 3.
Implement the class IntegerSet as discussed before. Overload the operators || to represent Union
of two sets and && to represent the Intersection of two sets. Also, overload the stream insertion
and extraction operator (>> and <<) as friend functions.

#include <iostream>

using namespace std;

class IntegerSet {
friend ostream &operator<<( ostream &, IntegerSet &);

public:
IntegerSet();
IntegerSet operator||(IntegerSet);
IntegerSet operator&&(IntegerSet);
void SetPrint();
bool operator==(IntegerSet);
void InsertElement(int );
void DeleteElement(int );
private:
int set[10];

};

IntegerSet::IntegerSet() {
for (int i = 0; i < 10; i++)
set[i] = 0;
}

IntegerSet IntegerSet::operator||(IntegerSet x) {
IntegerSet temp;
for (int i = 0; i < 10; i++) {
if (set[i] == 1 || [Link][i] == 1)
[Link][i] = 1;
}
return temp;
}
IntegerSet IntegerSet::operator&&(IntegerSet x) {
IntegerSet temp;
for (int i = 0; i < 10; i++) {
if (set[i] == 1 && [Link][i] == 1)
[Link][i] = 1;
}
return temp;
}

bool IntegerSet::operator== (IntegerSet x) {


IntegerSet temp;
bool test = true;
for (int i = 0; i < 100; i++) {
if (set[i] == [Link][i])
test = true;
else
test = false;
}

return test;

}
void IntegerSet::InsertElement(int x) {
set[x] = 1;
}

void IntegerSet::DeleteElement(int x) {
set[x] = 0;
}

void IntegerSet::SetPrint() {

for (int i = 0; i < 10; i++)


cout << set[i] << " \t" ;

ostream &operator<<( ostream & printset, IntegerSet & a){


for(int i=0; i<10;i++)
printset<<[Link][i]<<"\t";

return printset;
}

int main() {
IntegerSet x, y, z;

cout << "print initial set x(empty)." << endl;


cout<<x;

cout << "print initial set y(empty). " << endl;


cout << y;

[Link](2);
[Link](3);
[Link](5);
cout << "print updated set x(2;3;5)." << endl;
cout << x;

[Link](3);
[Link](4);
[Link](5);
cout << "print updated set y(3;4;5)." << endl;
cout << y;

z = x||y;
cout << "print z ( union of x and y )" << endl;
[Link]();

z = x&&y;
cout << "print z ( intersection of x and y )" << endl;
cout<<z;

if (x==y)
cout << " the sets z and y are equal " << endl;
else
cout << " the sets z and y are not equal " << endl;

return 0;
}

OR

#include <iostream>

using namespace std;

class IntegerSet {
friend ostream &operator<<( ostream &, IntegerSet &);
friend istream &operator>>(istream &, IntegerSet &);

public:
IntegerSet();
IntegerSet operator||(IntegerSet);
IntegerSet operator&&(IntegerSet);
void SetPrint();
bool operator==(IntegerSet);
void InsertElement(int );
void DeleteElement(int );
private:
int set[10];

};

IntegerSet::IntegerSet() {
for (int i = 0; i < 10; i++)
set[i] = 0;
}

IntegerSet IntegerSet::operator||(IntegerSet x) {
IntegerSet temp;
for (int i = 0; i < 10; i++) {
if (set[i] == 1 || [Link][i] == 1)
[Link][i] = 1;
}
return temp;
}

IntegerSet IntegerSet::operator&&(IntegerSet x) {
IntegerSet temp;
for (int i = 0; i < 10; i++) {
if (set[i] == 1 && [Link][i] == 1)
[Link][i] = 1;
}
return temp;
}

bool IntegerSet::operator== (IntegerSet x) {


IntegerSet temp;
bool test = true;
for (int i = 0; i < 100; i++) {
if (set[i] == [Link][i])
test = true;
else
test = false;
}

return test;

}
void IntegerSet::InsertElement(int x) {
set[x] = 1;
}

void IntegerSet::DeleteElement(int x) {
set[x] = 0;
}

void IntegerSet::SetPrint() {

for (int i = 0; i < 10; i++)


cout << set[i] << " \t" ;

istream &operator>>(istream & inputset, IntegerSet & a){


int num;
for(int c = 0; c<=9; c++){
inputset>>num;
[Link][num] = 1 ;
}

return inputset;
}

ostream &operator<<( ostream & printset, IntegerSet & a){


for(int i=0; i<10;i++)
printset<<[Link][i]<<"\t";

return printset;
}

int main() {
IntegerSet x, y, z;

cout << "print initial set x(empty)." << endl;


cout<<x;

cout << "print initial set y(empty). " << endl;


cout << y;

cin>>x;
cout << "print updated set x(2;3;5)." << endl;
cout << x;

cin>>y;
cout << "print updated set y(3;4;5)." << endl;
cout << y;

z = x||y;
cout << "print z ( union of x and y )" << endl;
[Link]();

z = x&&y;
cout << "print z ( intersection of x and y )" << endl;
cout<<z;

if (x==y)
cout << " the sets z and y are equal " << endl;
else
cout << " the sets z and y are not equal " << endl;

return 0;
}

You might also like