OOPS LAB Programs


****************************BANKING APPLICATIONS*****************************

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

class bank
    {
    char name[20];
    int acno;
    char actype[4];
    float balance;
    public:
    void init();
    void deposit();
    void withdraw();
    void disp_det();
    };
//member functions of bank class
void bank :: init()
{
cout<<"\nNew Account";
cout<<"\n\nEnter the Name of the depositor : ";
cin>>name;
cout<<"\n\nEnter the Account Number : ";
cin>>acno;
cout<<"\n\nEnter the Account Type : (CURR/SAVG/FD/RD/DMAT) ";
cin>>actype;
cout<<"\n\nEnter the Amount to Deposit : ";
cin >>balance;
}

void bank :: deposit()
{
float more;
cout <<"Depositing";
cout<<"\nEnter the amount to deposit : ";
cin>>more;
balance+=more;
}

void bank :: withdraw()
{
float amt;
cout<<"\nWithdrwal";
cout<<"\n\nEnter the amount to withdraw : ";
cin>>amt;
balance-=amt;
}

void bank :: disp_det()
{
cout<<"\nAccount Details";
cout<<"------------------";
cout<<"\n\nName of the depositor : "<<name<<endl;
cout<<"\n\nAccount Number        : "<<acno<<endl;
cout<<"\n\nAccount Type          : "<<actype<<endl;
cout<<"\n\nBalance               : $"<<balance<<endl;
cout<<"=====================";
}
// main function , exectution starts here
void main(void)
{
clrscr();
bank obj;
int choice =1;
while (choice != 0)
{
cout<<"Enter 0 to exit \n1.Initialize a new acc.\n2. Deposit \n3.Withdraw \n4.See A/c Status";
cin>>choice;
switch(choice)
{
case 0 :obj.disp_det();
        cout<<"EXITING PROGRAM.";
        break;
    case 1 : obj.init();
        break;
    case 2: obj.deposit();
        break;
    case 3 : obj.withdraw();
        break;
    case 4: obj.disp_det();
        break;
    default: 
        cout<<"Illegal Option"<<endl;
}
}
getch();
}
**********************************************************************************
Simple Program Book Entry Using structure Variable in C++ Programming

Example Program

#include<iostream.h>
#include<stdio.h>

struct books
{
    char name[20],author[20];
}a[50];
int main()
{
    
    int i,n;
    cout<<"No Of Books[less than 50]:";
    cin>>n;
cout<<"Enter the book details\n";
cout<<"----------------------\n";
    
    for(i=0;i<n;i++)
    {
 cout<<"Details of Book No "<<i+1<<"\n";
 cout<<"Book Name :";
 cin>>a[i].name;
 cout<<"Book Author :";
 cin>>a[i].author;
 cout<<"----------------------\n";
    }
    cout<<"================================================\n";
    cout<<" S.No\t| Book Name\t|author\n";
    cout<<"=====================================================";
    for(i=0;i<n;i++)
    {
 cout<<"\n  "<<i+1<<"\t|"<<a[i].name<<"\t| "<<a[i].author;
    }
    cout<<"\n=================================================";
    
    return 0;

Sample Output

No Of Books[less than 50]:2
Enter the book details
----------------------
Details of Book No 1
Book Name :Programming
Book Author :Dromy
Details of Book No 2
Book Name :C
Book Author :Byron
=======================================================
 S.No | Book Name |author
=======================================================
  1 |Programming | Dromy
  2 |C | Byron
=======================================================
Calculate Prime Number Using Constructor

Example Program :

#include<iostream.h>
#include<conio.h>
class prime
{
                int a,k,i;
              public:
              prime(int x)
              {
                            a=x;
              }
              void calculate()
              {
                 k=1;
                {
                     for(i=2;i<=a/2;i++)
                  
       if(a%i==0)
                     {
                              k=0;
                              break;
                     }
                     else
                    {
                            k=1;
                  }
                }
              }
             
void show()
              {
                if(k==1)
                  cout<< “\n\tA is prime Number. ";
                else
                  cout<<"\n\tA is Not prime.";
              }
};
void main()
{
    clrscr();
    int a;
    cout<<"\n\tEnter the Number:";
    cin>>a;
    prime obj(a);
    obj.calculate();
    obj.show();
    getch();
}

Sample Output:

Enter the number: 7
Given number is Prime Number

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

PAYROLL SYSTEM USING SINGLE INHERITANCE

 Example Program :

#include<iostream.h>
#include<conio.h>
class emp
{
   public:
     int eno;
     char name[20],des[20];
     void get()
     {
              cout<<"Enter the employee number:";
              cin>>eno;
              cout<<"Enter the employee name:";
              cin>>name;
              cout<<"Enter the designation:";
              cin>>des;
     }
};
class salary:public emp
{
     float bp,hra,da,pf,np;
   public:
     void get1()
     {             
              cout<<"Enter the basic pay:";
              cin>>bp;
              cout<<"Enter the Humen Resource Allowance:";
              cin>>hra;
              cout<<"Enter the Dearness Allowance :";
              cin>>da;
              cout<<"Enter the Profitablity Fund:";
              cin>>pf;
     }
     void calculate()
     {
              np=bp+hra+da-pf;
     }
     void display()
     {
              cout<<eno<<"\t"<<name<<"\t"<<des<<"\t"<<bp<<"\t"<<hra<<"\t"<<da<<"\t"<<pf<<"\t"<<np<<"\n";
     }
};
void main()
{
    int i,n;
    char ch;
    salary s[10];
    clrscr();
    cout<<"Enter the number of employee:";
    cin>>n;
    for(i=0;i<n;i++)
    {
              s[i].get();
              s[i].get1();
              s[i].calculate();
    }
    cout<<"\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";
    for(i=0;i<n;i++)
    {
              s[i].display();
    }
    getch();
}

Output:

Enter the Number of employee:1
Enter the employee No: 150
Enter the employee Name: ram
Enter the designation: Manager
Enter the basic pay: 5000
Enter the HR allowance: 1000
Enter the Dearness allowance: 500
Enter the profitability Fund: 300
E.No   E.name   des      BP    HRA   DA   PF     NP
150    ram      Manager  5000  1000  500  300    6200
==========================================================================

Simple Program for Multiple Inheritance Using C++ Programming


To find out the student details using multiple inheritance.

ALGORITHM:

Step 1: Start the program.
Step 2: Declare the base class student.
Step 3: Declare and define the function get() to get the student details.
Step 4: Declare the other class sports.
Step 5: Declare and define the function getsm() to read the sports mark.
Step 6: Create the class statement derived from student and sports.
Step 7: Declare and define the function display() to find out the total and average.
Step 8: Declare the derived class object,call the functions get(),getsm() and display().
Step 9: Stop the program.

Example Program:

#include<iostream.h>
#include<conio.h>
class student
{
    protected:
       int rno,m1,m2;
    public:
                void get()
              {
                            cout<<"Enter the Roll no :";
                            cin>>rno;
                            cout<<"Enter the two marks   :";
                            cin>>m1>>m2;
              }
};
class sports
{
    protected:
       int sm;                   // sm = Sports mark
    public:
                void getsm()
              {
                 cout<<"\nEnter the sports mark :";
                 cin>>sm;
              }
};
class statement:public student,public sports
{
    int tot,avg;
    public:
    void display()
              {
                 tot=(m1+m2+sm);
                 avg=tot/3;
                 cout<<"\n\n\tRoll No    : "<<rno<<"\n\tTotal      : "<<tot;
               cout<<"\n\tAverage    : "<<avg;
              }
};
void main()
{
   clrscr();
   statement obj;
   obj.get();
   obj.getsm();
   obj.display();
   getch();
}

Output:

              Enter the Roll no: 100
              Enter two marks
             
              90
              80
              Enter the Sports Mark: 90
              Roll No: 100
              Total    : 260
              Average: 86.66
=========================================================================

Simple Program for Friend Function Using C++ Programming

To find the mean value of a given number using friend function.

ALGORITHM:

STEP 1:  Start the program.
STEP 2:  Declare the class name as Base with data members and member functions.
STEP 3:  The function get() is used to read the 2 inputs from the user.
STEP 4:  Declare the friend function mean(base ob) inside the class.
STEP 5:  Outside the class to define the friend function and do the following.
STEP 6:  Return the mean value (ob.val1+ob.val2)/2 as a float.
STEP 7:  Stop the program.

PROGRAM:

#include<iostream.h>
#include<conio.h>
class  base
{
    int val1,val2;
   public:
    void get()
    {
       cout<<"Enter two values:";
       cin>>val1>>val2;
    }
    friend float mean(base ob);
};
float mean(base ob)
{
   return float(ob.val1+ob.val2)/2;
}
void main()
{
    clrscr();
    base obj;
    obj.get();
    cout<<"\n Mean value is : "<<mean(obj);
    getch();
}          

Output:

Enter two values: 10, 20
Mean Value is: 15
 ===========================================================================

Simple Program for Function Overloading Using C++ Programming


To calculate the area of  circle, rectangle and  triangle using function overloading.

ALGORITHM:

STEP 1:  Start the program.
STEP 2:  Declare the class name as fn with data members and member functions.
STEP 3:  Read the choice from the user.
STEP 4:  Choice=1 then go to the step 5.
STEP 5:  The function area() to find area of circle with one integer argument.
STEP 6:  Choice=2 then go to the step 7.
STEP 7:  The function area() to find area of rectangle with two integer argument.
STEP 8:  Choice=3 then go to the step 9.
STEP 9:  The function area() to find area of triangle with three arguments, two as Integer and one as float.
STEP 10: Choice=4 then stop the program.

PROGRAM:

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define pi 3.14
class fn
{
      public:
        void area(int); //circle
        void area(int,int); //rectangle
        void area(float ,int,int);  //triangle
};
 
void fn::area(int a)
{
      cout<<"Area of Circle:"<<pi*a*a;
}
void fn::area(int a,int b)
{
      cout<<"Area of rectangle:"<<a*b;
}
void fn::area(float t,int a,int b)
{
      cout<<"Area of triangle:"<<t*a*b;
}
 
void main()
{
     int ch;
     int a,b,r;
     clrscr();
     fn obj;
     cout<<"\n\t\tFunction Overloading";
     cout<<"\n1.Area of Circle\n2.Area of Rectangle\n3.Area of Triangle\n4.Exit\n:”;
     cout<<”Enter your Choice:";
     cin>>ch;
 
     switch(ch)
     {
              case 1:
                cout<<"Enter Radious of the Circle:";
                cin>>r;
                obj.area(r);
                break;
              case 2:
                cout<<"Enter Sides of the Rectangle:";
                cin>>a>>b;
                obj.area(a,b);
                break;
              case 3:
                cout<<"Enter Sides of the Triangle:";
                cin>>a>>b;
                obj.area(0.5,a,b);
                break;
              case 4:
                exit(0);
     }
getch();
}

Output:

              Function Overloading
              1. Area of Circle
              2. Area of Rectangle
              3. Area of Triangle
              4. Exit
              Enter Your Choice: 2
 
              Enter the Sides of the Rectangle: 5 5
             
              Area of Rectangle is: 25
 
              1. Area of Circle
              2. Area of Rectangle
              3. Area of Triangle
              4. Exit
              Enter Your Choice: 4
 =========================================================================