Dear guys, Here I'm gonna show u how to overload different operators in c++.

First of all I'm going to overload unary operator(the operator that acts on one operand) that are ++ and --. so lets overload the -- with a simple example.

#include<iostream>
using namespace std;
class school         // create a class school
{
      private:
       int clsrooms;
      public:
      school() : clsrooms(10)   // ctor to make classrooms to 10 value
        {  }
      school(int c) : clsrooms(c)       // for nameless temp object>next 
      {  }
      int getrooms()
       {
         return clsrooms;
        }
      school operator -- (school);      // Declaration for main overld func
};

 school school::operator--(school s)
{ 
   int crms= clsroom-s.rooms;    // subtract one from another
   return school(crms);    // return nameless temp object to call        
// function, the object which dont have any name, here I've initize that to crms, the number of classrooms.
 }

///////////////////////////////////////////////////////////////////
 int main()   / / main function
{
   school s1;  
   school s2;   
    --s1;   // s1 now 9
s2= --s1;   // s1=8 and s2=8
cout<<" s1 = " <<s1.getrooms();
cout<<"\n s2= "<<s2.getrooms();
return 0;
}

So the output will be

s1= 8
s2=8

Throw some code tags around there so it's a bit more readable, but thanks for the tutorial. Is this the right place to post these? I have a handful of things like this that may be useful to others - where should we put them?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.