Saturday, 11 January 2014

C++ Programm using oops concepts

A program to creat a base Class shape. Use this Class to store two double type values that could be used to compute the area of figures. Derive two specific classes triangle and rectangle from the base class shape.Add to the base class a member function get_data() to initialize base class data members and another function display_area() to compute and display the area of figures.Make the display_area function a virtual function and redefine this function in the derived classes to suit their requirements.
Using these three classes design a program that will accept dimensions of a triangle or a rectangle interactively and display the area.


#include<iostream.h>
#include<conio.h>
class shape{

Advance Time Program in C++

Create a Class time that has separate int member data for hours,minutes and seconds.One constructor should initialize this data to 0 and another should initial;ize it to fixed values. Another member function should display it in 11:59:59 format. The final member function should add two time objects passed as arguments.

#include<iostream.h>
#include<conio.h>
class time
{private:int h;
             int m;
             int s;
 public:time()
            {h=0;m=0;s=0;}
            time(int a,int b,int c)
            {h=a;m=b;s=c;}
            void getdata();
            void showdata()
            {cout<<"\n the time -> "<<h<<":"<<m<<":"<<s<<".";
             cout<<"\n";
             getch();
            }
            void sum(time a,time b)
            {int t=0;
             h=a.h+b.h;
             m=a.m+b.m;
             s=a.s+b.s;
             if(s>60)
             {t=s/60;
              s=s-(60*t);
              m=m+t;
             }
             t=0;
             if(m>60)
             {t=m/60;
              m=m-(60*t);
              h=h+t;
             }
            }
}t1,t2,t3;
void time :: getdata()
{int c=0,t=0;
 clrscr();
 while(c==0)
 {cout<<"\n enter the parameters of time \n";
  cout<<"\n enter seconds: ";
  cin>>s;
  if(s<0)
  {cout<<"\n please enter the positive value \n";
  }
  else
  {if(s>60)
   {t=s/60;
    s=s-(t*60);
   }
   c=1;
  }
 }
 c=1;
 while(c==1)
 {cout<<"\n enter minutes: ";
  cin>>m;
  if(m<0)
  {cout<<"\n you have entered wrong value enter again";
  }
  else
  {m=m+t;
   if(m>60)
   {t=m/60;
     m=m-(t*60);
   }
   else
   {t=0;}
   c=0;
  }
 }
 c=0;
 while(c==0)
 {cout<<"\n enter hour: ";
  cin>>h;
  if(h<0)
  {cout<<"\n sorry you have entered wrong value enter again";
  }
  else
  {h=h+t;
   c=1;
  }
 }
}
void main()
{int n,i;
 clrscr();
 t1.getdata();
 t1.showdata();
 t2.getdata();
 t2.showdata();
 cout<<" sum of";
 t3.sum(t1,t2);
 t3.showdata();
 getch();
} 

A program in C++ to create a Class Octal to perform decimal to octal conversion and addition of an octal and an interger by overloading ‘+’ and ‘=’.

#include<iostream.h>
#include<conio.h>
class octal
{private:int ov;
 public:octal()
            {ov=0;}
            void getdata();
            void octcon(int);
            octal operator=(int a);
            int operator+(int a);
            void showdata(int iv,int x)
            {cout<<"\n the decimal number:";
             cout<<iv;
             cout<<"\n the sum:"<<ov;
             getch();
            }
            void showdata(int iv)
            {cout<<"\n the octal number:";
             cout<<ov;
             cout<<"\n the decimal number:";
             cout<<iv;
             getch();
            }
}n1,n2;
void octal :: getdata()
{int x=0,i=0,temp,c=0;
 cout<<"\n for addition \n";
 while(x!=1)
 {temp=0;i=0;c=0;
  cout<<"\n enter octal number to be added:";
  cin>>ov;
  temp=ov;
  while(i==0)
  {if((temp%10)>7)
   {c++;}
   temp=temp/10;
   if(temp==0)
   {i=1;}
  }
  if(c!=0)
  {cout<<"\n invalid value enter again \n";
  }
  if(c==0)
  {x=1;}
 }
}
int octal :: operator+(int v)
{int c=0,l,k=1,temp;
 temp=ov;
 while(temp!=0)
 {l=temp%10;
  l=l*k;
  c=c+l;
  temp=temp/10;
  k=k*8;
 }
 temp=c+v;
 return temp;
}
octal octal:: operator=(int iv)
{int r=0,i=0,j,l,k,temp;
 temp=iv;
 while(temp!=0)
 {k=1;
  for(j=0;j<i;j++)
  {k=k*10;}
  l=temp%8;
  temp=temp/8;
  r=k*l+r;
  i++;
 }
 ov=r;
}
void octal :: octcon(int iv)
{int r=0,i=0,j,l,k,temp;
 temp=iv;
 while(temp!=0)
 {k=1;
  for(j=0;j<i;j++)
  {k=k*10;}
  l=temp%8;
  temp=temp/8;
  r=k*l+r;
  i++;
 }
 ov=r;
}
void main()
{int c=0,k=0,v;
 clrscr();
 while(k==0)
 {clrscr();
  cout<<"\n enter the choice of operation to be performed \n1-conversion of decimal to octal \n2-addition of an octal and an integer \n3-exit\n \n enter choice:";
  cin>>c;
  switch(c)
  {case 1:cout<<"\n enter the integer value to be converted:";
              cin>>v;
              n1.octcon(v);
              n1.showdata(v);
              break;
   case 2:n1.getdata();
              cout<<"\n enter the integer value to be added:";
              cin>>v;
              n2=n1+v;
              n2.showdata(v,0);
              break;
   case 3:k=1;
              cout<<"\n thanks for using the program";
              break;
   default:cout<<"\n you have entered wrong choice try again";
               getch();
  }
 }
 getch();

}

program in C++ to create a linked list

A program in C++ to create a  linked list  with insert_front and delete_front as member functions by creating to objects. Use constructor to initialize the objects.

#include<iostream.h>
#include<conio.h>
struct node
{int info;
 struct node *next;
};
typedef struct node node;
class Linklist
{

A program in C++ using classes to enter the data of an employee and display it.

#include<iostream.h>
#include<conio.h>
class Emp
{public:char name[10];
            int age;
            int salary;
public:void init();
       void display();
};
void main()
{clrscr();
 Emp obj;
 obj.init();
 obj.display();
 getch();
}
void Emp::init()
{Emp obj;
 cout<<"\n ENTER THE NAME ==> \n";
 cin>>obj.name;
 cout<<"\n ENTER THE AGE ==> \n";
 cin>>obj.age;
 cout<<"\n ENTER THE SALARY ==> \n";
 cin>>obj.salary;
}
void Emp::display()
{Emp obj;
 cout<<"\n EMPLOYEE'S NAME \n"<<obj.name;
 cout<<"\n EMPLOYEE'S AGE \n"<<obj.age;
 cout<<"\n EMPLOYEE'S SALARY \n"<<obj.salary;
}


A program in C++ to create a Class Date and add and subtract two valid dates. Illustrate the use of copy constructor.

#include<iostream.h>
#include<conio.h>
class date
{private:int d;
             int m;
             int y;
 public:date()
            {d=0;m=0;y=0;}
            date(int a,int b,int c)
            {d=a;m=b;y=c;}
             void getdata();
             void showdata()
             {
             

Program Of Complex number

A program in C++ to create Class called complex and implement addition and subtraction of complex numbers by overloading the functions add and subtract which returns the complex number. Also overload the operator “<<” to display a complex number.

Small Project Of Banking System in C++

Assume that a bank maintains two kinds of accounts for its customers, one called as savings account and other as current account. The savings account provides compound interest and withdrawal facilities but no checkbook facility. The current account provides checkbook facility but no interest. Current account holders should maintain a minimum balance  and if the balance falls below this level as service charge is imposed.
Create a Class account that stress customer name, account number and type of account. From this derive Classes cur_account and sav_account to make them more specific to their requirements include necessary member functions in order to achieve the following tasks:
1) Accept deposits from a customer and update the balance.
2) Display the balance.
3) Compute and deposit the interest.
4) Permit withdrawal and update the balance.
5)Check for the minimum balance and impose penalty necessary and update the balance.

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<string.h>
class Account
{private:char c_name[25];
             int acc_no;
             char type[7];
 public:void read(int);
            void display();
};
class Sav_acc:public Account
{private:float balance;
             float debit;
             float deposit;
             float interest;
 public:void update();
};
class Cur_acc:public Account
{private:float balance;
             int pages_ch;
             float debit;
             float deposit;
 public:void update_cur();
};
void Account::read(int x)
{int i=0,j=0;
 char sav[7]="savings",cur[7]="current";
 clrscr();
 cout<<"\nEnter your account number:";
 cin>>acc_no;
 cout<<"\nEnter your name:";
 cin>>c_name;
 if(x==1)
 {for(i=0;i<7;i++)
  {type[i]=sav[i];
  }
 }
 else
 {for(j=0;j<7;j++)
  {type[j]=cur[j];
  }
 }
}
void Sav_acc::update()
{int amt;
 cout<<"\n enter the balance:";
 cin>>balance;
 cout<<"\n Enter the amount of money to be withdrawn:";
 cin>>debit;
 cout<<"\n The balance after the withdrawal is:";
 balance=balance-debit;
 cout<<balance;
 cout<<"\n Enter the amount to be deposited:";
 cin>>deposit;
 cout<<"\n The amount after deposit is:";
 balance=balance+deposit;
 cout<<balance;
 cout<<"\n The compound interest for the two years is  3.5% \n";
 amt=(1+(3.5/100));
 interest=pow(amt,2);
 cout<<"\n The balance after the compound interest is:";
 balance=balance+interest;
 cout<<balance;
}
void Cur_acc::update_cur()
{int c=0;
 char ch;
 balance=0;
 cout<<"\n Enter the amount to be deposited:";
 cin>>deposit;
 cout<<"\n The amount after deposit is:";
 balance=balance+deposit;
 cout<<balance;
 cout<<"\n Enter the amount of money to be withdrawn:";
 cin>>debit;
 cout<<"\n The balance after the withdrawal is:";
 balance=balance-debit;
 cout<<balance;
 if(balance<1000)
 {cout<<"\n The balance in the account is less than the limit";
  balance=balance-100;
  cout<<"\n The balance after the penalty is:"<<balance;
 }
 while(c==0)
 {cout<<"\n do you want to issue the cheque book (y-yes,n-no): " ;
  cin>>ch;
  if(ch=='y')
  {cout<<"\n Enter the number of pages to be issued of the cheque book:";
   cin>>pages_ch;
   cout<<"\n the book will be issued to you after two days:";
   c=1;
  }
  else
  {if(ch=='n')
   {c=1;}
   else
   {cout<<"\n sorry you have entered wrong choice:";
    getch();
   }
  }
 }
}
void Account::display()
{cout<<"\nThe account number is:"<<acc_no;
 cout<<"\nThe name of the customer is:"<<c_name;
 cout<<"\nThe type of account is:"<<type;
 getch();
}
void main()
{int i=0,c=0;
 clrscr();
 while(i==0)
 {clrscr();
  cout<<"\n enter the choice of operation to be performed \n  1:savings \n  2:current \n  3:exit \n  enter choice:";
  cin>>c;
  switch(c)
  {case 1:Sav_acc a;
              a.read(c);
              a.update();
              a.display();
              getch();
              break;
   case 2:Cur_acc b;
              b.read(c);
              b.update_cur();
              b.display();
              getch();
              break;
   case 3:i=1;
              cout<<"\n thanks for using our services:";
              getch();
              break;
   default:cout<<"\n sorry you have entered wrong choice enter again:";
               getch();
  }
 }

}

Friday, 10 January 2014

A program in C++ using classes to find out whether the entered number is automorphic or not.

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{int n,r=0,i,s=0,m,a,b,l,k=0;
cout<<"enter a number";
clrscr();
cin>>n;
l=n;
 while(l!=0)
 {r=l%10;
 s=s+1;
 l=l/10;
 }
 b=n*n;
 m=pow(10,s);
 k=b/m;
 a=b-m*k;
 if (a==n)
  {cout<<"automorphic number"<<n;}
 else
  {cout<<"not automorphic";}
  getch();
}




1-A program in C++ using classes to calculate the area of a circle , a rectangle and a triangle on user’s choice.

#include<iostream.h>
#include<conio.h>
class ar
{private:float a;
 public:ar()
            {a=0;}
            ~ar()
            {a=0;}
            void area(float x)
            {a=3.4142*x*x;
            }
            void area(float x,float y)
            {a=x*y;
            }
            void area(float x,float y,int u)
            {a=(x*y)/2;
            }
            void showarea()
            {cout<<"\n the area of the figure is:"<<a;
            }
}n1;
void main()
{int n=0,i;
 float h,b,r;
 clrscr();
 while(n!=1)
 {clrscr();
  cout<<"\n enter the choice of figure whose area is to be calculated \n1-circle\n2-rectangle\n3-triangle\nenter your choice:";
  cin>>i;
  switch(i)
  {case 1:cout<<"\nfor circle\n";
              cout<<"\n enter the radius of the circle:";
              cin>>r;
              n1.area(r);
              n1.showarea();
              break;
   case 2:cout<<"\nfor rectangle\n";
              cout<<"\n enter the  length of the rectangle:";
              cin>>h;
              cout<<"\n enter the width of the rectangel:";
              cin>>b;
              n1.area(h,b);
              n1.showarea();
              break;
   case 3:cout<<"\nfor triangle\n";
              cout<<"\n enter the  height of the triangle:";
              cin>>h;
              cout<<"\n enter the base length of the triangel:";
              cin>>b;
              n1.area(h,b,1);
              n1.showarea();
              break;
   default:cout<<"\n sorry you have entered wrong choice\n";
  }
  cout<<"\n do you want to continue (yes:0,no:1):";
  cin>>n;
 }
 getch();
}