Sunday, April 22, 2012

operator over loading

#include<iostream.h>
#include<conio.h>
class time
{
int hr;
   int min;
   int sec;
   public:
   time();
   time(int h, int m, int s);
   time (time &t);
   time operator +(time t);
   time operator -(time t);
   time operator *(time t);
   time operator /(time t);
   time operator ++();
   time operator --(int);
   time friend operator *(int , time t); // operators to
   time friend operator +(int , time t);// multiply int data type
   time friend operator -(int , time t);//   with time class object
   time friend operator /(int , time t);
   ///////////////////////////////////////
   time friend operator *( time t,int); // operators to
   time friend operator +( time t,int);// multiply time class object
   time friend operator -(time t,int); // with int data type.
   time friend operator /(time t, int);
   friend istream & operator >>(istream & in, time &t);
   friend ostream & operator <<(ostream & out,time &t);
   ~time()
   {
    cout<<"Time object destroyed \n";
      getch();
   }
};
time::time()
{
hr=0;min=0;sec=0;
}
time::time(int h, int m, int s)
{
hr=h;
   min=m;
   sec=s;
}
time::time(time &t)
{
hr=t.hr;
   min=t.min;
   sec=t.sec;

}

time time ::operator++()
{
hr++;
   min++;
   sec++;
return *this;
}
time time :: operator --(int )
{
hr--;
   min--;
   sec--;
return *this;
}
time time::operator +(time t)
{
time temp;
   temp.hr=hr+t.hr;
   temp.min=min+t.min;
   temp.sec=sec+t.sec;
   return temp;
}
time time::operator -(time t)
{
time temp;
   temp.hr=hr-t.hr;
   temp.min=min-t.min;
   temp.sec=sec-t.sec;
   return temp;
}
time time::operator *(time t)
{
time temp;
   temp.hr=hr*t.hr;
   temp.min=min*t.min;
   temp.sec=sec*t.sec;
   return temp;
}
time time::operator /(time t)
{
time temp;
   temp.hr=hr/t.hr;
   temp.min=min/t.min;
   temp.sec=sec/t.sec;
   return temp;
}
ostream & operator <<(ostream & out,time &t)
{
cout<<t.hr<<endl<<t.min<<endl<<t.sec<<endl;
   return out;
}
istream & operator >>(istream & in,time &t)
{
cin>>t.hr>>t.min>>t.sec;
   return in;
}
time operator +(int n, time t)
{
time temp;
   temp.hr=n+t.hr;
   temp.min=n+t.min;
   temp.sec=n+t.sec;
   return temp;
}
time operator -(int n, time t)
{
time temp;
   temp.hr=n-t.hr;
   temp.min=n-t.min;
   temp.sec=n-t.sec;
   return temp;
}
time operator *(int n, time t)
{
time temp;
   temp.hr=n*t.hr;
   temp.min=n*t.min;
   temp.sec=n*t.sec;
   return temp;
}
time operator /(int n, time t)
{
time temp;
   temp.hr=n/t.hr;
   temp.min=n/t.min;
   temp.sec=n/t.sec;
   return temp;
}
time operator +( time t,int n)
{
time temp;
   temp.hr=t.hr+n;
   temp.min=t.min+n;
   temp.sec=t.sec+n;
   return temp;
}
time operator -( time t,int n)
{
time temp;
   temp.hr=t.hr-n;
   temp.min=t.min-n;
   temp.sec=t.sec-n;
   return temp;
}
time operator *( time t,int n)
{
time temp;
   temp.hr=t.hr*n;
   temp.min=t.min*n;
   temp.sec=t.sec*n;
   return temp;
}
time operator /( time t,int n)
{
time temp;
   temp.hr=t.hr/n;
   temp.min=t.min/n;
   temp.sec=t.sec/n;
   return temp;
}
void  main()
{
clrscr();
time t1,t2,t3(5,2,3),t4(6,6,6);
   cout<<t3;
   t2=2*t3;
   cout<<t2;
   t1=t2+6;
   cout<<t1;


     getch();
}

Friday, March 16, 2012

Question No. 1 From classes excercise


STATEMENT: 1.       Define a class to represent  a bank account include the following members:
Data Members:
·         Name of the depositor
·         Account no
·         Type of the account
·         Balance amount in the account
               Member Functions:
·         Constructor with arguments to assign initial value
·         Destructor to destroy objects
·         Function to deposit an amount
·         Function to withdraw an amount after checking balance
·         Function to display name and balance
Write main() function and initialize three different objects in it.  Invoke all member functions with these three objects.
Solution :


#include<iostream.h>
#include<conio.h>
#include<string.h>
class bank
{
private:
char a[10];
int acc_no;
char typ[10];
int balance;
public:
bank(char h[10],int j,char k[10],int l)
{
strcpy(a,h);
acc_no=j;
strcpy(typ,k);
balance=l;
}
void deposit();
void withdraw();
void showdata();
~bank()
{
cout<<"object is going to destroy"<<endl;
showdata();
getch();
}
};
void main()
{
clrscr();
bank t1("ehsan",1,"BBA",100),t2("aqeel",2,"BBc",200);
cout<<"1st object"<<endl;
t1.showdata();
cout<<"For deposit ammount"<<endl;
t1.deposit();
cout<<"For with draw ammount"<<endl;
t1.withdraw();
cout<<"1st object updata"<<endl;
t1.showdata();
getch();
}
void bank::deposit()
{
int m;
cout<<"enter ammount to diposit"<<endl;
cin>>m;
balance=balance+m;
}
void bank::withdraw()
{
int n;
cout<<"your remaining balance"<<endl;
cout<<balance<<endl;
cout<<"Enter ammount enter"<<endl;
cin>>n;
balance=balance-n;
cout<<"your current balance is ="<<endl;
cout<<balance<<endl;
}
void bank::showdata()
{
cout<<"Account title"<<endl;
cout<<a<<endl;
cout<<"balanc="<<balance<<endl;
cout<<"=================================="<<endl;
}

Question no 3 from classes excercise...


STATEMENT:1.       Write a class electricity_bill that contains the following members:
Data Members:
·         Meter Id
·         Units consumed
·         amount of bill
              Member Functions:
·         constructor (default argument constructor with fixed amount bill 0)
·         destructor to destroy objects
·         Function to calculate the bill according to given criteria
§  For the first 100 units cost is 4 Rs per unit
§  For the next 200 units cost is 6 Rs per unit
§  Beyond 300 units cost is 8 Rs per unit
§  Add 50 RS as meter charges in the bill
§  If bill amount is greater than 300 Rs then add 15% additional surcharges in the bill
§  Finally update the bill field
·         Function to show the meter id, units consumed, and bill amount
In main initialize three objects with 15, 115, 315 units respectively, calculate the bill for each object and display the details of each object.

Solution:


#include<iostream.h> // class exc Q # 3
#include<conio.h>

class bill
{
private:
int id;
   int units;
   float t_bill;
   public:
   void cal_bill();
   bill(int i, int u)
   {
      id=i;
      units=u;
    t_bill=0;
   }
   void showdata();
   ~bill()
   {
    cout<<"Object gona be destroyed...!!!\n";
      showdata();
      cout<<"---\n";
      getch();
   }
};


//////////////////////
void main()
{
bill b1(123,15),b2(456,115),b3(789,315);
   cout<<"before calculating bill of object 1\n";
   b1.showdata();
   b1.cal_bill();
   cout<<"after calculating bill of object 1\n";
   b1.showdata();
   cout<<"before calculating bill of object 2\n";
   b2.showdata();
   b2.cal_bill();
   cout<<"after calculating bill of object 2\n";
   b2.showdata();
   getch();
}

void bill::showdata()
{
cout<<"Meter id = "<<id<<endl;
   cout<<"Units consumed = "<<units<<endl;
   cout<<"Total bill = "<<t_bill<<endl;
}

void bill::cal_bill()
{
if(units<=100)
   t_bill=units*4;
   else
   if(units<=300)
   t_bill=units*6;
   else//(units>300)
   t_bill=units*8;
   if(t_bill>300)
   {
    float temp;
      temp=(t_bill*15)/100;
      t_bill=t_bill+temp;
   }
}

Decimal to Binary conversion...


#include<iostream.h>
#include<conio.h>

void main ()

{
int num,c=-1,arr[1000],n;

 cout<<"Enter the number :";
 cin>>num;
 for(int i=num;i>0;i=i/2)
  {c++;
  arr[c]=i%2;
  n=c;
  }
  for(c=n;c>=0;c--)
  cout<<arr[c];
  getch();

}

Swapping Values of two variables without 3rd variable..


#include<iostream.h>
#include<conio.h>
void swap(int a,int b);
void main()
{
clrscr();
int x,y;
cout<<"Enter the 1st number";
cin>>x;
cout<<"Enter the 2nd Number";
cin>>y;
swap(x,y);
getch();
}
void swap(int a,int b)
{
a=a+b;
b=a-b;
a=a-b;
cout<<"\t 1st num="<<a<<"\t 2nd num="<<b;
}