01-25-2011, 03:22 AM
Code:
#include <iostream.h>
#include <conio.h>
using namespace std;
class price{
//Declaring private data members
private:
int rupees;
int paisas;
//Declaring public functions
public:
price(); //Default constructor
price (int,int); //Overloaded constructor
void Print(); //print function for displaying the array elements
price operator+(price); //Operator overloading function for operator +
};
// Default constructor
price::price()
{
rupees = 0;
paisas = 0;
}
// Overload constructor
price::price(int crupees, int cpaisas)
{
rupees = crupees;
paisas = cpaisas;
}
//Function to display the elements
void price::Print()
{
cout<<"\nPrice is "<<rupees<<" Rupees and "<<paisas<<" Paisas "<< endl;
}
// Function for + operator overloading
price price::operator+(price d2)
{
int r = rupees + d2.rupees; //adding rupeees
int p = paisas + d2.paisas; //adding paisas
if (p>=100) //If paisas exceeds from 100 then adding a
{
p-=100; // them into rupees
r++;
}
return price (r,p);
}
main()
{
price result;
price obj1(10, 60);
price obj2 (12, 80);
result = obj1+obj2;
obj1.Print();
cout<<"\n";
obj2.Print();
cout<<"\n";
cout<<"After Addition\n";
result.Print();
getch();
return 0;
system("pasure");
}
