0% found this document useful (0 votes)
5 views3 pages

operator overloading

The document contains C++ code demonstrating operator overloading in a class named 'sample'. It includes implementations for increment and decrement operators, as well as a binary addition operator and a less-than operator. The main function tests these overloaded operators with instances of the 'sample' class.

Uploaded by

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

operator overloading

The document contains C++ code demonstrating operator overloading in a class named 'sample'. It includes implementations for increment and decrement operators, as well as a binary addition operator and a less-than operator. The main function tests these overloaded operators with instances of the 'sample' class.

Uploaded by

or12261618
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/* Online C++ Compiler and Editor */

#include <iostream>

class sample
{
public:
int a;
void operator ++()
{
a=a+1;
}
void operator ++(int)
{
a=a+1;
}
void operator --()
{
a=a-1;
}
void operator --(int)
{
a=a-1;
}
};

int main()
{
sample s,s1;

s.a=1;

++s;
s++;
--s;
s--;

cout<<s.a;

return 0;
}

Binary operator overloading


/* Online C++ Compiler and Editor */
#include <iostream>

class sample
{
public:
int a;
void operator ++()
{
a=a+1;
}
void operator ++(int)
{
a=a+1;
}
void operator --()
{
a=a-1;
}
void operator --(int)
{
a=a-1;
}
int operator +(sample &i)
{
return a+i.a;
}

};

int main()
{
sample s,s1;

s.a=1;
s1.a=2;

int r=s+s1;//s.+(s1);
std::cout<<r;

return 0;
}

task:
#include<iostream>
using namespace std;

class sample
{
public:
int a;

int operator <(sample &i)


{
return a<i.a;
}

};

int main()
{
sample s,s1;
s.a=1;
s1.a=2;

if(s<s1){
cout<<"s1 bigger"<<endl;
}
else{
cout<<"s bigger"<<endl;
}
return 0;
}

You might also like