google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.

Thursday, February 26, 2015

Write a C++ program to create class currency using operator overloading

Write a C++ program to create class currency using operator overloading:


Create a class currency containing rupees and paisa as data members.
Write a necessary member function using operator overloading for the following.
1.currency(long int rup=0,int paisa=0)
2.currency &operator +=(currency &)to add one currency to another
3.currency & operator -=(currency &) to subtracts one currency from another accept rupees & paisa from user and display it.



#include
#include
class currency
{
int r,p;
public:currency(long int rs=0,int paisa=0)
       {
       r=rs;
       p=paisa;
       }
       currency  & operator +=(currency &);
       currency  & operator -=(currency &);
};
currency & currency :: operator +=(currency &c2)
{
int a,b;
a=r+c2.r;
b=p+c2.p;
if(b>=100)
{
b=b-100;
a=a+1;
}
cout<<"\n Addition of two currency is:-";
cout<<"\n Rupees:-"<<a;
cout<<"\n Paisa:-"<<b;
return *this;
}
currency & currency::  operator -=(currency &c2)
{
int a,b;
a=r-c2.r;
b=p-c2.p;
cout<<"\n Subtraction of two currency is:-";
cout<<"\n Rupees:-"<<a;
cout<<"\n Paisa:-"<<b;
return *this;
}
int main()
{
int rs,paisa;
clrscr();
cout<<"\n\t\t Accept two currency \n\n";
cout<<"\n Enter rupees:-";
cin>>rs;
cout<<"Enter paisa";
cin>>paisa;
currency c1(rs,paisa);
cout<<"\n Enter rupees:-";
cin>>rs;
cout<<"Enter paisa";
cin>>paisa;
currency c2(rs,paisa);
c1+=c2;
c1-=c2;
getch();
return 0;
}


===================
OUTPUT:-

        Accept two currency


Enter rupees:-12
Enter paisa34
Enter rupees:-45
Enter paisa34
Addition of two currency is:-
Rupees:-57
Paisa:-68
Subtractions of two currency is:-
rupees:--33
paisa:-0

Write a C++ program to reverse case of each alphabet in the string

Consider the following class mystring
class mystring
{
char str[100];
public:\\methods
};
overload operator "!" to reverse case of each alphabet in the string


#include<iostream.h>
#include<string.h>
#include<conio.h>
class mystring
{
char str[100];
public:void accept()
       {
       cout<<"\n Enter string:-";
       cin>>str;
       }
       void operator !();
};
void mystring::operator !()
       {
       int len;
       len=strlen(str);
       cout<<"\n Reverse string is:-";
       for(int i=len-1;i>=0;i--)
       cout<<str[i];
       }
int main()
{
mystring m1;
clrscr();
m1.accept();
!m1;
getch();
return 0;
}



======================================================
OUTPUT :-

Enter string:-stop

Reverse string is:-pots

अच्छे विचार करे विचार

  पहचान की नुमाईश, जरा कम करें... जहाँ भी "मैं" लिखा है, उसे "हम" करें... हमारी "इच्छाओं" से ज़्यादा "सुन...