balaguruswamy c++ unit 4

http://www.htlwebs.com
e- balaguruswamy c++ solution
PROGRAMMING EXERCISE – 4
1. Define a class to represent a bank account. Include the following members:
a. Name of the depositor
b. Account number
c. Type of account
d. Balance amount in the account
Member functions
a. To assign initial values
b. To deposit an amount
c. To withdraw an amount after checking the balance
d. To display name and balance
#include<iostream.h>
#include<conio.h>
#include<string.h>
class bank
{
public:
char n[20];
double an;
char tp;
float amt;
int ass()
{
cout<<"Enter the following:\n";
cout<<"1.Name of the depositor\n";
cout<<"2.Account number\n";
cout<<"3.Type of account\n";
cout<<"4.Balance amt\n";
cin>>n>>an>>tp>>amt;
return 0;
}
int dep()
{
float x;
cout<<"Enter amt to deposit\n";
cin>>x;
amt+=x;
return 0;
}
int with()
{
float x;
a:
cout<<"Enter the amt to withdraw\n";
cin>>x;
if(x>amt)
{
cout<<"Can not withdraw\n";
goto a;
}
amt-=x;
return 0;
}
int dis()
http://www.htlwebs.com
e- balaguruswamy c++ solution
{
cout<<"1.Name of the depositor "<<n<<endl;
cout<<"2.Account number "<<an<<endl;
cout<<"3.Type of account "<<tp<<endl;
cout<<"4.Balance amt "<<amt<<endl;
return 0;
}
};
int main()
{
bank o; b:
cout<<"\n1.To assign initial values\n";
cout<<"2.To deposit an amount\n";
cout<<"3.To withdraw an amount\n";
cout<<"4.To display name & balance\n";
int x;
cin>>x;
if(x==1) o.ass();
else if(x==2) o.dep();
else if(x==3) o.with();
else if(x==4) o.dis();
else cout<<"Wrong! \n";
goto b;
}
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, …)
#include<iostream.h>
#include<conio.h>
class vector
{
float a[20];int n,i,j,u;
public:
create(void);
mod(void);
mult(void);
dis1(void);
dis2(void);
};
vector::create()
{
cout<<"No of elements in vector\n";
cin>>n;
cout<<"Enter the elements\n";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
}
vector::mod()
{int u;
http://www.htlwebs.com
e- balaguruswamy c++ solution
cout<<"Enter the no. of element\n";
cin>>u;
--u;
cout<<"Enter the modified value\n";
cin>>a[u];
}
vector::mult()
{
cout<<"Enter the value of scalar\n";
int v;
cin>>v;
for(int i=0;i<n;i++)
{
a[i]*=v;
}
}
vector::dis1()
{
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
}
vector::dis2()
{
for(int i=0;i<n;i++)
{
cout<<int(a[i])<<" ";
}
}
int main()
{
vector o;
while(1){
cout<<"\n1.To create vector\n";
cout<<"2.To modify\n";
cout<<"3.To multiply\n";
cout<<"4.To display simply\n";
cout<<"5.To display as(10,20,30,...)\n";
int x;
cin>>x;
if(x==1) o.create();
else if(x==2) o.mod();
else if(x==3) o.mult();
else if(x==4) o.dis1();
else if(x==5) o.dis2();
else cout<<"Wrong! value\n";
}
return 0;
}
3. Modify the class and the program 1 for handling 10 customers.
#include<iostream.h>
#include<conio.h>
#include<string.h>
http://www.htlwebs.com
class bank
{
public:
char n[20];
double an;
char tp;
float amt;
int ass()
{
cout<<"Enter the following:\n";
cout<<"1.Name of the Customer\n";
cout<<"2.Account number\n";
cout<<"3.Type of account\n";
cout<<"4.Balance amt\n";
cin>>n>>an>>tp>>amt;
return 0;
}
int dep()
{
float x;
cout<<"Enter amt to deposit\n";
cin>>x;
amt+=x;
return 0;
}
int with()
{
float x;
a:
cout<<"Enter the amt to withdraw\n";
cin>>x;
if(x>amt)
{
cout<<"Can not withdraw\n";
goto a;
}
amt-=x;
return 0;
}
int dis()
{
cout<<"1.Name of the depositor :"<<n<<endl;
cout<<"2.Account number :"<<an<<endl;
cout<<"3.Type of account :"<<tp<<endl;
cout<<"4.Balance amt :Rs"<<amt<<endl<<endl;
return 0;
}
};
int main()
{
bank o[9];q:
cout<<"1.New User\n";
cout<<"2.Existing User\n";
int s,t=0;
cin>>s;
e- balaguruswamy c++ solution
http://www.htlwebs.com
e- balaguruswamy c++ solution
if(s==1)
{int i=0; u:
cout<<"\n1.To assign initial values\n";
cout<<"2.To deposit an amount\n";
cout<<"3.To withdraw an amount\n";
cout<<"4.To display name & balance\n";
cout<<"5.New user\n";
cout<<"6.To go back\n";
int x;
cin>>x;
if(x==1) o[i].ass();
else if(x==2) o[i].dep();
else if(x==3) o[i].with();
else if(x==4) o[i].dis();
else if(x==6) {clrscr();goto q;}
else if(x==5) i++;
else cout<<"Wrong! \n"; goto u;
}
if(s==2)
{
int ad;
cout<<"Enter the account no.\n";
cin>>ad;
for(int y=0;y<10;y++)
{
if(o[y].an==ad){t++; break;}
}
if(t==0){cout<<"No User found!\n";goto q;}
hg:
cout<<"1.To deposit an amount\n";
cout<<"2.To withdraw an amount\n";
cout<<"3.To display name & balance\n";
cout<<"4.Go back\n";
int x;
cin>>x;
if(x==1) o[y].dep();
else if(x==2) o[y].with();
else if(x==3) o[y].dis();
else if(x==4) goto q;
else cout<<"Wrong entry!\n";goto hg;
}
return 0;
}
4. Modify the class and program 2 such that the program would be able to add vectors and display the
resultant vector.
#include<iostream.h>
#include<conio.h>
class vector
{
float aa[20];
public:
int n;
add(vector&,vector&);
create(void);
http://www.htlwebs.com
e- balaguruswamy c++ solution
vector::create()
{
cout<<"No of elements in vector\n";
cin>>n;
cout<<"Enter the elements\n";
for(int i=0;i<n;i++)
{
cin>>aa[i];
}
}
vector::add(vector & a,vector &b)
{
if(a.n>b.n) ;else a.n=b.n;
for(int i=0;i<a.n;i++)
{
cout<<a.aa[i]+b.aa[i]<<" ";
}
}
int main()
{
vector o1,o2,c;
cout<<"Enter the vector 1\n";
o1.create();
cout<<"Enter the vector 2\n";
o2.create();
cout<<"Addition of 2 vectors\n";
c.add(o1,o2);
return 0;
}
5. Create two classes DM and DB which store the value of distances. DM stores distances in meters
and centimetres and DB in feet and inches. Write a program that can read values for the class objects
and add one of DM with another object of DB.
#include<iostream.h>
#include<conio.h>
#include<process.h>
class DB;
class DM
{
float f,i;
public:
void set(float x,float y)
{
f=x; i=y;
}
friend void add(DM,DB);
};
class DB
{
float m,cm;
public:
void set(float x,float y)
{
m=x; cm=y;
}
http://www.htlwebs.com
friend void add(DM,DB);
};
void add(DM a,DB b)
{
float s=(a.f+a.i/12)*0.3048;
float d=s+b.m+(b.cm)/100;
int h=(int)d;
cout<<h<<"mtrs "<<((d-h)*100)<<"cms "<<endl;
float v=(b.m+(b.cm/100))*3.2808;
float w=v+a.f+(a.i/12);
int g=(int)w;
float j=(w-g)*12;
cout<<g<<"feets "<<(j)<<"inches"<<endl;
}
int main()
{
DM q; DB r;
float x,y,a,b;char c;
do{
cout<<"Enter the value1 in metres & centimetres\n";
cin>>x>>a;
r.set(x,a);
cout<<"Enter the value2 in feets & inches\n";
cin>>y>>b;
q.set(y,b);
add(q,r);
cout<<"Enter again (y/n)\n";
c=getch();
}
while(c=='y');
return 0;
}
e- balaguruswamy c++ solution