5.1

Define a class to represent a bank account. Include the following members:

Data members:

  • Name of the depositor.
  • Account number.
  • Type of account.
  • Balance amount in the account.
    Member functions:
  • To assign initial values.
  • To deposit an amount.
  • To withdraw an amount after checking the balance.
  • To display the name and balance.
    Write a main program to test the program.
Answer
#include<iostream>
#include<iomanip>
using namespace std;
class bank
{
    char name[40];
    int ac_no;
    char ac_type[20];
    double balance;
public:
    int assign(void);
    void deposite(float b);
    void withdraw(float c);
    void display(void);
};
 
int bank::assign(void)
{
    float initial;
    cout<<" You have to pay   500 TK to open your account \n"
    <<" You have to store at least 500 TK to keep your account active\n"
    <<"Would you want to open a account????\n"
    <<" If Yes press 1 \n"
    <<" If No press 0 : ";
    int test;
    cin>>test;
    if(test==1)
    {
        initial=500;
        balance=initial;
      cout<<" Enter name ,account number & account type to creat account : \n";
         cin>>name>>ac_no>>ac_type;
    }
    else
    ;// do nothing
 
    return  test;
 
}
void bank::deposite(float b)
{
    balance+=b;
}
void bank::withdraw(float c)
{
    balance-=c;
    if(balance<500)
    {
        cout<<" Sorry your balance is not sufficient to withdraw "<<c<<"TK\n"
             <<" You have to store at least 500 TK to keep your account active\n";
                balance+=c;
    }
}
void bank::display(void)
{
    cout<<setw(12)<<"Name"<<setw(20)<<"Account type"<<setw(12)<<"Balance"<<endl;
    cout<<setw(12)<<name<<setw(17)<<ac_type<<setw(14)<<balance<<endl;
}
 
int main()
{
    bank account;
 
    int  t;
    t=account.assign();
    if(t==1)
    {
        cout<<" Would you want to deposite: ?"<<endl
        <<"If NO press 0(zero)"<<endl
        <<"If YES enter deposite ammount :"<<endl;
        float dp;
        cin>>dp;
        account.deposite(dp);
        cout<<" Would you want to withdraw : ?"<<endl
        <<"If NO press 0(zero)"<<endl
        <<"If YES enter withdrawal ammount :"<<endl;
        float wd;
        cin>>wd;
        account.withdraw(wd);
        cout<<" see details :"<<endl<<endl;
        account.display();
    }
            else if(t==0)
    cout<<" Thank you ,see again\n";
    return 0;
}

OUTPUT

You have to pay 500 TK to open your account
You have to store at least 500 TK to keep your account active
Would you want to open a account????
If Yes press 1
If No press 0 : 0
Thank you ,see again

5.2

Write a class to represent a vector (a series of float values). Include member functions to perform the following tasks:

  • (a) To create the vector.
  • (b) To modify the value of a given element.
  • (c) To multiply by a scalar value.
  • (d) To display the vector in the form (10, 20, 30 …)

Write a program to test your class.

Answer
#include<iostream>
#include<iomanip>
using namespace std;
class vector
{
    float *p;
    int size;
public:
    void creat_vector(int a);
    void set_element(int i,float value);
    void modify(void);
    void multiply(float b);
    void display(void);
};
 
void vector::creat_vector(int a)
{
    size=a;
    p=new float[size];
}
void vector::set_element(int i,float value)
{
    p[i]=value;
}
void vector :: multiply(float b)
{
    for(int i=0;i<size;i++)
        p[i]=b*p[i];
}
void vector:: display(void)
{
      cout<<"p["<<size<<"] = ( ";
      for(int i=0;i<size;i++)
      {
          if(i==size-1)
              cout<<p[i];
          else
          cout<<p[i]<<" , ";
 
      }
      cout<<")"<<endl;
}
 
void vector::modify(void)
{
    int i;
    cout<<" to edit a given element enter position of the element : ";
    cin>>i;
    i--;
    cout<<" Now enter new value of "<<i+1<<"th  element : ";
    float v;
    cin>>v;
    p[i]=v;
    cout<<" Now new contents : "<<endl;
    display();
 
    cout<<" to delete an element enter position of the element :";
    cin>>i;
    i--;
 
    for(int j=i;j<size;j++)
    {
        p[j]=p[j+1];
    }
    size--;
    cout<<" New contents : "<<endl;
    display();
}
 
int main()
{
    vector santo;
    int s;
    cout<<" enter size of vector : ";
    cin>>s;
    santo.creat_vector(s);
    cout<<" enter "<<s<<" elements  one by one :"<<endl;
    for(int i=0;i<s;i++)
    {
        float v;
        cin>>v;
        santo.set_element(i,v);
    }
    cout<<" Now contents  :"<<endl;
    santo.display();
    cout<<" to multiply this vector by a scalar quantity enter this scalar quantity : ";
    float m;
    cin>>m;
    santo.multiply(m);
    cout<<" Now contents : "<<endl;
    santo.display();
    santo.modify();
    return 0;
}

OUTPUT

enter size of vector : 5

enter 5 elements one by one :

11     22     33     44     55

Now contents p[5] = ( 11  ,  22  ,  33  ,  44  ,  55)

to multiply this vector by a scalar quantity enter this scalar quantity : 2

Now contents :

p[5] = ( 22  ,  44  ,  66  ,  88  ,  110)

to edit a given element enter position of the element : 3

Now enter new value of 3th element : 100

Now new contents :

p[5] = ( 22  ,  44  ,  100  ,  88  ,  110)

to delete an element enter position of the element :2

New contents :

p[4] = ( 22  ,  100  ,  88  ,  110)

5.3

Modify the class and the program of Exercise 5.1 for handling 10 customers.

Answer
#include<iostream>
#include<iomanip>
#define size 10
using namespace std;
char *serial[size]={" FIRST "," SECOND "," THIRD "," 4th "," 5th "," 6th "," 7th "," 8th "," 9th ","10th"};
 
class bank
{
    char name[40];
    int ac_no;
    char ac_type[20];
    double balance;
public:
    int assign(void);
    void deposit(float b);
    void withdraw(float c);
    void displayon(void);
          void displayoff(void);
};
 
int bank::assign(void)
{
    float initial;
    cout<<" You have to pay   500 TK to open your account \n"
    <<" You have to store at least 500 TK to keep your account active\n"
    <<"Would you want to open a account????\n"
    <<" If Yes press 1 \n"
    <<" If No press 0 : ";
    int test;
    cin>>test;
    if(test==1)
    {
        initial=500;
        balance=initial;
cout<<" Enter name ,account number & account type to create account : \n";
         cin>>name>>ac_no>>ac_type;
    }
    else
    ;// do nothing
 
    return  test;
 
}
void bank::deposit(float b)
{
    balance+=b;
}
void bank::withdraw(float c)
{
    balance-=c;
    if(balance<500)
    {
    cout<<" Sorry your balance is not sufficient to withdraw "<<c<<"TK\n"
    <<" You have to store at least 500 TK to keep your account active\n";
                balance+=c;
    }
}
void bank::displayon(void)
{
    cout<<setw(12)<<name<<setw(17)<<ac_type<<setw(14)<<balance<<endl;
}
void bank::displayoff(void)
{        cout<<” Account has not created”<<endl;  }
int main()
{
    bank account[size];
          int  t[10];
    for(int i=0;i<size;i++)
    {
                    cout<<" Enter information for "<<serial[i]<<"customer : "<<endl;
        t[i]=account[i].assign();
                    if(t[i]==1)
                   {
            cout<<" Would you want to deposit: ?"<<endl
            <<"If NO press 0(zero)"<<endl
            <<"If YES enter deposit amount :"<<endl;
            float dp;
            cin>>dp;
            account[i].deposit(dp);
            cout<<" Would you want to with draw : ?"<<endl
            <<"If NO press 0(zero)"<<endl
            <<"If YES enter withdrawal amount :"<<endl;
            float wd;
            cin>>wd;
            account[i].withdraw(wd);
            cout<<endl<<endl;
                   }
                   else if(t[i]==0)
                   cout<<”Thank  you , see again  \n”;
 
    }
 
     cout<<" see details :"<<endl<<endl;
     cout<<setw(12)<<"Name"<<setw(20)<<"Account type"
                  <<setw(12)<<"Balance"<<endl;
 
    for(i=0;i<size;i++)
    {
                      if(t[i]==1)
           account[i].displayon();
                     else  if(t[i]==0)
                       account[i].displayoff();
    }
    return 0;
}

Note: Here we will show output only for Three customers. But when you run this program you can see output for 10 customer.

OUTPUT

Enter information for FIRST customer :
You have to pay 500 TR to open your account
You have to store at least 500 TR to keep your account active Would you want to open a account????
If Yes press 1
If No press 0 : 0
Thank you , see again
Enter information for SECOND customer :
You have to pay 500 TR to open your account
You have to store at least 500 TR to keep your account active Would you want to open a account????
If Yes press 1
If No press 0 : 1
Enter name ,account number & account type to create account :
Robin 11123 saving
Would you want to deposit: ?
If HO press 0(zero)
If YES enter deposit amount :
0
Would you want to with draw : ?
If HO press 0(zero)
If YES enter withdrawal amount :
0
Enter information for 3rd customer :
You have to pay 500 TK to open your account
You have to store at least 500 TK to keep your account active Would you want to open a account????
If Yes press 1
If No press 0 : 1
Enter name ,account number & account type to create account :
Billal 11123 fixed
Would you want to deposit: ?
If HO press 0(zero)
If YES enter deposit amount :
1000000
Would you want to with draw : ?
If HO press 0(zero)
If YES enter withdrawal amount :
100000

see details :

Name          Account type          Balance

Account has not created

Robin           saving                       500

Billal            fixed                         900500

5.4

Modify the class and the program of Exercise 5.12 such that the program would be able to add two vectors and display the resultant vector. (Note that we can pass objects as function arguments)

Answer
#include<iostream>
#include<iomanip>
#define size 8
using namespace std;
class vector
{
    float *p;
 
public:
    void creat_vector(void);
    void set_element(int i,float value);
    friend void add(vector v1,vector v2);
 
};
void vector::creat_vector(void)
{
    p=new float[size];
}
void vector::set_element(int i,float value)
{
    p[i]=value;
}
void add(vector v1,vector v2)
{
 
    float *sum;
    cout<<"sum["<<size<<"] = (";
    sum= new float[size];
 
    for(int i=0;i<size;i++)
    {
        sum[i]=v1.p[i]+v2.p[i];
        if(i==size-1)
             cout<<sum[i];
        else
            cout<<sum[i]<<" , ";
    }
    cout<<")"<<endl;
 
}
 
int main()
{
    vector x1,x2,x3;
    x1.creat_vector();
    x2.creat_vector();
    x3.creat_vector();
    cout<<" Enter "<<size<<" elements of FIRST vector : ";
    for(int i=0;i<size;i++)
    {
         float v;
         cin>>v;
         x1.set_element(i,v);
    }
 
    cout<<" Enter "<<size<<" elements of SECOND vector : ";
    for(i=0;i<size;i++)
    {
               float v;
         cin>>v;
         x2.set_element(i,v);
    }
    add(x1,x2);
 
    return 0;
}

OUTPUT

Enter 8 elements of FIRST vector :  4  7  8  2  4  3  2  9

Enter 8 elements of SECOND vector :  1  2  3  4  5  6  7  8

sum[8] = (5 , 9 , 11 , 6 , 9 , 9 , 9 , 17)

5.5

Create two classes DM and DB which store the value of distances. DM stores distances in meters and centimeters and DB in feet and inches. Write a program that can read values for the class objects and add one object of DM with another object of DB.
Use a friend function to carry out the addition operation. The object that stores the results may be a DM object or DB object, depending on the units in which the results are required.
The display should be in the format of feet and inches or meters and centimeters depending on the object on display.

Answer
#include<iostream>
#define factor 0.3048
using namespace std;
class DB;
class DM
{
                    float d;
         public:
         void store(float x){d=x;}
         friend void sum(DM,DB);
         void show();
};
class DB
{
        float d1;
        public:
        void store(float y){d1=y;}
        friend void sum(DM,DB);
        void show();
};
 
void DM::show()
{
 
      cout<<"\n Distance = "<<d<<" meter or "<<d*100<<" centimeter\n";
}
 
void DB::show()
{
 
      cout<<"\n Distance = "<<d1<<" feet or "<<d1*12<<" inches \n";
}
void sum(DM m,DB b)
{
 
         float sum;
 
         sum=m.d+b.d1*factor;
         float f;
         f=sum/factor;
         DM m1;
         DB b1;
 
         m1.store(sum);
         b1.store(f);
 
       cout<<" press 1 to display result in meter\n"
          <<" press 2 to display result in feet \n"
          <<" What is your option ? : ";
          int test;
          cin>>test;
 
             if(test==1)
             m1.show();
             else if(test==2)
             b1.show();
 
 
}
 
 
int main()
{
     DM dm;
     DB db;
     dm.store(10.5);
     db.store(12.3);
    sum(dm,db);
     return 0;
}

OUTPUT

Press 1 to display result in meter
Press 2 to display result in feet
What is your option ? 1
Distance = 14.24904 meter or 1424.903931 centimeter


Next Previous