PART B
Program 1:
/* Create a class bank which include data members account no, name and
balance. and parameterized constructor to initialized data members and
other methods like deposit, withdrawal and display the details of the
customer.(hint: minimum balance of rs.500 should be maintained and
amount should be positive) */
#include<iostream.h>
#include<conio.h>
#include<string.h>
class bank
{
private:
int accno;
char name[20];
float bal;
public:
int amt;
bank(int ano,char na[20],float balance);
void display ();
void deposit ();
void withdrawl ();
};
bank :: bank(int ano,char na[20],float balance)
{
accno=ano;
strcpy(name,na);
bal=balance;
}
void bank :: deposit()
{
cout<<"Enter amt to deposit"<<"\n";
cin>>amt;
bal=bal+amt;
}
void bank :: withdrawl ()
{
cout<<"Enter the amt to be withdrawal"<<"\n";
cin>>amt;
bal=bal-amt;
if(bal<500)
{
cout<<"Amt cannot be withdrawal"<<"\n";
bal=bal+amt;
}
}
void bank :: display()
{
cout<<"The acc number is"<<accno<<"\n";
cout<<"The name is"<<name<<"\n";
cout<<"The balance is"<<bal<<"\n";
}
int main()
{
int ch,ans=1;
bank b(111,"ARBAZ",5000);
clrscr();
while(ans)
{
cout<<"1.Deposite"<<"\n";
cout<<"2.Withdrawl"<<"\n";
cout<<"3.Display"<<"\n";
cout<<"Enter the choice";
cin>>ch;
switch(ch)
{
case 1:b.deposit();
break;
case 2:b.withdrawl();
break;
case 3:b.display();
break;
default: cout<<"Invalid choice";
}
cout<<"Do you want to continue(if yes then type 1 else 0)";
cin>>ans;
}
getch();
return 0;
}
Output:
1.Deposite
2.Withdrawl
3.Display
Enter the choice1
Enter amt to deposit
500
Do you want to continue(if yes then type 1 else 0)1
1.Deposite
2.Withdrawl
3.Display
Enter the choice2
Enter the amt to be withdrawal
2500
Do you want to continue(if yes then type 1 else 0)1
1.Deposite
2.Withdrawl
3.Display
Enter the choice3
The acc number is111
The name isARBAZ
The balance is3000
Do you want to continue(if yes then type 1 else 0)1
1.Deposite
2.Withdrawl
3.Display
Enter the choice2
Enter the amt to be withdrawal
2600
Amt cannot be withdrawed
Do you want to continue(if yes then type 1 else 0)1
1.Deposite
2.Withdrawl
3.Display
Enter the choice3
The acc number is111
The name isARBAZ
The balance is3000
Do you want to continue(if yes then type 1 else 0)1
1.Deposite
2.Withdrawl
3.Display
Enter the choice2
Enter the amt to be withdrawal
2000
Do you want to continue(if yes then type 1 else 0)1
1.Deposite
2.Withdrawl
3.Display
Enter the choice3
The acc number is111
The name isARBAZ
The balance is1000
Do you want to continue(if yes then type 1 else 0)0
Program2:
/*Using constructors and proper methods design a class graphics which
stores shapes, area, back color and fore color. Use this class in the main
program to input any ‘n’ shapes and perform the following options and
print the list in the neat format.
a. Sort according to area.
b. Search according to accepted shape. */
#include<iostream.h>
#include<conio.h>
#include<string.h>
class graphics
{
private:
char bcolor[20];
char fcolor[20];
public:
char shape[20];
float area;
void read();
void show();
graphics();
};
graphics :: graphics()
{
strcpy(shape," ");
area=0;
}
void graphics :: read()
{
cout<<"Enter the shape"<<"\n";
cin>>shape;
cout<<"Enter the area"<<"\n";
cin>>area;
cout<<"Enter the bcolor"<<"\n";
cin>>bcolor;
cout<<"Enter the fcolor"<<"\n";
cin>>fcolor;
}
void graphics :: show()
{
cout<<"The shape is "<<shape<<"\n";
cout<<"The area is "<<area<<"\n";
cout<<"The bcolor is "<<bcolor<<"\n";
cout<<"The fcolor is "<<fcolor<<"\n";
}
int main()
{
graphics g[50],temp;
int n,i,j,flag=0;
char s[20];
clrscr();
cout<<"Enter the size of the array"<<"\n";
cin>>n;
for(i=0;i<n;i++)
{
g[i].read();
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(g[i].area<g[j].area)
{
temp=g[i];
g[i]=g[j];
g[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
g[i].show();
}
cout<<"Enter the shape to be search"<<"\n";
cin>>s;
for(i=0;i<n;i++)
{
if(strcmp(g[i].shape,s)==0)
{
flag=1;
g[i].show();
}
}
if(flag==0)
cout<<"The enterd shape is not found"<<"\n";
getch();
return 0;
}
Output 1:
Enter the size of the array
2
Enter the shape
circle
Enter the area
3
Enter the bcolor
red
Enter the fcolor
blue
Enter the shape
triangle
Enter the area
8
Enter the bcolor
pink
Enter the fcolor
green
The shape is circle
The area is 3
The bcolor is red
The fcolor is blue
The shape is triangle
The area is 8
The bcolor is pink
The fcolor is green
Enter the shape to be search
triangle
The shape is triangle
The area is 8
The bcolor is pink
The fcolor is green
Output 2:
Enter the size of the array
1
Enter the shape
circle
Enter the area
4
Enter the bcolor
yellow
Enter the fcolor
black
The shape is circle
The area is 4
The bcolor is yellow
The fcolor is black
Enter the shape to be search
rectangle
The enterd shape is not found
Program: 3
/* Write a program to accept 2 strings using operator overloading.
Perform the following
a) Concatenation of 2 strings.
b) Comparison of 2 strings alphabetically.
[ Hint: for concatenation +,for comparison ==,>,< ] */
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char str[50];
public:
void read();
void display();
string();
string operator +(string st2);
void operator ==(string st2);
void operator >(string st2);
void operator <(string st2);
};
string :: string()
{
strcpy(str," ");
}
void string :: read()
{
cout<<"Enter the string";
cin>>str;
}
void string :: display()
{
cout<<"The string is"<<str<<"\n";
}
string string :: operator +(string s2)
{
string s3;
strcpy(s3.str,str);
strcat(s3.str,s2.str);
return s3;
}
void string :: operator ==(string s2)
{
if((strcmp(str,s2.str)==0))
cout<<"String are equal";
}
void string :: operator >(string s2)
{
if((strcmp(str,s2.str)>0))
cout<<"First string is greater";
}
void string :: operator <(string s2)
{
if((strcmp(str,s2.str)<0))
cout<<"First string is smaller";
}
int main()
{
string st1,st2,st3;
clrscr();
st1.read();
st2.read();
st3=st1+st2;
st3.display();
st1==st2;
st1>st2;
st1<st2;
getch();
return 0;
}
OUTPUT 1:
Enter the string abc
Enter the string abc
The string is abcabc
String are equal
OUTPUT 2:
Enter the string abc
Enter the string def
The string is abcdef
First string is smaller
Program: 4
/* Using operator overloading concepts do the following
a) To add 2 complex numbers.
b) To subtract 2 complex numbers.
c) To compare 3 complex numbers.
[ Hint: to add +, to subtract -, to compare == must be overloaded and
compare the real with the real and imaginary with the imaginary part.]
*/
#include<iostream.h>
#include<conio.h>
class complex
{
private:
int real;
int img;
public:
void read();
void display();
complex operator +(complex b);
complex operator -(complex b);
int operator ==(complex b);
};
void complex :: read()
{
cout<<"Enter the real part"<<"\n";
cin>>real;
cout<<"Enter the imaginary part"<<"\n";
cin>>img;
}
void complex :: display()
{
if(img<0)
cout<<real<<img<<"i"<<"\n";
else
cout<<real<<"+"<<img<<"i"<<"\n";
}
complex complex :: operator +(complex b)
{
complex c;
c.real=real+b.real;
c.img=img+b.img;
return(c);
}
complex complex :: operator -(complex b)
{
complex c;
c.real=real-b.real;
c.img=img-b.img;
return(c);
}
int complex :: operator ==(complex b)
{
int eq;
if((real==b.real)&&(img==b.img))
eq=1;
else
eq=0;
return(eq);
}
int main()
{
complex n1,n2,n3;
int ch,ans=1,comp;
clrscr();
n1.read();
n1.display();
n2.read();
n2.display();
while(ans)
{
cout<<"1.Addition"<<"\n";
cout<<"2.Substraction"<<"\n";
cout<<"3.compairison"<<"\n";
cout<<"Enter your choice";
cin>>ch;
switch(ch)
{
case 1: n3=n1+n2;
cout<<"The result is";
n3.display();
break;
case 2: n3=n1-n2;
cout<<"the result is";
n3.display();
break;
case 3:comp=n1==n2;
if(comp==1)
cout<<"The number is equal"<<"\n";
else
cout<<"The number is not equal"<<"\n";
break;
default:
cout<<"Invalid key";
break;
}
cout<<"Do you want to continue(if you want to continue press 1 else 0)"<<"\n";
cin>>ans;
}
getch();
return 0;
}
OUTPUT:
Enter the real part
53
Enter the imaginary part
5+3i
Enter the real part
34
Enter the imaginary part
3+4i
1.Addition
2.Substraction
3.compairison
Enter your choice 1
The result is8+7i
Do you want to continue(if you want to continue press 1 else 0)
1
1.Addition
2.Substraction
3.compairison
Enter your choice2
the result is2-1i
Do you want to continue(if you want to continue press 1 else 0)
1
1.Addition
2.Substraction
3.compairison
Enter your choice3
The number is not equal
Do you want to continue(if you want to continue press 1 else 0)
0
Program: 5
/* Create a class called time which includes the data members hours,
minutes and seconds. Use the method
a) To accept time
b) To display time
c) To increment time by 1 second by overloading unary operator ++
d) To decrement time by 1 second by overloading unary operator –
Write a menudrevent for the above operation.
[ Hint: minutes and seconds must be always within the range 0 to 59.] */
#include<iostream.h>
#include<conio.h>
class time
{
int hrs;
int min;
int sec;
public:
void read();
void display();
void operator ++();
void operator --();
};
void time :: read()
{
cout<<"Enter Hours Minutes Secound"<<"\n";
cin>>hrs>>min>>sec;
}
void time :: display()
{
cout<<hrs<<"Hours"<<"\t"<<min<<"Minutes"<<"\t"<<sec<<"secound"<<"\n";
}
void time :: operator ++()
{
sec=sec+1;
if(sec>60)
{
sec=sec-60;
min++;
}
if(min>60)
{
min=min-60;
hrs++;
}
}
void time::operator --()
{
sec=sec-1;
if(sec<0)
{
sec=sec+60;
min--;
}
if(min<0)
{
min=min+60;
hrs--;
}
}
int main()
{
time t;
int ch,ans=1;
clrscr();
while(ans)
{
cout<<"1.Accept time"<<"\n";
cout<<"2.Display time"<<"\n";
cout<<"3.Increment time"<<"\n";
cout<<"4.dicrement time"<<"\n";
cout<<"Enter the choice"<<"\n";
cin>>ch;
switch(ch)
{
case 1:t.read();
break;
case 2:t.display();
break;
case 3:++t;
break;
case 4:--t;
break;
default:
cout<<"Invalid key";
break;
}
cout<<"Do you want to continue(if yes type1 else 0)";
cin>>ans;
if(ans==0)
break;
}
getch();
return 0;
}
Output :
1.Accept time
2.Display time
3.Increment time
4.dicrement time
Enter the choice
1
Enter Hours Minutes Secound
12 16 36
Do you want to continue(if yes type1 else 0)1
1.Accept time
2.Display time
3.Increment time
4.dicrement time
Enter the choice
2
12Hours 16Minutes 36secound
Do you want to continue(if yes type1 else 0)1
1.Accept time
2.Display time
3.Increment time
4.dicrement time
Enter the choice
3
Do you want to continue(if yes type1 else 0)1
1.Accept time
2.Display time
3.Increment time
4.dicrement time
Enter the choice
2
12Hours 16Minutes 37secound
Do you want to continue(if yes type1 else 0)1
1.Accept time
2.Display time
3.Increment time
4.dicrement time
Enter the choice
4
Do you want to continue(if yes type1 else 0)1
1.Accept time
2.Display time
3.Increment time
4.dicrement time
Enter the choice
2
12Hours 16Minutes 36secound
Do you want to continue(if yes type1 else 0)0
Program 6:
/*Create a class rectangle with length breadth and area create another
class cuboid that inherits rectangle and has additional member height and
volume use single inheritance properly. */
#include<iostream.h>
#include<conio.h>
class rectangle
{
private:
int length;
int bradth;
int area;
public:
void read();
void show();
};
void rectangle :: read()
{
cout<<"Enter the length "<<"\n";
cin>>length;
cout<<"Enter the breadth "<<"\n";
cin>>bradth;
area=length*bradth;
}
void rectangle :: show()
{
cout<<"The length is "<<length<<"\n";
cout<<"The breadth is "<<bradth<<"\n";
cout<<"The area is "<<area<<"\n";
}
class cubiod : public rectangle
{
private:
float volume;
int hight;
public:
void getdata();
void putdata();
};
void cubiod :: getdata()
{
rectangle :: read();
cout<<"Enter height";
cin>>hight;
volume=hight*hight*hight;
}
void cubiod :: putdata()
{
rectangle :: show();
cout<<"The height is "<<hight<<"\n";
cout<<"The volume is "<<volume<<"\n";
}
int main()
{
cubiod c;
clrscr();
c.getdata();
c.putdata();
getch();
return 0;
}
Output:
Enter the length
6
Enter the breadth
4
Enter hight3
The length is 6
The breadth is 4
The area is 24
The height is 3
The volume is 27