Posts

CSIT Object Oriented Programming With C++ (Overloading Comparison Operator)

 In this post, I will teach you to overload Comparison operator. Comparison operator are used to compare two values and its result is always true or false. So operator function must return an integer value 1(if true) or 0 (if false). We can overload greater than operator(>) as follows. //Overloading comparison (greater than) operator #include<iostream> using namespace std; class Distance { int feet,inch; public: void getdata() { cout<<"Enter Distance: ";  //feet first then inch cin>>feet>>inch; } int operator > (Distance d) { int fd,sd;//first distance and second distance fd=feet*12+inch; sd=d.feet*12+d.inch; if(fd>sd) return 1; else return 0; } }; int main() { Distance d1,d2; d1.getdata(); d2.getdata(); if(d1>d2) cout<<"d1 is greater than d2"<<endl; else cout<<"d2 is greater than d1"<<endl; return 0; } In this way we can overload Comparison( greater than) operator. We can also do it in same ways ...

CSIT Object Oriented Programming with C++(Insertion and Extraction Operator Overloading ) Part VI

  I am assuming that, You have already studied previous parts of Operator Overloading Series. So i n this post, I am going to teach you  How to Overload Insertion and Extraction operator. Here, I will overload '<<' and '>>' operator to input and output  distance class data.  So, let's get started, #include<iostream> using namespace std; class Distance{ int feet,inch; public: Distance(){ feet=0;inch=0; } Distance(int f,int i){ feet=f;inch=i; } friend ostream &operator <<(ostream &output , const Distance &d) { output<<"Feet: "<<d.feet<<" Inch: "<<d.inch<<endl; return output; } friend istream &operator >>(istream &input, Distance& d) { input>>d.feet>>d.inch; return input; } }; int main() { Distance d1(4,8), d2; cin>>d2; cout<<d1<<endl; cout<<d2; return 0; } In this ...

CSIT Object Oriented Programming with C++(Binary plus Operator Overloading ) Part V

I am assuming that, You have already studied Part I,II,III & IV  of Operator Overloading Series. So i n this post, I am going to teach you  How to perform Binary operator overloading. Here, I will overload '+' operator to add two complex numbers.  So, let's get started, Lets have a look at code and understand concept.   //Overloading plus operator to add two complex numbers. #include <iostream> using namespace std; class complex {     int real,imaginary;     public:         void getdata(int a, int b){                 real=a;                 imaginary=b;         }         void display(){             cout<<real<<" + "<<imaginary<<"i"<<endl;         }         complex operator +(complex c1)     ...

CSIT Object Oriented Programming with C++(Unary Operator Overloading using Friend function) Part IV

  I am assuming that, You have already studied Part I,II and III of Operator Overloading Series. So i n this post, I am going to teach you How to perform Unary operator overloading using friend function. So, let's get started, //Overloading pre-increment operator using friend function #include<iostream> class A{ int length,breadth; public: A(int l, int b) { length=l; breadth=b; } friend void operator ++( A&); void display() { cout<<"Length: "<<length<<endl<<"Breadth: "<<breadth<<endl; } }; void operator ++( A& b); { ++b.length; ++b.breadth; } int main() { A a(7,14); ++a;     //equivalent to operator ++(a); a.display(); return 0; } OUTPUT Length: 8 Breadth: 15 This is for pre-increment operator. In next post, I will teach you overloading binary operator using member function.  So refer next  post for that topic. If This post Helped you, Please leave a comment, and share with your Coder Friends. Thank You. Keep Lea...

CSIT Object Oriented Programming with C++(Unary Operator Overloading) Part III

  I am assuming that, You have already studied Part I and II of Operator Overloading Series. So i n this post, I am going to teach you How to do Unary  operator Overloading . Before we get started towards our topic, I wanted to remind you, what  Unary Operator Really is? An operator which acts upon only one operator is called unary operator. For example: Increment operator, Decrement operator and Negation operator (Minus) which  acts as both unary as well as binary operator. Now, lets do coding to understand , how to perform unary operator overloading. Here, I will do Pre-Increment operator overloading. Have a look at this simple code and understand the basic. //Overloading pre-increment operator #include<iostream> class A{ int length,breadth; public: A(int l, int b) { length=l; breadth=b; } void operator ++() { ++length; ++breadth; } void display() { cout<<"Length: "<<length<<endl<<"Breadth: "<<breadth<<endl; } }; int main(...

CSIT Object Oriented Programming with C++(Rules for Operator Overloading ) Part II

I am assuming that, You have already studied Part I of Operator Overloading Series. So i n this post, I am going to teach you about Rules That should be followed while performing operator Overloading. So, let's get started, The following Rules must be followed while doing operator overloading in C++. Operators that are already defined in the C++ compiler can be only overloaded. Operator can't change operator templates that is for example, the decrement operator '--' is used only as unary operator. It can not be used as binary operator. Overloading an operator does not change its basic meaning. for example assume the + operator can be overloaded to subtract two objects. But, still when we use it with two numbers, it finds sum of two numbers. Unary operators, overloaded by means of a member function, takes no explicit argument and return no explicit values. But, those overloaded by means of a friend function takes one reference argument(the object of relevant class). Bina...

CSIT Object Oriented Programming with C++(Operator Overloading)Part I

  In this post, I am going to teach you Basic Concept of OPERATOR OVERLOADING . So, let's get started, Firstly, What is overloading? Here is the Answer :  Overloading refers to the ability to use a single identifier to define multiple methods of a class that differ in their input and output parameters. Now take a look at definition of Operator Overloading: In C++ , Operator Overloading means giving additional meanings and schematics to Normal C++ operators so that we can use them with User Defined Data Types.  Now look at the list of Operators that can't be Overloaded. Class Member Access / Dot Operator (.) Pointer to Member Operator (.*) Scope Resolution Operator (::) Size Of Operator ( sizeof() ) Conditional Operator (?:) There are two ways to Overload Operators  in C++. They are; Using Member Function Using Friend Function IMPORTANT : For Overloading Operators ,We have to use 'operator' function.  Now lets have a look at syntax for overloading operators. Syn...