the code that i have coded is of operator overloading for incrementing and the i need some help in the operator overloading for decrementing ,mutiplication and division please send the following in a code form shall be thankful..

#include<iostream>


class Weight
{
  private:
    int kg,grms;

    void ConvertWeight()
    {
	if(grms >= 1000) {
	    kg += grms/1000;
	    grms = grms%1000;
	}
    }

  public:
    Weight()
    {
	kg = grms = 0;
    }

    Weight(int k,int g)
    {
	kg = k;
	grms = g;
       //	ConvertWeight();

    }
void operator ++()
	  {
	   kg++;
	   grms+=10;
	  }
	  void operator ++(int)
	  {
		kg++;
		grms+=10;
	  }

    void getWeight()
    {
	cout << endl << "Enter KG : ";cin>>kg;
	cout << "Enter Grams : ";cin>>grms;
   //     ConvertWeight();
    }

    void printWeight()
    {
	cout << "KG : " << kg;
	cout << "     Grams : " << grms;
	cout << endl;

    }

    void AddWeight(int k,int g)
    {
	kg   += k;
	grms += g;
       //	ConvertWeight();

    }

    void AddWeight(Weight w)
    {
	kg   += w.kg;
	grms += w.grms;
	ConvertWeight();
    }

    int equals(Weight w)
    {
	if((kg == w.kg) && (grms == w.grms)) {
	   return 1;
	}
	else {
	   return 0;
	}
    }
};

int main()
{
	
	Weight Marker,Duster(2,50);
	Marker.AddWeight(2,5050);
	cout<<"\nMarker :";
	Marker.printWeight();
	cout<<"\nDuster :";
	Duster.printWeight();
	if(Duster.equals(Marker))
	cout<<"\nBoth are Equal in Weight :";
	else
	cout<<"\nBoth are Different in Weight :";
	cout<<"\n\nAfter Adding Weights Of Marker & Duster :";
	cout<<"\n\nDuster + Marker :";
	Duster.AddWeight(Marker);
	Duster.printWeight();


		if(Duster.equals(Marker))
			cout<<"\nAfter Adding Weights Of Duster And Marker Both are Equal in Weight :";
		else
			cout<<"\nAfter Adding Weights Of Duster And Marker Both are Different in Weight :";







	cout<<"\n\n\nThe Operator function :";
	cout<<"\nFor Marker";
	++Marker;
	Marker.printWeight();
	cout<<"For Duster :";
	Duster++;
	Duster.printWeight();

return 0;
}

Recommended Answers

All 3 Replies

the code that i have coded is of operator overloading for incrementing and the i need some help in the operator overloading for decrementing ,mutiplication and division please send the following in a code form shall be thankful..

What's the question? You need to ask a specific question and point us to some difficulty you are having. Sounds like you want someone to turn the code below into decrementing , multiplication and division operators and send you the finished product or something?

void operator ++()
	  {
	   kg++;
	   grms+=10;
	  }
	  void operator ++(int)
	  {
		kg++;
		grms+=10;
	  }

why assignment operator not be overloaded as friend function

Member Avatar for jencas

Your operators ++ are looking strange. As an example here the operators ++ for int:

// prefix increment
 int operator ++ (int i)
 {
        i = i + 1;
        return i;
 }

 // postfix increment
 int operator ++ (int i, int)
 {
        int temp = i;
        i = i + 1;
        return temp;
 }
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.