EXERCISE - 6

A) Create a Distance class with:
  • feet and inches as data members
  • member function to input distance
  • member function to output distance
  • member function to add two distance objects
Write a main function to create objects of DISTANCE class. Input two distances and output the sum.
B) Write a C++ program demonstrating a Bank Account with necessary methods and variables.
A)AIM: Create a Distance class with:
  • feet and inches as data members
  • member function to input distance
  • member function to output distance
  • member function to add two distance objects
Write a main function to create objects of DISTANCE class. Input two distances and output the sum.
SOURCE CODE:
#include<math.h>
#include<iostream>
using namespace std;
class Distance
{
    float feet,inch;
    public: Distance()
    {
        feet=0.0;
        inch=0.0;
    }
    void read_dist();
    void display_dist();
    void add(Distance , Distance);
};
void Distance::read_dist()
{
    cout<<"Enter distance(feet and inches):";
    cin>>feet>>inch;
}
void Distance::display_dist()
{
    cout<<"Distance Feet:"<<feet<<", Inches:"<<inch<<endl;
}
void Distance::add(Distance x,Distance y)
{
    inch=x.inch+y.inch;
    feet=x.feet+y.feet;
    if(inch>=12.0)
    {
        feet=x.feet+y.feet+(inch/12.0);
        inch=(int)inch%12;
    }
}
int main()
{
    Distance d1,d2,d3;
    cout<<"Enter first measure:\n";
    d1.read_dist();
    cout<<"Enter second measure:\n";
    d2.read_dist();
    d3.add(d1,d2);
    d3.display_dist();
}
OUTPUT:
Enter first measure:
Enter distance(feet and inches):
5
9
Enter second measure:
Enter distance(feet and inches):
7
6
Distance Feet:13.25, Inches:3
B)AIM: Write a C++ program demonstrating a BankAccount with necessary methods and variables.
SOURCE CODE:
#include<iostream>
using namespace std;
class Bank
{
    int accno;
    char name[20];
    float bal;
    public:
    void create()
    {
        cout<<"Enter account number:"<<endl;
        cin>>accno;
        cout<<"Enter name:"<<endl;
        cin>>name;
        cout<<"Enter minimum balance:"<<endl;
        cin>>bal;
    }
    void deposit()
    {
        int amtdeposit;
        cout<<"Enter amount to deposit:"<<endl;
        cin>>amtdeposit;
        bal=bal+amtdeposit;
        cout<<"After deposit balance is:"<<bal<<endl;
    }
    void withdraw()
    {
        int amtwithdraw;
        wd:
        cout<<"Enter amount to withdraw:"<<endl;
        cin>>amtwithdraw;
        if(amtwithdraw>bal)
        {
            cout<<"Insufficient balance."<<endl;
            goto wd;
        }
        bal=bal-amtwithdraw;
        cout<<"Remaining balance is:"<<bal<<endl;
    }
    void balance()
    {
        cout<<"Account balance is:"<<bal<<endl;
    }
};
int main()
{
    Bank b;
    int op;
    cout<<"Create account:"<<endl;
    b.create();
    cout<<"MENU:\n1. Deposit\n2. Withdraw\n3. Balance Enquiry \n4. Exit\n";
    cout<<"Select your option:";
    cin>>op;
    switch(op)
    {
        case 1: b.deposit();
            break;
        case 2: b.withdraw();
            break;
        case 3: b.balance();
            break;
        case 4: break;
        default: cout<<"Invalid transaction."<<endl;
    }
}
OUTPUT:
Create account:
Enter account number:
53162
Enter name:
RISE
Enter minimum balance:
10000
MENU:
1. Deposit
2. Withdraw
3. Balance Enquiry
4. Exit
Select your option:1
Enter amount to deposit:
4000
After deposit balance is:14000