Showing posts with label CPP. Show all posts
Showing posts with label CPP. Show all posts

Monday, June 6, 2016

c++ program to show operation on time, add seconds, add minutes, add hours, show time

#include<iostream.h>
#include<process.h>
class Time
{
 private:
  int hours;
  int minutes;
  int seconds;
 public:
  Time();
  void addhour(int);
  void addminute(int);
  void addsecond(int);
  void displaytime();
  void addtime();
  int gethour();
  int getminute();
  int getsecond();
  void sethour(int);
  void setminute(int);
  void setsecond(int);
};
Time::Time()
 {
  sethour(0);
  setminute(0);
  setsecond(0);
 }
void Time::addhour(int h)
 {
  h=gethour()+h;
  if(h>24)
   {
    h=h%24;
   }
  sethour(h);
 }
//function to get the value of hour
int Time::gethour()
 {
  return hours;
 }
//function to set the value of the hour
void Time::sethour(int h)
 {
  hours=h;
 }
//function to add the minutes in current minutes
void Time::addminute(int m)
 {
  m=getminute()+m;
  if(m>60)
   {
    m=m%60;
    addhour(1);
   }
  setminute(m);
 }
//function to get the value of current minutes
int Time::getminute()
 {
  return minutes;
 }
//function to set the value of current value
void Time::setminute(int m)
 {
  minutes=m;
 }
//function to add the seconds in current second value
void Time::addsecond(int s)
 {
  s=getsecond()+s;
  if(s>60)
   {
    s=s%60;
    addminute(1);
   }
  setsecond(s);
 }
//function to get the value of current second value
int Time::getsecond()
 {
  return seconds;
 }
//function to set the value of current second value
void Time::setsecond(int s)
 {
  seconds=s;
 }
void Time::displaytime()
 {
  cout<<"Time:: ";
  cout.width(2);
  cout.fill('0');
  cout<<gethour()<<":";
  cout.width(2);
  cout.fill(2);
  cout<<getminute()<<":";
  cout.width(2);
  cout.fill('0');
  cout<<getsecond();
 }
void Time::addtime()
 {
  int h,m,s;
  cout<<"Enter hours, minutes and seconds to add in time";
  cin>>h>>m>>s;
  addsecond(s);
  addminute(m);
  addhour(h);
 }
void main()
 {
  int i;
  Time t;
  int h;
  int m;
  int s;
  do
   {
    cout<<endl<<"Enter 1.Add hours \n2.Add minutes \n3.Add seconds \n4.Add time \n5.Show time \n6.Exit";
    cout<<endl<<"Enter your choice";
    cin>>i;
    switch(i)
     {
      case 1:
       cout<<endl<<"How many hours to add";
       cin>>h;
       t.addhour(h);
       break;
      case 2:
       cout<<endl<<"How many minutes to add";
       cin>>m;
       t.addminute(m);
       break;
      case 3:
       cout<<endl<<"How many seconds to add";
       cin>>s;
       t.addsecond(s);
       break;
      case 4:
       t.addtime();
       break;
      case 5:
       t.displaytime();
       break;
      case 6:
       exit(0);
      default:
       cout<<endl<<"Please enter a correct choice";
     }
   }while(i!=6);
 }

output
Enter 1.Add hours
2.Add minutes
3.Add seconds
4.Add time
5.Show time
6.Exit
1
How many hours to add5
Enter 1.Add hours
2.Add minutes
3.Add seconds
4.Add time
5.Show time
6.Exit
2
How many minutes to add25
Enter 1.Add hours
2.Add minutes
3.Add seconds
4.Add time
5.Show time
6.Exit
5
Time::05:25:00
Enter 1.Add hours
2.Add minutes
3.Add seconds
4.Add time
5.Show time
6.Exit
6

Tuesday, May 10, 2016

C++ program to explain default, parameterized and copy constructor

#include<iostream.h>
class vector
{
 public:
  int *a;
  int b;
//default constructor
 vector()
  {
   a = new int;
   *a=10;
  }
//parameterized constructor
 vector(int k)
  {
   a = new int;
   *a=k;
  }
 void getvalue()
  {
   cout<<"a="<<*a<<"\n";
  }
//copy constructor
  vector(vector &v)
  {
  a = new int;
  *a=*v.a;
  }
};
void main()
{
 vector vector1; //invoke default constructor
 vector1.getvalue();
 vector vector2(25); //invoke parameterized constructor
 vector.getvalue();
 vector vector3;
 vector vector4(vector3); //invoke copy constructor
 vector4.getvalue();
}
output
a=10
a=25
a=10

Saturday, March 12, 2016

C ++ program for bank account management

#include<iostream.h>
#include<conio.h>
 class account
{
char c_name[20];
int acc_no;
char acc_type;
public:
float balance;
void get_balance(int a)
{
   balance=a;
   penalty();
}
void put_balance()
{
cout<<"\nyour current balance is: "<<balance;
}
void debit()
{
int deb;
cout<<"\nenter ammount to be withdrawl: ";
cin>>deb;
balance=balance-deb;
put_balance();
penalty();
}

void credit()
 {
 int cre;
   cout<<"\nenter ammount to be credited: ";
  cin>>cre;
   balance=balance+cre;
   put_balance();
 }
/*void intrest()
{
 int intr;
 intr=balnce
 balance=balance+intr;
 cout<<"\ntotal intrest added:"<<intr;
 put_balance();
} */
void penalty()
{
 float pen=0;
 float f=0.05;
 if(balance<500)
 {
 cout<<"\nyour balance is less then 500";
 pen=balance*f;
 balance=balance-pen;

 cout<<"\nyour penalty amount is: "<<pen;
 put_balance();
 }
 else
 cout<<"\nno penalty";
}
};
void main()
{
clrscr();
account acc;
acc.get_balance(5000);
acc.put_balance();
acc.credit();
acc.debit();
getch();
}

output