Nothing Special   »   [go: up one dir, main page]

Oops Lab Program

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 74

Ex.No:1.

A GENERATING THE ELECTRICITY BILL USING STRUCTURE


PROGRAM:
#include<iostream.h>
#include<conio.h>
struct ele
{
char name[30];
int unit;
float tot;
}a[10];
int main()
{
clrscr();
int i,n;
cout<<"\n\n\tHow many records :\n";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\n\n\tEnter record of consumer "<<i+1;
cout<<"\n\n\tName :\n";
gets(a[i].name);
cout<<"\n\n\tTotal no of unit consumed :\n";
cin>>a[i].unit;
a[i].tot=0;
if(a[i].unit>0&&a[i].unit<=100)
a[i].tot+=1.5*a[i].unit;
else if(a[i].unit>100&&a[i].unit<=200)
a[i].tot+=150+1.8*(a[i].unit-100);
else if(a[i].unit>200)
a[i].tot+=150+180+2.5*(a[i].unit-200);
if(a[i].tot>300)
a[i].tot+=0.15*a[i].tot;
if((a[i].tot)<=50)
a[i].tot=50;
}
cout<<"\n\n______________________________________________________\n"
;
cout<<"\n\t\tELECTRICITY BILL\n";
cout<<"\n________________________________________________________\n
";
cout<<"\nS.No\tNAME\t\t\tUNIT\tBILL AMOUNT\n";
cout<<"\n_________________________________________________________\
n";
for(i=0;i<n;i++)
1

cout<<i+1<<"\t"<<a[i].name<<"\t\t\t"<<a[i].unit<<"\tRs
"<<a[i].tot<<"\n";
cout<<"\n_________________________________________________________\
n";
getch();
return 0;
}
INPUT
How many records: 2
Enter record of consumer 1
Name : karthi
Total no of unit consumed: 500
Enter record of consumer 2
Name :arun
Total no of unit consumed: 300
OUTPUT
_______________________________________________
ELECTRICITY BILL
________________________________________________________
S.No NAME
UNIT
BILL AMOUNT
________________________________________________________
1
karthi
500
Rs 1242
2
arun
300
Rs 667
________________________________________________________

EX.NO:1.B
Displaying alphabets
PROGRAM:
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int n,i,j;
char ch='A';
cout<<"\n\nEnter the number of lines :\n";
cin>>n;
for(i=0;i<n;i++)
{
int c=0;
cout<<"\n";
for(j=2*(n-i);j>0;j-=2)
{
while(c<=i)
{
cout<<'\0';
c++;
}
cout<<ch++<<'\0';
}
ch='A';
}
getch();
return 0;
}
Output:
Enter the number of lines:5
ABCDE
AB C D
AB C
AB
A

Ex.No:1.C
PRINT THE GIVEN FORMAT
PROGRAM:
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int n,i,j;
char ch='A';
cout<<"\n\nEnter the number of lines :\n";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\n";
for(j=0;j<=i;j++)
{
cout<<ch++;
}
ch='A';
}
getch();
return 0;
}
INPUT
Enter the number of lines: 4
OUTPUT
A
AB
ABC
ABCD

EX.No:1d
MULTIPLICATION TABLE
PROGRAM:
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int n;
cout<<"\n\n\tEnter The number :";
cin>>n;
cout<<"\n\n\tTABLE OF "<<n<<" IS GIVEN BELOW :\n";
for(int i=1;i<=10;i++)
cout<<"\n\t"<<n<<"x"<<i<<"="<<n*i;
getch();
return 0;
}
INPUT
Enter The number :4
OUTPUT
TABLE OF 4 IS GIVEN BELOW:
4x1=4
4x2=8
4x3=12
4x4=16
4x5=20
4x6=24
4x7=28
4x8=32
4x9=36
4x10=40

EX.NO:1.e
PRIME NUMBER
PROGRAM:
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int sn,ln;
cout<<"\n\n\tEnter the starting number :";
cin>>sn;
cout<<"\n\n\tEnter the last number :";
cin>>ln;
cout<<"\n\n\tPrime number from "<<'\0'<<sn<<'\0'<<"to"<<'\0'<<ln<<'\0'<<"is
given below :";
for(int i=sn;i<=ln;i++)
{
int c=0;
for(int j=2;j<=i/2;j++)
{
if(i%j==0)
c++;
}
if(c==0)
cout<<"\n\t"<<i;
}
getch();
return 0;
}
Output :
Enter the starting number :
12
Enter the last number :23
Prime number from 13
17
19
23

Ex.No:1f
6

CHECK WHETHER THE GIVEN MATRIX IS AN IDENTITY MATRIX OR NOT


PROGRAM:
#include<iostream.h>
#include<conio.h>
int main()
{
int a[4][4],j,c=0,d=0;
cout<<"\n\n\tEnter the 3x3 matrix \n\t";
for(int i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>a[i][j];
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]==0)
c=1;
if((i==j)&&(a[i][j]==1))
d=1;
}
}
if(c==1&&d==1)
cout<<"\n\tGiven matrix is identity matrix ";
else
cout<<"\n\tGiven matrix is not identity matrix ";
getch();
return 0;
}
Input and Output
1) Enter the 3x3 matrix
234
679
124
Given matrix is not identity matrix
2) Enter the 3x3 matrix
100
010
001
Given matrix is identity matrix
EX.NO:1.g Transpose of a given matrix
PROGRAM:
7

#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a[4][4],j;
cout<<"\n\n\tEnter the 3x3 matrix :\n\t";
for(int i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>a[i][j];
}
cout<<"transpose of matrix is:";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[j][i]<<"\t";
}
cout<<endl;
}
return 0;
}
Output :
Enter the 3x3 matrix :1
23456789
transpose of matrix is:
1 4 7
2 5 8
3 6 9

Ex.No: 2.aAREA OF RECTANGLE USING FUNCTION


PROGRAM:
8

#include <iostream.h>
#include<conio.h>
float area (float, float);
void main( )
{
clrscr();
cout << "Area of a rectangle = " << area(1.5, 3.6);
getch();
}
float area(float length, float width)
{
return(length * width);
}
OUTPUT
Area of a rectangle = 5.4

EX.NO:2.b 3*3 MATRIX


PROGRAM:
#include<iostream.h>
9

#include<conio.h>
int main()
{
clrscr();
int a[4][4],j;
cout<<"\n\n\tEnter the 3x3 matrix :\n\t";
for(int i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>a[i][j];
}
cout<<"given matrix is:";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
return 0;
}
Output :
Enter the 3x3 matrix :
123456789
given matrix is:
1 2 3
4 5 6
7 8 9

EX.NO:2.c Swapping using call by value,call by reference,call by pointer


PROGRAM:
#include<iostream.h>
#include<conio.h>
10

void swap(int a,int b)


{
int temp;
temp=a;
a=b;
b=temp;
}
void swapref(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
void swapptr(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int a1,b1,a2,b2,a3,b3;
clrscr();
cout<<"Enter the a1 and b1 value:"<<endl;
cin>>a1>>b1;
swap(a1,b1);
cout<<"Swapped elements :"<<a1<<" "<<b1<<endl;
cout<<"Enter the a2 and b2 value:"<<endl;
cin>>a2>>b2;
swapref(a2,b2);
cout<<"Swapped elements :"<<a2<<" "<<b2<<endl;
cout<<"Enter the a3 and b3 value:"<<endl;
cin>>a3>>b3;
swapptr(&a3,&b3);
cout<<"Swapped elements :"<<a3<<" "<<b3<<endl;
getch();
return 0;
}
Output :
Enter the a1 and b1 value:
1
2
11

Swapped elements :1 2
Enter the a2 and b2 value:
3
4
Swapped elements :4 3
Enter the a3 and b3 value:
5
6
Swapped elements :6 5

EX.NO:2.d Volume of cube using inline function


PROGRAM:
12

#include<iostream.h>
#include<conio.h>
inline volume (int a)
{
return a*a*a;
}
int main()
{
int a,x;
clrscr();
cout<<"Enter the value:"<<endl;
cin>>a;
x=volume(a);
cout<<"Volume of the cube="<<x;
getch();
return 0;
}
Output:
Enter the value:
5
Volume of the cube=125

EX.NO:2.e
MAXIMUM OF 3 NUMBERS USING INLINE FUNCTION
PROGRAM:
13

#include<iostream.h>
#include<conio.h>
inline int max(int a,int b,int c)
{
int max= (a>b?a:b)?(a>c?a:c):(b>c?b:c);
return max;
}
int main()
{
clrscr();
int a,b,c,max1;
cout<<"\n\n\tEnter the three numbers :";
cin>>a>>b>>c;
max1=max(a,b,c);
cout<<"\n\n\n\tMaximum number ="<<max1;
getch();
return 0;
}
Output :
Enter the three numbers :4
5
6
Maximum number =6

EX.NO:2.f Simple interest using default argument


PROGRAM:
#include<iostream.h>
#include<conio.h>
int si(int p,int n=1,float r=0.12)
14

{
return p*n*r;
}
int main()
{
int simint,a;
clrscr();
cout<<"Enter the Amount:"<<endl;
cin>>a;
simint=si(a);
cout<<"Simple interest="<<simint;
getch();
return 0;
}
Output :
Enter the Amount:
100
Simple interest=11

EX.NO:2.g
N power
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<math.h>
void power(int m,int n=2)
15

{
int r1;
r1=pow(m,n);
cout<<"the power of m,n is:"<<r1;
}
int main()
{
int n;
double m;
clrscr();
cout<<"\n enter the value of m&n:";
cin>>m>>n;
power(m,n);
getch();
return 0;
}
Output:
Enter the value of m n:2 3
The power of m , n is:8

EX.NO:3.a
Employee database
PROGRAM:
#include<iostream.h>
#include<conio.h>
class employee
{
16

char fname[20],lname[20];
double ms,ys;
public:
employee()
{
fname[20]='\0';
lname[20]='\0';
ms=0;
}
void get()
{
cout<<"Enter the first name:";
cin>>fname;
cout<<"Enter the last name:";
cin>>lname;
cout<<"Enter the monthly salary:";
cin>>ms;
}
void raise_salary()
{
ms=ms+(ms*0.1);
ys=ms*12;
}
void display()
{
cout<<"Name="<<fname<<lname<<endl;
cout<<"Monthly Salary="<<ms<<endl;
cout<<"Yeraly Income="<<ys<<endl;
}
};
int main()
{
employee em1,em2;
clrscr();
em1.get();
em2.get();
em1.raise_salary();
em2.raise_salary();
cout<<"Details after raising salary:"<<endl;
em1.display();
em2.display();
getch();
return 0;
}
Output :
17

Enter the first name:FG


Enter the last name:RE
Enter the monthly salary:45
Enter the first name:SD
Enter the last name:EW
Enter the monthly salary:34
Details after raising salary:
Name=FGRE
Monthly Salary=49.5
Yeraly Income=594
Name=SDEW
Monthly Salary=37.4
Yeraly Income=448.8

EX.NO:3.b Bank Database


PROGRAM:
#include<iostream.h>
#include<conio.h>
class bank
{
char name[20];
18

int accno,balamt,amt;
public:
void getdetails();
void deposit();
void withdraw();
void display();
};
void bank::getdetails()
{
cout<<"Enter the name ,accno,totalamt"<<endl;
cin>>name>>accno>>amt;
}
void bank::deposit()
{
int da;
cout<<"Enter the amount to be deposited:"<<endl;
cin>>da;
balamt=amt+da;
cout<<"Balance amount="<<balamt;
}
void bank::withdraw()
{
int wa;
cout<<"Enter the amount to withdraw:"<<endl;
cin>>wa;
balamt=amt-wa;
cout<<"Balance amount="<<balamt;
}
void bank::display()
{
cout<<endl<<"Name \t Balance amount"<<endl;
cout<<name<<"
"<<balamt;
}
int main()
{
bank b;
int ch;
clrscr();
b.getdetails();
cout<<"Enter the choice:";
cout<<"1.Deposit\t2.Withdraw\t3.Exit"<<endl;
cin>>ch;
switch(ch)
{
case 1:
b.deposit();
19

b.display();
break;
case 2:
b.withdraw();
b.display();
break;
case 3:
break;
}
getch();
return 0;
}
Output:
Enter the choice:
1.Deposit 2.Withdraw 3.Exit
Enter the name ,accno,totalamt : gk 777 5000
Enter the amount to be deposited : 5000
Balance amount=10000

EX.NO:3.c Complex number using member function


PROGRAM:
#include<iostream.h>
#include<conio.h>
class complex
{
int r1,i1,r2,i2;
int c1,c2;
20

public:
void get();
void add();
void mul();
};
void complex::get()
{
cout<<"Enter the first complex no:"<<endl;
cin>>r1>>i1;
cout<<"Enter the second complex no:"<<endl;
cin>>r2>>i2;
}
void complex::add()
{
c1=r1+r2;
c2=i1+i2;
cout<<"The addition of two complex no:";
cout<<c1<<"+i"<<c2;
}
void complex::mul()
{
c1=((r1*r2)-(i1*i2));
c2=((i1*r2)+(r1*i2));
cout<<"The multiplication of two complex no:";
cout<<c1<<"+i"<<c2;
}
int main()
{
complex c;
int ch;
clrscr();
c.get();
cout<<"1.Add\t2.Mul\t3.exit"<<endl;
cout<<"Enter the choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:
c.add();
break;
case 2:
c.mul();
break;
case 3:
break;
}
21

getch();
return 0;
}
Output:
1.add 2.mul
3.exit
Enter the choice 1
Enter the first complex number:5 3
Enter the second complex number: 2 1
Addition:7+4i

EX.NO:3.d
Stack data structure
PROGRAM:
#include<iostream.h>
#include<conio.h>
class stack
{
int data[10];
int i,top;
public:
22

stack()
{
top=-1;
}
void push(int d)
{
if(top==10)
cout<<"stack is full\n";
else
{
top++;
data[top]=d;
}
}
void pop()
{
if(top==0)
cout<<"Stack is empty\n";
else
{
cout<<"The popped data:"<<data[top]<<"\n";
top--;
}
}
void display()
{
int i;
cout<<"STACK DATA\n";
for(i=top;i>=0;i--)
{
cout<<data[i]<<"\n";
}
cout<<"\n";
}
};
void main()
{
stack s;
int ch,d;
clrscr();
do
{
cout<<"1.Push\n2.Pop\n3.Display\n";
cout<<"Enter the choice:";
cin>>ch;
23

switch(ch)
{
case 1:
{
cout<<"Enter the data to be pushed:";
cin>>d;
s.push(d);
break;
}
case 2:
{
s.pop();
break;
}
case 3:
{
s.display();
break;
}
}
}while(ch<=3);
getch();
}
Output:
1.push
2.pop
3.display
Enter the choice:1
Enter the data to be pushed:2
Enter the choice:1
Enter the data to be pushed:3
Enter the choice:2
Poped element=3
Enter the choice:3
Stack content[2]
EX.NO:3.e Addition and multiplication of vector
PROGRAM:
#include<iostream.h>
#include<conio.h>
class Vector
{
int size;
24

float *a;
public:
Vector()
{
}
Vector(int s);
void getdata();
void multiply(int s);
void add(Vector ,Vector );
void display();
};
Vector::Vector(int s)
{
size=s;
a=new float[size];
}
void Vector::getdata()
{
int i;
cout<<"\n"<<"Enter the"<<size<<"of elements";
for(i=0;i<size;i++)
cin>>a[i];
}
void Vector::multiply(int s)
{
int i;
for(i=0;i<size;i++)
{
a[i]=a[i]*s;
}
}
void Vector::add(Vector v1,Vector v2)
{
int i;
a=new float[size];
for(i=0;i<size;i++)
{
a[i]=v1.a[i]+v2.a[i];
}
}
void Vector::display()
{
int i;
cout<<"("<<a[0];
for(i=1;i<size;i++)
25

{
cout<<","<<a[i];
}
cout<<")";
}
int main()
{
clrscr();
Vector v1,v2,v3;
int size,s;
cout<<"Enter the size";
cin>>size;
v1=Vector(size);
v1.getdata();
cout<<"\n"<<"Created vector";
v1.display();
v2=Vector(size);
v2.getdata();
cout<<"\n"<<"Created Vector";
v2.display();
cout<<"\n"<<"Enter the multiply number";
cin>>s;
v1.multiply(s);
cout<<"\n"<<"after multiply";
v1.display();
v2.multiply(s);
cout<<"\n"<<"after multiply";
v2.display();
v3=Vector(size);
v3.add(v1,v2);
cout<<"\n"<<"After addition";
v3.display();
getch();
return (0);
}
Output:
Enter the size:3
Enter the element:1
2
4
Created vector(1,2,4)
Enter the element:
2
7
26

8
Created vector(2,7,8)
Enter the multiply number:
After multiply(9,18,36)
After multiple(18,63,72)
After addition(27,81,108)

EX.NO:4.a Area of square and rectangle using function overloading


PROGRAM:
#include<iostream.h>
#include<conio.h>
int area(int a)
{
return a*a;
}
int area(int b,int h)
27

{
return b*h;
}
int main()
{
int a,b,h;
clrscr();
cout<<"Enter the side of square:"<<endl;
cin>>a;
cout<<"area of square:"<<area(a);
cout<<"enter the length &breath of rectangle:"<<endl;
cin>>b>>h;
area(b,h);
cout<<"area of rectangle:"<<area(b,h);
getch();
return 0;
}
Output :
Enter the value of x & y7
8
X=7
Y=8
X=17
Y=18

EX.NO:4.bVolume of cube,cone and rectangle


#include<iostream.h>
#include<conio.h>
int volume(int a)
{
return a*a*a;
}
int volume(float x,float pi,int r,int h)
28

{
return x*pi*r*r*h;
}
int volume(int l,int b,int he)
{
return l*b*he;
}
int main()
{
int a,r,h,l,b,he;
clrscr();
cout<<"Enter the value of a:"<<endl;
cin>>a;
cout<<"Volume of a cube:"<<volume(a)<<endl;
cout<<"Enter the r and h:"<<endl;
cin>>r>>h;
cout<<"Volume of a cone:"<<volume(0.33,3.14,r,h)<<endl;
cout<<"Enter the values of l,b,he:"<<endl;
cin>>l>>b>>he;
cout<<"Volume of the rectangle:"<<volume(l,b,he)<<endl;
getch();
return 0;
} /*Output :
Enter the value of a:4
Volume of a cube:64
Enter the r and h: 5 6
Volume of a cone:155
Enter the values of l,b,he: 1 2 3
Volume of the rectangle:6*/

EX.NO:4.cSwapping of integer,float,charactrev ,string using function overloading


PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<string.h>
void swap(int &x,int &y)
{
int t;
29

t=x;
x=y;
y=t;
cout<<"After swapping the integers:"<<x<<" "<<y<<endl;
}
void swap(float &x1,float &x2)
{
float t1;
t1=x1;
x1=x2;
x2=t1;
cout<<"After swapping the float pointer:"<<x1<<" "<<x2<<endl;
}
void swap(char &a,char &b)
{
char t2;
t2=a;
a=b;
b=t2;
cout<<"After swapping the characters:"<<a<<" "<<b<<endl;
}
int main()
{
int x,y;
float x1,x2;
char a,b;
clrscr();
cout<<"Enter the integer value:"<<endl;
cin>>x>>y;
swap(x,y);
cout<<"Enter the float value:"<<endl;
cin>>x1>>x2;
swap(x1,x2);
cout<<"Enter the characters:"<<endl;
cin>>a>>b;
swap(a,b);
getch();
return 0;
}
Output :
Enter the integer value:
1
2
After swapping the integers:2 1
Enter the float value:
30

1.2
2.3
After swapping the float pointer:2.3 1.2
Enter the characters:
A
S
After swapping the characters:S A

EX.NO:5.aAddition,subtraction of complex number using operator overloading


PROGRAM:
#include<iostream.h>
#include<conio.h>
class complex
{
int real,img;
public:
31

friend istream & operator>>(istream &inst,complex c);


friend ostream & operator<<(ostream &outst,const complex c);
friend complex operator +(complex,complex);
friend complex operator -(complex,complex);
friend complex operator *(complex,complex);
};
istream & operator>>(istream &inst ,complex c)
{
cout<<"Enter the real part:"<<endl;
inst>>c.real;
cout<<"Enter the img part:"<<endl;
inst>>c.img;
cout<<"\n\t"<<c.real<<"+i"<<c.img<<endl;
return inst;
}
ostream & operator<<(ostream &outst,const complex c)
{
outst<<"\n\t"<<c.real<<"+i"<<c.img<<endl;
return outst;
}
complex operator+(complex c1,complex c2)
{
complex c;
c.real=c1.real+c2.real;
c.img=c1.img+c2.img;
return c;
}
complex operator-(complex c1,complex c2)
{
complex c;
c.real=c1.real-c2.real;
c.img=c1.img-c2.img;
return c;
}
complex operator *(complex c1,complex c2)
{
complex c;
c.real=((c1.real*c2.real)+(c1.img*c2.img));
c.img=((c1.img*c2.real)-(c1.real*c2.img));
return c;
}
int main()
{
complex c1,c2,c3;
clrscr();
cout<<"\nEnter the 1st complex no:";
32

cin>>c1;
cout<<"\nEnter the 2nd complex no:";
cin>>c2;
c3=c1+c2;
cout<<endl<<"Addition"<<endl;
cout<<c3;
c3=c1-c2;
cout<<endl<<"Subtraction"<<endl;
cout<<c3;
c3=c1*c2;
cout<<endl<<"Multiplication"<<endl;
cout<<c3;
getch();
return 0;
}
Output :
Enter the 1st complex no:Enter the real part:
4
Enter the img part:
5
4+i5
Enter the 2nd complex no:Enter the real part:
6
Enter the img part:
7
6+i7
Addition
10+i12
Subtraction
-2+i-2
Multiplication
59+i54

EX.NO:5.b Addition of matrix using operator overloading


PROGRAM:
#include<iostream.h>
#include<conio.h>
class matrix
{
int row,col;
33

int a[10][10];
public:
friend istream & operator>>(istream &inst, matrix &m);
friend ostream & operator<<(ostream &outst, const matrix &m);
friend matrix operator +(matrix,matrix);
friend matrix operator -(matrix,matrix);
friend matrix operator *(matrix,matrix);
};
istream & operator>>(istream &inst, matrix &m)
{
cout<<"\n\n\tEnter no of rows and columns :";
inst>>m.row>>m.col;
cout<<"\n\n\tEnter the elements :";
for(int i=0;i<m.row;i++)
for(int j=0;j<m.col;j++)
inst>>m.a[i][j];
return inst;
}
ostream & operator<<(ostream &outst, const matrix &m)
{
outst<<"\n\tMatrix is :";
for(int i=0;i<m.row;i++)
{
outst<<endl;
for(int j=0;j<m.col;j++)
outst<<"\t\t"<<m.a[i][j];
}
return outst;
}
matrix operator +(matrix m1,matrix m2)
{
matrix tmp;
tmp.row=m1.row;
tmp.col=m1.col;
for(int i=0;i<m1.row;i++)
{
for(int j=0;j<m1.col;j++)
{
tmp.a[i][j]=m1.a[i][j]+m2.a[i][j];
}
}
return tmp;
}
matrix operator -(matrix m1,matrix m2)
{
matrix tmp;
34

tmp.row=m1.row;
tmp.col=m1.col;
for(int i=0;i<m1.row;i++)
for(int j=0;j<m1.col;j++)
tmp.a[i][j]=m1.a[i][j]-m2.a[i][j];
return tmp;
}
matrix operator *(matrix m1,matrix m2)
{
matrix tmp;
tmp.row=m1.row;
tmp.col=m1.col;
for(int i=0;i<m1.row;i++)
for(int j=0;j<m1.col;j++)
{
tmp.a[i][j]=0;
for(int k=0;k<m1.col;k++)
tmp.a[i][j]+=m1.a[i][k]*m2.a[k][j];
}
return tmp;
}
int main()
{
clrscr();
matrix m1,m2,m3;
//int x1,y1,x2,y2;
cout<<"\n\n\tEnter the matrix A :";
cin>>m1;
cout<<"\n\n\tEnter matrix B :";
cin>>m2;
cout<<"\n\n\tMatrix A is :";
cout<<m1;;
cout<<"\n\n\tMatrix B is :";
cout<<m2;
m3=m1+m2;
cout<<"\n\n\tAddition :";
cout<<m3;
m3=m1-m2;
cout<<"\n\n\tSubstraction :";
cout<<m3;
m3=m1*m2;
cout<<"\n\n\tMultiplication :";
cout<<m3;
getch();
return 0;
35

}
Output :
Enter the matrix A :
Enter no of rows and columns :2
2
Enter the elements :1
2
3
4
Enter matrix B :
Enter no of rows and columns :2
2
Enter the elements :1
2
3
4
Matrix A is :
Matrix is :
1
2
3
4
Matrix B is :
Matrix is :
1
2
3
4
Addition :
Matrix is :
2
4
6
8
Substraction :
Matrix is :
0
0
0
0
Multiplication :
Matrix is :
7
10
15
22
EX.NO:5.c Relational operator
PROGRAM:
#include<iostream.h>
#include<conio.h>
class Integer
{
int i;
public:
36

Integer(){ i=0;}
int operator<(Integer x);
int operator>(Integer x);
int operator>=(Integer x);
int operator<=(Integer x);
int operator==(Integer x);
int operator!=(Integer x);
friend istream &operator>>(istream &din,Integer &x);
friend ostream &operator<<(ostream &dout,Integer &x);
};
int Integer::operator<(Integer x)
{
return (i<x.i);
}
int Integer::operator>(Integer x)
{
return (i>x.i);
}
int Integer::operator<=(Integer x)
{
return (i<=x.i);
}
int Integer::operator>=(Integer x)
{
return (i>=x.i);
}
int Integer::operator==(Integer x)
{
return (i==x.i);
}
int Integer::operator!=(Integer x)
{
return (i!=x.i);
}
istream &operator>>(istream &din,Integer &x)
{
din>>x.i;
return din;
}
ostream &operator<<(ostream &dout,Integer &x)
{
dout<<x.i;
return dout;
}
int main()
{
37

clrscr();
Integer a,b;
cout<<"\n\n\tEnter two integers :";
cin>>a>>b;
if(a>b)
cout<<"\n\n\t"<<a<<"Is greater than"<<b;
else
cout<<"\n\n\t"<<b<<"is greater than"<<a;
if(a<b)
cout<<"\n\n\t"<<a<<"is Smaller than"<<b;
else
cout<<"\n\n\t"<<b<<"is smaller than"<<a;
if(a==b)
cout<<"\n\n\t"<<a<<" and "<<b<<"are equal";
if(a!=b)
cout<<"\n\n\t"<<a<<" is not equal to "<<b;
getch();
return 0;
}
Output :
Enter two integers :1 2
2is greater than1
1is Smaller than2
1 is not equal to 2

EX.NO:5.dPost as well as pre increment & decrement


PROGRAM:
#include<iostream.h>
#include<conio.h>
class intr
{
int x,y;
public :
38

friend istream &operator>>(istream &din,intr &r);


friend ostream &operator<<(ostream &dout,intr &r);
void operator++();
void operator--();
};
istream &operator>>(istream &din,intr &r)
{
cin>>r.x>>r.y;
return din;
}
ostream &operator<<(ostream &dout,intr &r)
{
cout<<r.x<<"\t"<<r.y;
return dout;
}
void intr::operator++()
{
++x;y++;
//return *this;
}
void intr::operator--()
{
--x;y--;
//return *this;
}
int main()
{
clrscr();
intr r1;
cout<<"\n\n\tEnter x and y :";
cin>>r1;
cout<<"\n\n\tPre increment on x :";
++r1;
cout<<r1;
cout<<"\n\n\tPre decrement on x :";
--r1;
cout<<r1;
cout<<"\n\n\tPost increment on y :";
++r1;
cout<<r1;
cout<<"\n\n\tPost decrement on y :";
--r1;
cout<<r1;
getch();
return 0;
}
39

Output :
Enter x and y :1
2
Pre increment on x :2 3
Pre decrement on x :1 2
Post increment on y :2 3
Post decrement on y :1 2

EX.NO:5.e Arithmetic operation


PROGRAM:
#include<iostream.h>
#include<conio.h>
class integer
{
int i;
public:
40

friend istream &operator>>(istream &din,integer &x);


friend ostream &operator<<(ostream &dout,integer &x);
void operator+=(int k);
void operator-=(int k);
void operator*=(int k);
void operator/=(int k);
};
istream &operator>>(istream &din,integer &x)
{
cin>>x.i;
return din;
}
ostream &operator<<(ostream &dout,integer &x)
{
cout<<x.i;
return dout;
}
void integer::operator+=(int k)
{
i=i+k;
}
void integer::operator-=(int k)
{
i=i-k;
}
void integer::operator*=(int k)
{
i=i*k;
}
void integer::operator/=(int k)
{
i=i/k;
}
int main()
{
clrscr();
integer i1,i2,i3,i4;
cout<<"\n\n\tEnter the four integers :";
cin>>i1>>i2>>i3>>i4;
cout<<"\n\n\t Performing += operation with 10 on "<<i1<<" result is :";
i1+=10;
cout<<i1;
cout<<"\n\n\t Performing -= operation with 10 on "<<i2<<" result is :";
i2-=10;
cout<<i2;
cout<<"\n\n\t Performing *= operation with 10 on "<<i3<<" result is :";
41

i3*=10;
cout<<i3;
cout<<"\n\n\t Performing /= operation with 10 on "<<i4<<" result is :";
i4/=10;
cout<<i4;
getch();
return 0;
}
Output :
Enter the four integers :1
2
3
4
Performing += operation with 10 on 1 result is :11
Performing -= operation with 10 on 2 result is :-8
Performing *= operation with 10 on 3 result is :30
Performing /= operation with 10 on 4 result is :0

EX.NO:5.f Unary minus opearator


PROGRAM:
#include<iostream.h>
#include<conio.h>
class num
{
int a,b,c,d;
public:
42

num(int i,int k,int m,int l)


{
a=i;
b=k;
c=m;
d=l;
}
void show()
{
cout<<"\nA="<<a<<"\nB="<<b<<"\nC="<<c<<"\nD="<<d;
}
void operator-()
{
a=-a;
b=-b;
c=-c;
d=-d;
}
};
void main()
{
clrscr();
num x(3,2,5,7);
x.show();
cout<<"\n Positive values of x";
-x;
cout<<"\n Negative values of x";
x.show();
getch();
}
Output:
Positive values:
A=3
B=2
C=5
D=7
Negative values:
A=-3
B=-2
C=-5
D=-7

43

EX.NO:6.a Program to store book and cassette record


PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class publish
{
protected :
char title[30];
44

float price;
public:
void getp()
{
cout<<"\n\n\tEnter the Title :";
gets(title);
cout<<"\n\n\tEnter the price :";
cin>>price;
}
void putp()
{
cout<<"\n\t"<<title<<"\t"<<price<<"\t";
}
};
class book:public publish
{
int no_page;
public:
void get()
{
cout<<"\n\n\tEnter number of pages :";
cin>>no_page;
}
void put()
{
cout<<no_page<<"\n";
}
};
class tape:public publish
{
int len;
public:
void get()
{
cout<<"\n\n\tEnter Length in minutes :";
cin>>len;
}
void put()
{
cout<<len<<"\n";
}
};
int main()
{
int n1,n2;
clrscr();
45

book b[10];
tape t[10];
cout<<"\n\n\tHow many book :";
cin>>n1;
cout<<"\n\n\tEnter the book data :";
for(int i=0;i<n1;i++)
{
cout<<"\n\n\tInformation of book "<<i+1;
b[i].getp();
b[i].get();
}
cout<<"\n\n\tHow many cassette :";
cin>>n2;
cout<<"\n\n\tEnter the cassette data :";
for(i=0;i<n2;i++)
{
cout<<"\n\n\tInformation of cassette "<<i+1;
t[i].getp();
t[i].get();
}
clrscr();
cout<<"\n\n\tSTORE RECORD :";
cout<<"\n\n
\n";
cout<<"\n\n\t\tBOOKS";
cout<<"\n\n
\n";
cout<<"\n\tTITLE\tPRICE\tNO OF PAGES \n";
cout<<"\n\n
\n";
for(i=0;i<n1;i++)
{
b[i].putp();
b[i].put();
}
cout<<"\n\n
\n";
cout<<"\n";
cout<<"\n\n\t\tCASSETTES";
cout<<"\n\n
\n";
46

cout<<"\n\tTITLE\tPRICE\tLENGTH \n";
cout<<"\n\n
\n";
for(i=0;i<n2;i++)
{
t[i].putp();
t[i].put();
}
getch();
return 0;
}
Output:
Information of book 1
Enter the Title :b1
Enter the price :123
Enter number of pages :777
How many cassette :1
Enter the cassette data :
Information of cassette 1
Enter the Title :6
Enter the price :37
Enter Length in minutes :77
TITLE PRICE NO OF PAGES
b1

123

777

CASSETTES
TITLE PRICE LENGTH
6

37

77

Ex.NO:6.B Area of circle,sector and segment


Program:
#include<iostream.h>
#include<conio.h>
#include<math.h>
47

class circle
{
protected:
float r;
public:
circle(float x){r=x;}
float area(){return 3.14*r*r;}
};
class sector:public circle
{
float angle;
public :
sector(float x,float y):circle(x)
{
angle=y;
}
float sarea()
{
return (angle/360)*area();
}
};
class segment:public circle
{
float len;
public :
segment(float x,float y):circle(x)
{
len=y;
}
float segarea()
{
return r*r*(acos((r-len)/r))-(r-len)*sqrt(2*r*len-len*len);
}
};
int main()
{
clrscr();
float x,y,z;
cout<<"\n\n\tEnter the radius of the circle :";
cin>>x;
cout<<"\n\n\tEnter the angle of sector :";
cin>>y;
cout<<"\n\n\tEnter the length of the segment :";
cin>>z;
segment sg(x,z);
sector se(x,y);
48

cout<<"\n\n\tArea of the circle :"<<se.area();


cout<<"\n\n\tArea of the sector :"<<se.sarea();
cout<<"\n\n\tArea of the Segment :"<<sg.segarea();
getch();
return 0;
}
Output:
Enter the radius of the circle :3
Enter the angle of sector :2
Enter the length of the segment :2
Area of the circle :28.26
Area of the sector :0.157
Area of the Segment :8.250208

EX.NO:6.cProgram for student record :multilevel inheritance


PROGRAM:
#include<iostream.h>
#include<conio.h>
//#include<stdio.h>
49

class student
{
protected:
//public:
char name[30];
int reg;
char dept[30];
void get()
{
cout<<"\n\n\tName :";
gets(name);
cout<<"\n\n\tDept :";
gets(dept);
cout<<"\n\n\tRegister number :";
cin>>reg;
}
void put()
{
cout<<endl;
cout<<"\t"<<name<<"\t"<<dept<<"\t\t"<<reg;
}
};
class test:public student
{
protected:
float mark[5];
public:
void get();
};
void test::get()
{
cout<<"\n\n\tEnter the marks in five subject :";
for(int i=0;i<5;i++)
cin>>mark[i];
}
class result:public test
{
float total;
public:
result(){total=0;}
float cal();
void put();
};
float result::cal()
{
for(int i=0;i<5;i++)
50

total+=mark[i];
return total/5;
}
void result::put()
{
cout<<"\t"<<total<<"\t\t"<<cal()<<endl;
}
int main()
{
clrscr();
result r[100];
cout<<"\n\n\tHow many students :";
int n;
cin>>n;
cout<<"\n\n\tEnter the data :";
for(int i=0;i<n;i++)
{
cout<<"\n\n\tEnter the student "<<i+1<<" Data :";
r[i].student::get();
r[i].test::get();
}
clrscr();
cout<<"\n\n\t\t\tSTUDENT INFORMATION :";
cout<<"\n
\n";
cout<<"\n\tNAME\tDEPARTMENT\tREG No\tTOTAL MARKS\tAVG";
cout<<"\n
\n";
for(i=0;i<n;i++)
{
r[i].student::put();
r[i].put();
}
cout<<"\n
\n";
getch();
return 0;
}
Output:
How many students :1
Enter the data :
Enter the student 1 Data :
Name :gg
Dept :cse
51

Register number :7
Enter the marks in five subject :99
44
77
88
55
STUDENT INFORMATION :

NAME DEPARTMENT REG No TOTAL MARKS AVG

gg

cse

363

72.599998

EX.NO:6.d Program for student record :multiple inheritance


PROGRAM:
#include<iostream.h>
52

#include<conio.h>
#include<stdio.h>
class student
{
protected:
char name[30];
int reg;
char dept[30];
public:
void get()
{
cout<<"\n\n\tName :";
gets(name);
cout<<"\n\n\tDept :";
gets(dept);
cout<<"\n\n\tRegister number :";
cin>>reg;
}
void put()
{
cout<<endl;
cout<<"\t"<<name<<"\t"<<dept<<"\t\t"<<reg;
}
};
class test
{
protected:
float mark[5];
public:
void get();
};
void test::get()
{
cout<<"\n\n\tEnter the marks in five subject :";
for(int i=0;i<5;i++)
cin>>mark[i];
}
class result:public student,public test
{
float total;
public:
result(){total=0;}
float cal();
void put();
};
float result::cal()
53

{
for(int i=0;i<5;i++)
total+=mark[i];
return total/5;
}
void result::put()
{
cout<<"\t"<<total<<"\t\t"<<cal()<<endl;
}
int main()
{
clrscr();
result r[100];
cout<<"\n\n\tHow many students :";
int n;
cin>>n;
cout<<"\n\n\tEnter the data :";
for(int i=0;i<n;i++)
{
cout<<"\n\n\tEnter the student "<<i+1<<" Data :";
r[i].student::get();
r[i].test::get();
}
clrscr();
cout<<"\n\n\t\t\tSTUDENT INFORMATION :";
cout<<"\n
\n";
cout<<"\n\tNAME\tDEPARTMENT\tREG No\tTOTAL MARKS\tAVG";
cout<<"\n
\n";
for(i=0;i<n;i++)
{
r[i].student::put();
r[i].put();
}
cout<<"\n
\n";
getch();
return 0;
}

Output:
54

How many students :1


Enter the data :
Enter the student 1 Data :
Name :gg
Dept :cse
Register number :7
Enter the marks in five subject :55
66
77
88
99

EX.NO:6.e Program for emp, boss, ceo : multilevel inheritance


55

PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class employee
{
public:
char name[30],dept[30],empid[10];
float salary;
void askinfo()
{
cout<<"\n\n\tName :";
gets(name);
cout<<"\n\n\tDepartment :";
gets(dept);
cout<<"\n\n\tEmployee id :";
gets(empid);
cout<<"\n\n\tSalary per month :Rs ";
cin>>salary;
}
void writeinfo()
{
cout<<"\n"<<name<<"\t"<<dept<<"\t"<<empid<<"\t"<<salary;
}
};
class boss:public employee
{
public:
float bonus;
void askinfo()
{
cout<<"\n\n\tBonus amount :";
cin>>bonus;
}
void writeinfo()
{
cout<<"\t\t"<<bonus;
}
};
class ceo:public boss
{
int no;
float price;
public:
void askinfo()
{
56

cout<<"\n\n\tNo of Shares :";


cin>>no;
cout<<"\n\n\tPrice of each share :Rs ";
cin>>price;
}
void writeinfo()
{
cout<<"\t\t"<<no<<"\t"<<price<<"\n";
}
};
int main()
{
ceo c[100];
cout<<"\n\n\tHow many employee :";
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"\n\n\tEnter record for employee "<<i+1;
c[i].employee::askinfo();
c[i].boss::askinfo();
c[i].askinfo();
}
clrscr();
cout<<"\n
\n";
cout<<"\n\t\t\tEMPLOYEE INFORMATION ";
cout<<"\n
\n";
cout<<"\nNAME\tDEPT\tID\tSALARY[Rs]\tBONUS[Rs]\tSHARE\tVALUE\SHARE[Rs]";
cout<<"\n
\n";
for(i=0;i<n;i++)
{
c[i].employee::writeinfo();
c[i].boss::writeinfo();
c[i].writeinfo();
}
cout<<"\n
\n";
getch();
return 0;
57

}
Output:
How many employee :1
Enter record for employee 1
Name :gg
Department :cse
Employee id :777
Salary per month :Rs 45789
Bonus amount :258
No of Shares :3
Price of each share :Rs 333

EMPLOYEE INFORMATION

NAME DEPT ID
SALARY[Rs]
BONUS[Rs]
SHARE VALUESHARE[Rs]

gg
cse 777 45789
258
3
333

58

EX.NO:7.a Shape class ,with rectangle and triangle derived class


PROGRAM:
#include<iostream.h>
#include<conio.h>
class shape
{
protected:
float a,b;
public:
void get()
{
cin>>a>>b;
}
virtual void print()=0;
};
class rectangle:public shape
{
public:
float area()
{
return (a*b);
}
void print()
{
cout<<"\n\n\tArea of rectangle ="<<area();
}
};
class triangle:public shape
{
public:
float area()
{
return 0.5*a*b;
}
void print()
{
cout<<"\n\n\tArea of triangle ="<<area();
}
};
int main()
{
clrscr();
shape *p;
//shape s;
59

rectangle r;
triangle t;
p=&r;
cout<<"\n\n\tEnter the value of length and breadth of rectangle :";
p->get();
p->print();
cout<<"\n\n\tEnter the value of height and base of triangle :";
p=&t;
p->get();
p->print();
getch();
return 0;
}
Output:
Enter the value of length and breadth of rectangle :5
4
Area of rectangle =20
Enter the value of height and base of triangle :7
8
Area of triangle =28

60

EX.NO:7.b Program to illustrate virtual destructor


PROGRAM:
#include<iostream.h>
#include<conio.h>
class first
{
public:
first()
{
cout<<"\n\n\tFirst constructor :";
}
virtual ~first()
{
cout<<"\n\n\tFirst destructor :";
}
};
class second:public first
{
public:
second()
{
cout<<"\n\n\tSecond constructor :";
}
~second()
{
cout<<"\n\n\tSecond destructor :";
}
};
class third:public second
{
public:
third()
{
cout<<"\n\n\tThird constructor :";
}
~third()
{
cout<<"\n\n\tThird destructor :";
}
};
int main()
{
clrscr();
61

first *f;
f=new second;;
delete f;
f=new third;
delete f;
getch();
return 0;
}
Output:
First constructor :
Second constructor :
Second destructor :
First destructor :
First constructor :
Second constructor :
Third constructor :
Third destructor :
Second destructor :
First destructor :

62

EX.NO:7.c Program to vehicle class , calculating refilling required


PROGRAM:
#include<iostream.h>
#include<conio.h>
class vehicle
{
protected:
float cap,mil,dis;
public:
virtual void getdata()=0;
virtual float distance()=0;
virtual int filling()=0;
};
class car:public vehicle
{
public:
void getdata()
{
cout<<"\n\n\tFull tank capacity [in litres] :";
cin>>cap;
cout<<"\n\n\tAverage consumption per 100 km :";
cin>>mil;
dis=distance();
}
float distance()
{
return cap*100/mil;
}
int filling()
{
float x;
cout<<"\n\n\tEnter the distance to be travelled :";
cin>>x;
return x/dis;
}
};
class truck:public vehicle
{
public:
void getdata()
{
cout<<"\n\n\tFull tank capacity [in litres] :";
cin>>cap;
cout<<"\n\n\tAverage consumption per 100 km :";
63

cin>>mil;
dis=distance();
}
float distance()
{
return cap*100/mil;
}
int filling()
{
float x;
cout<<"\n\n\tEnter the distance to be travelled :";
cin>>x;
return x/dis;
}
};
int main()
{
clrscr();
car c;
truck t;
vehicle *v;
v=&c;
v->getdata();
cout<<"\n\n\tRefilling required ="<<v->filling()<<" times ";
v=&t;
v->getdata();
cout<<"\n\n\tRefilling required ="<<v->filling()<<" times ";
getch();
return 0;
}
Output:
Full tank capacity [in litres] :444
Average consumption per 100 km :67
Enter the distance to be travelled :99
Refilling required =0 times
Full tank capacity [in litres] :666
Average consumption per 100 km :555
Enter the distance to be travelled :897
Refilling required =7 times

64

EX.NO:8.aMinimum number in an array


PROGRAM:
#include<process.h>
#include<conio.h>
template<class t>
t minimum(t a[],int n)
{
t min;
for(int i=0;i<n;i++)
{
min=a[i];
for(int j=0;j<n;j++)
if(a[j]<min)
min=a[j];
}
return min;
}
int main()
{
clrscr();
int a[100],n,ch;
float b[100];
do
{
clrscr();
cout<<"\n\n\tMAIN MENU ";
cout<<"\n\t1.INT TYPE\n\t2.FLOATING POINT TYPE\n\t3.EXIT";
cout<<"\n\n\tEnter your choice :";
cin>>ch;
switch(ch)
{
case 1:cout<<"\n\n\tEnter the size of array :";
cin>>n;
cout<<"\n\n\tEnter the "<<n<<" integer element :";
for(int i=0;i<n;i++)
cin>>a[i];
cout<<"\n\n\tMinimum number is ="<<minimum(a,n);
break;
case 2:cout<<"\n\n\tEnter the size of array :";
cin>>n;
cout<<"\n\n\tEnter the "<<n<<" floating point element :";
for(i=0;i<n;i++)
cin>>b[i];
cout<<"\n\n\tMinimum number is ="<<minimum(b,n);
65

break;
case 3: exit(0);
break;
}
getch();
}
while(1);
getch();
return 0;
}
Output:
MAIN MENU : ARRAY TYPE
1. INTEGER
2. FLOATING POINT
3. EXIT
Enter your choice: 1
Enter size: 4
Enter elements: 23 43 12 56
Minimum element is: 12
Enter your choice: 3
Exit
EX.NO:8.b
stack
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<process.h>
template <class t>
class stack
{
t *s;
int top,n;
public :
stack(int x)
{
s=new t[x];
n=x;
top=-1;
}
~stack()
{
delete []s;
}
void push(t x)
{
s[++top]=x;
66

}
void pop()
{
cout<<"\n\n\tPopped element is :"<<s[top];
top--;
}
int isempty()
{
if(top==-1)
return 1;
else
return 0;
}
t Top()
{
return s[top];
}
void view();
};
template<class t>
void stack<t>::view()
{
cout<<"\n\n\tStack is :";
for(int i=top;i>=0;i--)
cout<<"\n\t"<<s[i];
}
int main()
{
clrscr();
int ch1,ch2,x,n,nf,nc;
float y;
char z;
clrscr();
do
{
clrscr();
cout<<"\n\n\tMAIN MENU : STACK TYPE";
cout<<"\n\n\t1.INTEGER STACK\n\t2.FLOATING POINT STACK\n\t3.CHARACTER
STACK\n\t4.EXIT";
cout<<"\n\n\tEnter your choice :";
cin>>ch1;
switch(ch1)
{
case 1:
do
{
67

clrscr();
cout<<"\n\n\t\tMAIN MENU :INTEGER STACK";
cout<<"\n\n\t1.PUSH\n\t2.POP\n\t3.DISPLAY\n\t4.TOP
ELEMENT\n\t5.RETURN TO MAIN MENU [STACK TYPE]";
cout<<"\n\n\tEnter your choice :";
cin>>ch2;
switch(ch2)
{
case 1:
cout<<"\n\n\tEnter the size of stack :";
cin>>n;
stack<int> s1(n);
for(int i=0;i<n;i++)
{
cout<<"\n\n\tEnter the element :";
cin>>x;
s1.push(x);
}
break;
case 2:
s1.pop();
break;
case 3:
s1.view();
break;
case 4:
cout<<"\n\n\tElement on top is :"<<s1.Top();
break;
case 5: //exit(0);
break;
}
getch();
}while(ch2!=5);
break;
case 2:
do
{
clrscr();
cout<<"\n\n\t\tMAIN MENU :FLOATING POINT STACK";
cout<<"\n\n\t1.PUSH\n\t2.POP\n\t3.DISPLAY\n\t4.TOP
ELEMENT\n\t5.RETURN TO MAIN MENU [STACK TYPE]";
cout<<"\n\n\tEnter your choice :";
cin>>ch2;
switch(ch2)
{
case 1:
68

cout<<"\n\n\tEnter the size of stack :";


cin>>nf;
stack<float> s2(nf);
for(int i=0;i<nf;i++)
{
cout<<"\n\n\tEnter the element :";
cin>>y;
s2.push(y);
}
break;
case 2:
s2.pop();
break;
case 3:
s2.view();
break;
case 4:
cout<<"\n\n\tElement on top is :"<<s2.Top();
break;
case 5: //exit(0);
break;
}
getch();
}while(ch2!=5);
break;
case 3:
do
{
clrscr();
cout<<"\n\n\t\tMAIN MENU :CHARACTER STACK";
cout<<"\n\n\t1.PUSH\n\t2.POP\n\t3.DISPLAY\n\t4.TOP
ELEMENT\n\t5.RETURN TO MAIN MENU [STACK TYPE]";
cout<<"\n\n\tEnter your choice :";
cin>>ch2;
switch(ch2)
{
case 1:
cout<<"\n\n\tEnter the size of stack :";
cin>>nc;
stack<char> s3(nc);
for(int i=0;i<nc;i++)
{
cout<<"\n\n\tEnter the element :";
cin>>z;
s3.push(z);
}
69

break;
case 2:
s3.pop();
break;
case 3:
s3.view();
break;
case 4:
cout<<"\n\n\tElement on top is :"<<s3.Top();
break;
case 5:// exit(0);
break;
}
getch();
}while(ch2!=5);
break;
case 4:exit(0);
}
getch();
}
while(1);
getch();
return 0;
}
Output:
MAIN MENU: STACK TYPE
1. Integer
2. Floating Point
3. Character
4. Exit
Enter your choice :1
MENU : INTEGER STACK OPERATION
1. Push
2. Pop
3. Display
4. Top element
5. Return to main menu
Enter your choice :1
Enter the size :3
Enter the element :10 20 30
Enter your choice :3
Stack is: 30 20 10
Enter your choice :2
Popped element is 30
Enter your choice :4
Element at top is :20
70

Enter your choice :5


Exit
EX.NO:9a ARRAY
PROGRAM:
#include<iostream.h>
int main()
{
int a[5]={1,2,3,4,5},n;
cout<<"\n\n\tArray is :";
for(int i=0;i<5;i++)
cout<<"\t"<<a[i];
cout<<"\n\n\tEnter an index to see the element :";
cin>>n;
try
{
if(n>=5)
throw "Array index out of maximum limit :";
else
cout<<"\n\t"<<a[n];
}
catch(char *str)
{
cout<<str;
}
return 0;
}
OUTPUT:
Array is
1 2 3 4 5
Enter an index to see the element :4
4

71

EX.NO:9.b COMPLEX
PROGRAM:
#include<iostream.h>
int main()
{
int a,b,c,r1,r2,d;
cout<<"\n\n\tEnter the value of a,b and c :";
cin>>a>>b>>c;
d=b*b-4*a*c;
try
{
if(d>0)
{
cout<<"\n\n\tRoots are real and unequal :";
r1=-b+(sqrt(d))/(2*a);
r1=-b-(sqrt(d))/(2*a);
cout<<"\n\tRoot 1="<<r1<<"\t"<<"Root2 ="<<r2<<endl;
}
else if(d==0)
{
cout<<"\n\n\tRoots are real and equal :";
r1=r2=-b/(2*a);
cout<<"\n\tRoots are :"<<r1;
}
else
throw "Roots are imiginary ";
}
catch(char *str)
{
cout<<str;
}
return 0;
}
OUTPUT:
Enter the value
1
2
3
ROOTS ARE REAL AND EQUAL
Root1=-3.828
Root2=-1.828

72

EX.NO:9.c
MEMORY
PROGRAM:
#include<iostream.h>
#include<new.h>
const int n=20;
const int size=20;
void memory()
{
cout<<"\n\tMemory allocation error :";
exit(1);
}
typedef char byte[1000];
int main()
{
set_new_handler(memory);
byte *ptr[n];
for(int i=0;i<n;i++)
{
ptr[i]=new byte[size];
cout<<((i+1)*20<<"MB allocated "<<endl;
}
return 0;
}
Output:
20 MB allocated
40 MB allocated
60 MB allocated
80 MB allocated
100 MB allocated
120 MB allocated
140 MB allocated
160 MB allocated
180 MB allocated
200 MB allocated
220MB allocated
240 MB allocated
260 MB allocated
280 MB allocated
300 MB allocated
320 MB allocated
340 MB allocated
360 MB allocated
380 MB allocated
400 MB allocated
73

Memory allocated error


EX.NO:9.d
ZERO
PROGRAM:
#include<iostream.h>
Int main()
{
int a,b;
do
{
cout<<\n\n\tEnter numerator and denominator value :;
cin>>a>>b;
try
{
If(b==0)
throw divide by zero exception ;
else
cout<<\n\n\tResult =<<a/b;
}
catch(char *str)
{
cout<<str<<endl;
}
cout<<\n\n\tWant to continue :press 1 :;
cin>>ch;
}
while(ch==1);
return 0;
}
OUTPUT:
Enter the numerator and denominator value
10 5
RESULT=2

74

You might also like