You are on page 1of 9

Unary Operator overloading

#include<iostream>
using namespace std;

//Increment and decrement overloading
class Inc {
private:
int count ;
public:
Inc() {
//Default constructor
count = 0 ;
}

Inc(int C) {
// Constructor with Argument
count = C ;
}

Inc operator ++ () {
// Operator Function Definition
// for prefix
return Inc(++count);
}

() { // Operator Function Definition // for prefix return Inc(--count). . f(4).Inc operator ++ (int) { // Operator Function Definition // with dummy argument for postfix return Inc(count++). } Inc operator -. b(4). } Inc operator -. e(1). } }. int main() { Inc a. d. c.(int) { // Operator Function Definition // with dummy argument for postfix return Inc(count--). } void display(void) { cout << count << endl .

display(). b. cout << "After using the operator ++()\n". d. cout << "d = ".cout << "Before using the operator ++()\n". cout << "Result prefix (on a) and postfix (on b)\n". ++a. c = ++a. c.display(). cout << "a = ". cout << "c = ". cout << "b = ". b. b++. a.display(). cout << "b = ". cout << "a = ". a.display().display().display(). . d = b++.

e.display(). class Rational { private: int num.cout << "Before using the operator --()\n". cout << "f = ". cout << "f = ".display(). cout << "After using the operator --()\n". f. e.display(). cout << "e = ".display(). f--. return 0. f. // numerator . --e. cout << "e = ". } Binary Operator overloading #include<iostream> using namespace std.

Rational add(Rational object). Rational operator+(Rational object). bool operator!=(Rational object). void Rational::show() { cout << num << "/" << den << "\n". // denominator public: void show().int b) { setnumden(a. }.int den.b.int=1). } void Rational::setnumden(int x.b).a. Rational(int=1. if(b > a) { temp = b. .int).int y) { int temp. bool operator==(Rational object). a = x. b = y. void setnumden(int. } Rational::Rational(int a.

a = temp.b = a. return Rational(new_num. int new_den = den * object. } num = x / b. return Rational(new_num.num. den = y / b.den + den * object. } Rational Rational::add(Rational object) { int new_num = num * object.den. } Rational Rational::operator+(Rational object) { int new_num = num * object. temp = a % b. int new_den = den * object.den. b = temp. } . new_den). } while(a != 0 && b != 0) { if(a % b == 0) break.den + den * object.num. a = b. new_den).

4). } if(obj7 != obj8) { cout << "The two fractions are not equal. result2 = obj3 + obj4.add(obj2). result2.num || den != object.2). if(obj5 == obj6) { cout << "The two fractions are equal. } int main() { Rational obj1(1. obj8(1.den). result1. result1 = obj1.3).35)." << endl. . Rational obj3(1.840)." << endl. } bool Rational::operator!=(Rational object) { return (num != object.show(). result2.14).99).den). obj7(2. obj6(25. result1. obj4(33. Rational obj5(10.bool Rational::operator==(Rational object) { return (num == object.show(). obj2(210.num && den == object.3).

swap(c. cout<<"\nc and d after swaping :"<<c<<"\t"<<d.float c.int b.} return 0. cout<<"\n\nc and d before swaping :"<<c<<"\t"<<d. y=temp. } Function Templates #include<iostream. swap(a. } void fun(int a.d). .float d) { cout<<"\na and b before swaping :"<<a<<"\t"<<b. cout<<"\na and b after swaping :"<<a<<"\t"<<b.t &y) { t temp=x.h> template<class t> void swap(t &x. x=y.b).h> #include<conio.

cout<<"Enter C.} void main() { int a. cin>>a>>b. clrscr().B values(integer):".b. cin>>c>>d.c.d). cout<<"Enter A. fun(a. float c. getch().b. } .d.D values(float):".