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

Monday, February 23, 2015

Write a C++ program using class to overload following binary operators

Write a C++ program using class to overload following binary operators (+,-,*,/).

#include<iostream.h>
#include<conio.h>
class number
{ int a;
public:void get();
       void show();
       number operator +(number);
       number operator -(number);
       number operator *(number);
       number operator /(number);
};
void number:: get()
{
 cout<<"\nenter value";
 cin>>a;
 }
 void number:: show()
 {
 cout<<a;
 }
 number number::operator+(number n2)
 {
 number t;
 t.a=a+n2.a;
 return t;
 }
 number number::operator-(number n2)
 {
 number t;
 t.a=a-n2.a;
 return t;
 }
 number number::operator*(number n2)
 {
 number t;
 t.a=a*n2.a;
 return t;
 }
 number number::operator/(number n2)
 {
 number t;
 t.a=a/n2.a;
 return t;
 }
 int main()
 {
 number n1,n2,n3;
 clrscr();
 n1.get();
 n2.get();
 cout<<"\n First num ";
 n1.show();
 cout<<"\n Second num ";
 n2.show();
 n3=n1+n2;
 cout<<"\n Addition is ";
 n3.show();
 n3=n1-n2;
 cout<<"\n Subtraction is ";
 n3.show();
 n3=n1*n2;
 cout<<"\n Multiplication is ";
 n3.show();
 n3=n1/n2;
 cout<<"\n Division is ";
 n3.show();
 getch();
 return 0;
 }







===================================================
                                                             OUTPUT
Enter value4                                                                   
                                                                               
Enter value2                                                                   
                                                                               
First num4                                                                     
Second num 2                                                                    
Addition is 6                                                                   
Subtraction is 2                                                                
Multiplication is 8                                                             
Division is 2

Write Cpp program to represent bank account using member function

Define a class to represent a bank account which include following member
data member:-
1)name
2)Account number
3)type of account
4)bal.amt
member function:
a.to assign initial value
b.to deposit an account
c.to withdraw an account
d.to display name,account number and balance

#include<conio.h>
#include<iostream.h>
#include<string.h>
int w_amt;
class bank
{
public:int acc_no,bal;
char type[30],name[30];
public:void assign()
       {
       strcpy(name,"Vijay");
       acc_no=111;
       strcpy(type,"saving");
       bal=12000;
       }
       void deposit()
       {
       int amt;
       cout<<"\n Enter the amount which you want to deposit:-";
       cin>>amt;
       bal=bal+amt;
       }
       void withdraw()
       {
       cout<<"\n Enter how much money you want to withdraw";
       cin>>w_amt;
       }
       void display()
       {
       cout<<"\n Name:-"<<name
       <<"\n Account number:-"<<acc_no
       <<"\n Type of account:-"<<type
       <<"\n Balance amount:-"<<bal;
       }
};
int main()
{
bank b;
clrscr();
b.assign();
b.deposit();
b.withdraw();
if(w_amt>12000)
{
cout<<"You do not have that much balance try again";
}
else
b.bal=b.bal-w_amt;
b.display();
getch();
return 0;
}


==============================================

OUTPUT

Enter the amount which you want to deposit:-1200
Enter how much money you want to withdraw200
Name:-Vijay
Account number:-111
Type of account:-saving
Balance amount:-13000

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

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