SlideShare a Scribd company logo
EX.NO.1a) USE OF CONSTRUCTORS, DESTRUCTORS AND CONSTRUCTOR OVERLOADING 
PROGRAM 
#include<iostream.h> 
#include<conio.h> 
class stu 
{ private: char name[20],add[20]; 
int roll,zip; 
public: stu ( );//Constructor 
~stu( );//Destructor 
void read( ); 
void disp( ); 
}; 
stu :: stu( ) 
{ 
cout<<"This is Student Details"<<endl; } 
void stu :: read( ) 
{ 
cout<<"Enter the student Name"; 
cin>>name; 
cout<<"Enter the student roll no "; 
cin>>roll; 
cout<<"Enter the student address"; 
cin>>add; 
cout<<"Enter the Zipcode"; 
cin>>zip; } 
void stu :: disp( ) 
{ 
cout<<"nStudent Name :"<<name<<endl; 
cout<<"Roll no is :"<<roll<<endl; 
cout<<"Address is :"<<add<<endl; 
cout<<"Zipcode is :"<<zip; } 
stu :: ~stu( ) 
{ 
cout<<"Student Detail is Closed" } 
void main( ) 
{ 
stu s; 
s.read ( ); 
s.disp ( ); 
getch( ); }
OUTPUT 
This is Student Details 
Enter the student Name anu 
Enter the student roll no 101 
Enter the student address chennai 
Enter the Zipcode 620001 
Student Name : anu 
Roll no is : 101 
Address is :chennai 
Zipcode is : 620001 
Student Detail is closed
EX.NO.1b) COPY CONSTRUCTOR 
PROGRAM 
#include<iostream.h> 
#include<conio.h> 
class copy 
{ 
int var,fact; 
public: 
copy(int temp) 
{ 
var = temp; 
} 
double calculate() 
{ 
fact=1; 
for(int i=1;i<=var;i++) 
{ 
fact = fact * i; 
} 
return fact; 
} 
}; 
void main() 
{ 
clrscr(); 
int n; 
cout<<"ntEnter the Number : "; 
cin>>n; 
copy obj(n); 
copy cpy=obj; 
cout<<"nt"<<n<<" Factorial is:"<<obj.calculate(); 
cout<<"nt"<<n<<" Factorial is:"<<cpy.calculate(); 
getch(); 
} 
OUTPUT 
Enter the Number: 5 
Factorial is: 120 
Factorial is: 120
EX.NO.2a) FRIEND FUNCTION 
PROGRAM 
#include<iostream.h> 
#include<conio.h> 
class base 
{ 
int val1,val2; 
public: 
void get() 
{ 
cout<<"Enter two values:"; 
cin>>val1>>val2; 
} 
friend float mean(base ob); 
}; 
float mean(base ob) 
{ 
return float(ob.val1+ob.val2)/2; 
} 
void main() 
{ 
clrscr(); 
base obj; 
obj.get(); 
cout<<"n Mean value is : "<<mean(obj); 
getch(); 
} 
OUTPUT 
Enter two values: 10, 20 
Mean Value is: 15
EX.NO.2b) FRIEND CLASS 
PROGRAM 
#include <iostream.h> 
class CSquare; 
class CRectangle { 
int width, height; 
public: 
int area (void) 
{return (width * height);} 
void convert (CSquare a); 
}; 
class CSquare { 
private: 
int side; 
public: 
void set_side (int a) 
{side=a;} 
friend class CRectangle; 
}; 
void CRectangle::convert (CSquare a) { 
width = a.side; 
height = a.side; 
} 
int main () { 
CSquare sqr; 
CRectangle rect; 
sqr.set_side(4); 
rect.convert(sqr); 
cout << rect.area(); 
return 0; 
} 
OUTPUT 
The output is: 
16
EX.NO.3a) INHERITANCE-SINGLE INHERITANCE 
PROGRAM 
#include<iostream.h> 
#include<conio.h> 
class emp 
{ 
public: 
int eno; 
char name[20],des[20]; 
void get() 
{ 
cout<<"Enter the employee number:"; 
cin>>eno; 
cout<<"Enter the employee name:"; 
cin>>name; 
cout<<"Enter the designation:"; 
cin>>des; 
} 
}; 
class salary:public emp 
{ 
float bp,hra,da,pf,np; 
public: 
void get1() 
{ 
cout<<"Enter the basic pay:"; 
cin>>bp; 
cout<<"Enter the Humen Resource Allowance:"; 
cin>>hra; 
cout<<"Enter the Dearness Allowance :"; 
cin>>da; 
cout<<"Enter the Profitablity Fund:"; 
cin>>pf; 
} 
void calculate() 
{ 
np=bp+hra+da-pf; 
} 
void display() 
{ 
cout<<eno<<"t"<<name<<"t"<<des<<"t"<<bp<<"t"<<hra<<"t"<<da<<"t"<<pf<<"t"<<np<<"n"; 
} 
};
void main() 
{ 
int i,n; 
char ch; 
salary s[10]; 
clrscr(); 
cout<<"Enter the number of employee:"; 
cin>>n; 
for(i=0;i<n;i++) 
{ 
s[i].get(); 
s[i].get1(); 
s[i].calculate(); 
} 
cout<<"ne_no t e_namet des t bp t hra t da t pf t np n"; 
for(i=0;i<n;i++) 
{ 
s[i].display(); 
} 
getch(); 
} 
OUTPUT 
Enter the Number of employee:1 
Enter the employee No: 150 
Enter the employee Name: ram 
Enter the designation: Manager 
Enter the basic pay: 5000 
Enter the HR allowance: 1000 
Enter the Dearness allowance: 500 
Enter the profitability Fund: 300 
E.No E.name des BP HRA DA PF NP 
150 ram Manager 5000 1000 500 300 6200
EX.NO.3b) MULTIPLE INHERITANCE 
PROGRAM 
#include<iostream.h> 
#include<conio.h> 
class student 
{ 
protected: 
int rno,m1,m2; 
public: 
void get() 
{ 
cout<<"Enter the Roll no :"; 
cin>>rno; 
cout<<"Enter the two marks :"; 
cin>>m1>>m2; 
} 
}; 
class sports 
{ 
protected: 
int sm; // sm = Sports mark 
public: 
void getsm() 
{ 
cout<<"nEnter the sports mark :"; 
cin>>sm; 
} 
}; 
class statement:public student,public sports 
{ 
int tot,avg; 
public: 
void display() 
{ 
tot=(m1+m2+sm); 
avg=tot/3; 
cout<<"nntRoll No : "<<rno<<"ntTotal : "<<tot; 
cout<<"ntAverage : "<<avg; 
} 
};
void main() 
{ 
clrscr(); 
statement obj; 
obj.get(); 
obj.getsm(); 
obj.display(); 
getch(); 
} 
OUTPUT 
Enter the Roll no: 100 
Enter two marks 
90 
80 
Enter the Sports Mark : 90 
Roll No : 100 
Total : 260 
Average : 86.66
EX.NO.4 POLYMORPHISM - FUNCTION OVERLOADING 
PROGRAM 
#include<iostream.h> 
#include<conio.h> 
int area(int); 
int area(int,int); 
float area(float,int,int); 
void main() 
{ 
clrscr(); 
cout<< "n Area Of Square: "<< area(5); 
cout<< "n Area Of Rectangle: "<< area(4,4); 
cout<< "n Area Of Circle: "<< area(4.0,3,3); 
getch(); 
} 
int area(int a) 
{ 
return (a*a); 
} 
int area(int a,int b) 
{ 
return(a*b); 
} 
float area(float r,int a,int b) 
{ 
return(3.14 * r * r); 
} 
OUTPUT 
Area Of Square: 25 
Area Of Rectangle: 16 
Area Of Circle: 28.26
EX.NO.5a) VIRTUAL FUNCTION 
PROGRAM 
#include<iostream.h> 
#include<conio.h> 
class B 
{ 
public: 
virtual void display() 
{ 
cout<<"Content Of base class:"; 
}}; 
class D1:public B 
{ 
public: 
void display() 
{ 
cout<<"Content of first derived class:"; 
}}; 
class D2:public B 
{ 
public: 
void display(){ 
cout<<"Content of second derived class:"; 
}}; 
int main() 
{ 
clrscr(); 
B *b; 
D1 d1; 
D2 d2; 
b=&d1; 
b->display(); 
b=&d2; 
b->display(); 
getch(); 
return 0; 
} 
OUTPUT 
Content of first derived class 
Content of second derived class
EX.NO.5b) PURE VIRTUAL FUNCTION 
PROGRAM 
#include<iostream.h> 
#include<conio.h> 
class B 
{ 
public: 
virtual void display()=0; 
}; 
class D1:public B 
{ 
public: 
void display() 
{ 
cout<<"Content of first derived class:"; 
}}; 
class D2:public B 
{ 
public: 
void display(){ 
cout<<"Content of second derived class:"; 
}}; 
int main() 
{ 
clrscr(); 
B *b; 
D1 d1; 
D2 d2; 
b=&d1; 
b->display(); 
b=&d2; 
b->display(); 
getch(); 
return 0; 
} 
OUTPUT 
Content of first derived class 
Content of second derived class
EX.NO.6a) UNARY OPERATOR - MEMBER FUNCTION 
PROGRAM 
#include<iostream.h> 
#include<conio.h> 
class complex 
{ 
int a,b,c; 
public: 
complex(){} 
void getvalue() 
{ 
cout<<"Enter the Two Numbers:"; 
cin>>a>>b; 
} 
void operator++() 
{ 
a=++a; 
b=++b; 
} 
void operator--() 
{ 
a=--a; 
b=--b; 
} 
void display() 
{ 
cout<<a<<"+t"<<b<<"i"<<endl; 
} 
}; 
void main() 
{ 
clrscr();
complex obj; 
obj.getvalue(); 
obj++; 
cout<<"Increment Complex Numbern"; 
obj.display(); 
obj--; 
cout<<"Decrement Complex Numbern"; 
obj.display(); 
getch(); 
} 
OUTPUT 
Enter the two numbers: 3 6 
Increment Complex Number 
4 + 7i 
Decrement Complex Number 
3 + 6i
EX.NO.6b) UNARY OPERATOR - FRIEND FUNCTION 
PROGRAM 
#include<iostream.h> 
class complex 
{ 
float real,imag; 
public: 
complex() 
{ 
real=imag=0.0; 
} 
void read_data() 
{ 
cout<<"real part?"; 
cin>>real; 
cout<<"image part?"; 
cin>>imag; 
} 
void put_data() 
{ 
cout<<"("<<real; 
cout<<","<<imag<<")"; 
} 
friend complex operator -(complex c1) 
{ 
complex c; 
c.real=c1.real; 
c.imag=-c1.imag; 
return(c); 
} 
}; 
void main() 
{ 
complex c1,c2; 
cout<<"enter complex c1"<<endl;
c1.read_data(); 
c2=-c1; 
c2.put_data(); 
} 
OUTPUT 
enter complex c1 
real part?6 
image part?5 
(-6,5)
EX.NO.6c) BINARY OPERATOR - MEMBER FUNCTION 
PROGRAM 
#include<iostream.h> 
#include<conio.h> 
class complex 
{ 
int a,b; 
public: 
void getvalue() 
{ 
cout<<"Enter the value of Complex Numbers a,b:"; 
cin>>a>>b; 
} 
complex operator+(complex ob) 
{ 
complex t; 
t.a=a+ob.a; 
t.b=b+ob.b; 
return(t); 
} 
complex operator-(complex ob) 
{ 
complex t; 
t.a=a-ob.a; 
t.b=b-ob.b; 
return(t); 
} 
void display() 
{ 
cout<<a<<"+"<<b<<"i"<<"n"; 
} 
}; 
void main() 
{ 
clrscr(); 
complex obj1,obj2,result,result1; 
obj1.getvalue();
obj2.getvalue(); 
result = obj1+obj2; 
result1=obj1-obj2; 
cout<<"Input Values:n"; 
obj1.display(); 
obj2.display(); 
cout<<"Result:"; 
result.display(); 
result1.display(); 
getch(); 
} 
OUTPUT 
Enter the value of Complex Numbers a, b 
4 5 
Enter the value of Complex Numbers a, b 
2 2 
Input Values 
4 + 5i 
2 + 2i 
Result 
6 + 7i 
2 + 3i
EX.NO.6d) BINARY OPERATOR – FRIEND FUNCTION 
PROGRAM 
#include <iostream.h> 
#include<conio.h> 
class Values 
{ 
int a,b; 
public: 
Values(){} 
Values(int aa,int bb) 
{ 
a=aa; 
b=bb; 
} 
void show() 
{ 
cout <<a<<" "; 
cout <<b<<"n"; 
} 
friend Values operator+(Values p1 ,Values p2); //friend 
Values operator-(Values p2); 
Values operator=(Values p2); 
Values operator++(); 
}; 
//Now,+is overloaded using friend function. 
Values operator+(Values p1 ,Values p2) 
{ 
Values temp; 
temp.a =p1.a +p2.a; 
temp.b =p1.b +p2.b; 
return temp; 
} 
Values Values::operator-(Values p2) 
{ 
Values temp; 
temp.a =a +p2.b; 
temp.b =a +p2.b; 
return temp; 
}
Values Values::operator=(Values p2) 
{ 
a =p2.a; 
b =p2.a; 
return *this; 
} 
Values Values::operator++() 
{ 
a++; 
b++; 
return *this; 
} 
void main() 
{ 
clrscr(); 
Values v1 (20,30),v2(15,40); 
v1.show(); 
v2.show(); 
++v1; 
v1.show(); 
v2 =++v1 ; 
v1.show(); 
v2.show(); 
v1=v2; 
v1.show(); 
v2.show(); 
getch(); 
} 
OUTPUT 
20 30 
15 40 
21 31 
22 32 
22 22 
22 22 
22 22
EX.NO.7a) CLASS TEMPLATE 
PROGRAM 
#include <iostream> 
#include <vector> 
using namespace std; 
template <typename T> 
class MyQueue{ 
std::vector<T> data; 
public: 
void Add(T const &); 
void Remove(); 
void Print();}; 
template <typename T> void MyQueue<T> ::Add(T const &d){ 
data.push_back(d);} 
template <typename T> void MyQueue<T>::Remove(){ 
data.erase(data.begin( ) + 0,data.begin( ) + 1);} 
template <typename T> void MyQueue<T>::Print(){ 
std::vector <int>::iterator It1; 
It1 = data.begin(); 
for ( It1 = data.begin( ) ; It1 != data.end( ) ; It1++ ) 
cout << " " << *It1<<endl; } 
//Usage for C++ class templates 
int main(){ 
MyQueue<int> q; 
q.Add(1); 
q.Add(2); 
cout<<"Before removing data"<<endl; 
q.Print(); 
q.Remove(); 
cout<<"After removing data"<<endl; 
q.Print();} 
OUTPUT 
Before removing data 
1 
2 
After removing data 
2
EX.NO.7b) FUNCTION TEMPLATE 
PROGRAM: 
#include<iostream.h> 
#include<conio.h> 
template<class t> 
void swap(t &x,t &y) 
{ 
t temp=x; 
x=y; 
y=temp; 
} 
void fun(int a,int b,float c,float d) 
{ 
cout<<"na and b before swaping :"<<a<<"t"<<b; 
swap(a,b); 
cout<<"na and b after swaping :"<<a<<"t"<<b; 
cout<<"nnc and d before swaping :"<<c<<"t"<<d; 
swap(c,d); 
cout<<"nc and d after swaping :"<<c<<"t"<<d; 
} 
void main() 
{ 
int a,b; 
float c,d; 
clrscr(); 
cout<<"Enter A,B values(integer):"; 
cin>>a>>b; 
cout<<"Enter C,D values(float):"; 
cin>>c>>d; 
fun(a,b,c,d); 
getch(); 
} 
OUTPUT: 
Enter A, B values (integer): 10 20 
Enter C, D values (float): 2.50 10.80 
A and B before swapping: 10 20 
A and B after swapping: 20 10 
C and D before swapping: 2.50 10.80 
C and D after swapping: 10.80 2.50
EX.NO.8a) EXCEPTION HANDLING 
PROGRAM 
#include<iostream.h> 
#include<conio.h> 
void test(int x) 
{ 
try 
{ 
if(x>0) 
throw x; 
else 
throw 'x'; 
} 
catch(int x) 
{ 
cout<<"Catch a integer and that integer is:"<<x; 
} 
catch(char x) 
{ 
cout<<"Catch a character and that character is:"<<x; 
} 
} 
void main() 
{ 
clrscr(); 
cout<<"Testing multiple catchesn:"; 
test(10); 
test(0); 
getch(); 
} 
OUTPUT 
Testing multiple catches 
Catch a integer and that integer is: 10 
Catch a character and that character is: x
EX.NO.8b) EXCEPTION HANDLING FOR DIVIDE BY ZERO EXCEPTION 
PROGRAM 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
int a,b,c; 
float d; 
clrscr(); 
cout<<"Enter the value of a:"; 
cin>>a; 
cout<<"Enter the value of b:"; 
cin>>b; 
cout<<"Enter the value of c:"; 
cin>>c; 
try 
{ 
if((a-b)!=0) 
{ 
d=c/(a-b); 
cout<<"Result is:"<<d; 
} 
else 
{ 
throw(a-b); 
} 
} 
catch(int i) 
{ 
cout<<"Answer is infinite because a-b is:"<<i; 
} 
getch(); 
} 
OUTPUT 
Enter the value for a: 20 
Enter the value for b: 20 
Enter the value for c: 40 
Answer is infinite because a-b is: 0
EX.NO.9 STANDARD TEMPLATE LIBRARY 
PROGRAM 
#include <iostream> 
#include <vector> 
#include <string> 
using namespace std; 
main() 
{ 
vector<string> SS; 
SS.push_back("The number is 10"); 
SS.push_back("The number is 20"); 
SS.push_back("The number is 30"); 
cout << "Loop by index:" << endl; 
int ii; 
for(ii=0; ii < SS.size(); ii++) 
{ 
cout << SS[ii] << endl; 
} 
cout << endl << "Constant Iterator:" << endl; 
vector<string>::const_iterator cii; 
for(cii=SS.begin(); cii!=SS.end(); cii++) 
{ 
cout << *cii << endl; 
} 
cout << endl << "Reverse Iterator:" << endl; 
vector<string>::reverse_iterator rii; 
for(rii=SS.rbegin(); rii!=SS.rend(); ++rii) 
{ 
cout << *rii << endl; 
} 
cout << endl << "Sample Output:" << endl; 
cout << SS.size() << endl; 
cout << SS[2] << endl; 
swap(SS[0], SS[2]); 
cout << SS[2] << endl; 
} 
OUTPUT 
Loop by index: 
The number is 10 
The number is 20 
The number is 30 
Constant Iterator: 
The number is 10 
The number is 20 
The number is 30 
Reverse Iterator: 
The number is 30 
The number is 20 
The number is 10 
Sample Output: 
3 
The number is 30 
The number is 10
EX.NO.10a) FILE STREAM CLASSES 
PROGRAM 
#include<fstream.h> 
#include<stdio.h> 
#include<ctype.h> 
#include<string.h> 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
char c,u; 
char fname[10]; 
clrscr(); 
ofstream out; 
cout<<"Enter File Name:"; 
cin>>fname; 
out.open(fname); 
cout<<"Enter the text(Enter # at end)n"; //write contents to file 
while((c=getchar())!='#') 
{ 
u=c-32; 
out<<u; 
} 
out.close(); 
ifstream in(fname); //read the contents of file 
cout<<"nnttThe File containsnn"; 
while(in.eof()==0) 
{ 
in.get(c); 
cout<<c; 
} 
getch(); 
} 
OUTPUT 
Enter File Name: two.txt 
Enter contents to store in file (enter # at end) 
oops programming 
The File Contains 
OOPS PROGRAMMING
EX.NO.10b) FILE STREAM USING MANIPULATOR 
PROGRAM 
#include <iostream> 
using namespace std; 
#include <iomanip> 
void main(void) 
{ 
int p; 
cout<<"Enter a decimal number:"<<endl; 
cin>>p; 
cout<<p<< " in hexadecimal is: " 
<<hex<<p<<'n' 
<<dec<<p<<" in octal is: " 
<<oct<<p<<'n' 
<<setbase(10) <<p<<" in decimal is: " 
<<p<<endl; 
cout<<endl; 
} 
Output: 
Enter a decimal number : 365 
365 in Hexadecimal is : 16d 
365 in Octal is : 555 
365 in Decimal is : 555
EX.NO.11a) APPLICATION OF STACK-CONVERT INFIX TO POSTFIX EXPRESSION 
PROGRAM 
#include<stdio.h> 
#include<ctype.h> 
#include<string.h> 
static char str[20]; 
int top=-1; 
main() 
{ char in[20],post[20],ch; 
int i,j,l; 
clrscr(); 
printf("enter the string"); 
gets(in); 
l=strlen(in); 
for(i=0,j=0;i<l;i++) 
if(isalpha(in[i])) 
post[j++]=in[i]; 
else 
{ if(in[i]=='(') 
push(in[i]); 
else if(in[i]==')') 
while((ch=pop())!='(') 
post[j++]=ch; 
else 
{ 
while(priority(in[i])<=priority(str[top])) 
post[j++]=pop(); 
push(in[i]); 
}} 
while(top!=-1) 
post[j++]=pop(); 
post[j]='0'; 
printf("n equivalent infix to postfix is:%s",post); 
getch(); 
} 
priority (char c) 
{s 
witch(c) 
{ case'+': 
case'-': return 1; 
176 
case'*': 
case'/': 
return 2; 
case'$': 
return 3; 
}r 
eturn 0; 
} 
push(char c) 
{s 
tr[++top]=c; 
} 
pop() 
{return(str[top--]); 
}}
OUTPUT 
enter the string(a+b)-(c-d)*e/f 
equivalent infix to postfix is:ab+cd-e*f/- 
enter the stringa+b/c*d 
equivalent infix to postfix is:abc/d*+
EX.NO.11b) APPLICATION OF QUEUE 
PROGRAM 
#include <iostream.h> 
#include <stdlib.h> 
struct node 
{ 
int info; 
struct node *ptr; 
}*front,*rear,*temp,*front1; 
int frontelement(); 
void enq(int data); 
void deq(); 
void empty(); 
void display(); 
void create(); 
void queuesize(); 
int count = 0; 
void main() 
{ 
int no, ch, e; 
cout<<"n 1 - Enque"; 
cout<<"n 2 - Deque"; 
cout<<"n 3 - Front element"; 
cout<<"n 4 - Empty";
cout<<"n 5 - Exit"; 
cout<<"n 6 - Display"; 
cout<<"n7 - Queue size"; 
create(); 
while (1) 
{ 
cout<<"n Enter choice : "; 
cin>>ch; 
switch (ch) 
{ 
case 1: 
cout<<”Enter data : "; 
cin>>no; 
enq(no); 
break; 
case 2: 
deq(); 
break; 
case 3: 
e = frontelement(); 
if (e != 0) 
cout<<"Front element : ">> e; 
else 
cout<<"n No front element in Queue as queue is empty"; 
break; 
case 4: 
empty(); 
break; 
case 5:
exit(0); 
case 6: 
display(); 
break; 
case 7: 
queuesize(); 
break; 
default: 
cout<<"Wrong choice, Please enter correct choice "; 
break; 
} 
} 
} 
/* Create an empty queue */ 
void create() 
{ 
front = rear = NULL; 
} 
/* Returns queue size */ 
void queuesize() 
{ 
Cout<<"n Queue size : %d", count”; 
} 
/* Enqueing the queue */ 
void enq(int data) 
{
if (rear == NULL) 
{ 
rear = (struct node *)malloc(1*sizeof(struct node)); 
rear->ptr = NULL; 
rear->info = data; 
front = rear; 
} 
else 
{ 
temp=(struct node *)malloc(1*sizeof(struct node)); 
rear->ptr = temp; 
temp->info = data; 
temp->ptr = NULL; 
rear = temp; 
} 
count++; 
} 
/* Displaying the queue elements */ 
void display() 
{ 
front1 = front; 
if ((front1 == NULL) && (rear == NULL)) 
{ 
Cout<<”Queue is empty"; 
return; 
}
while (front1 != rear) 
{ 
Cout<< front1->info; 
front1 = front1->ptr; 
} 
if (front1 == rear) 
cout<< front1->info; 
} 
/* Dequeing the queue */ 
void deq() 
{ 
front1 = front; 
if (front1 == NULL) 
{ 
Cout<<”n Error: Trying to display elements from empty queue"; 
return; 
} 
else 
if (front1->ptr != NULL) 
{ 
front1 = front1->ptr; 
cout<<"n Dequed value : ", front->info; 
free(front); 
front = front1; 
} 
else 
{
Cout<<”n Dequed value : %d", front->info”; 
free(front); 
front = NULL; 
rear = NULL; 
} 
count--; 
} 
/* Returns the front element of queue */ 
int frontelement() 
{ 
if ((front != NULL) && (rear != NULL)) 
return(front->info); 
else 
return 0; 
} 
/* Display if queue is empty or not */ 
void empty() 
{ 
if ((front == NULL) && (rear == NULL)) 
cout<<”n Queue empty"; 
else 
cout<<”nQueue not empty"; 
}
OUTPUT 
1 - Enque 
2 - Deque 
3 - Front element 
4 - Empty 
5 - Exit 
6 - Display 
7 - Queue size 
Enter choice : 1 
Enter data : 14 
Enter choice : 1 
Enter data : 85 
Enter choice : 1 
Enter data : 38 
Enter choice : 3 
Front element : 14 
Enter choice : 6 
14 85 38 
Enter choice : 7 
Queue size : 3 
Enter choice : 2 
Dequed value : 14 
Enter choice : 6 
85 38 
Enter choice : 7 
Queue size : 2 
Enter choice : 4 
Queue not empty 
Enter choice : 5
EX.NO.12 BINARY SEARCH TREE 
PROGRAM 
#include<iostream.h> 
#include<conio.h> 
#include<process.h> 
struct tree_node 
{ 
tree_node *left; 
tree_node *right; 
int data; 
} ; 
class bst 
{ 
tree_node *root; 
public: 
bst() 
{ 
root=NULL; 
} 
int isempty() 
{ 
return(root==NULL); 
} 
void insert(int item); 
void inordertrav(); 
void inorder(tree_node *); 
void postordertrav(); 
void postorder(tree_node *); 
void preordertrav(); 
void preorder(tree_node *); 
}; 
void bst::insert(int item) 
{ 
tree_node *p=new tree_node; 
tree_node *parent; 
p->data=item; 
p->left=NULL; 
p->right=NULL;
parent=NULL; 
if(isempty()) 
root=p; 
else 
{ 
tree_node *ptr; 
ptr=root; 
while(ptr!=NULL) 
{ 
parent=ptr; 
if(item>ptr->data) 
ptr=ptr->right; 
else 
ptr=ptr->left; 
} 
if(item<parent->data) 
parent->left=p; 
else 
parent->right=p; 
} 
} 
void bst::inordertrav() 
{ 
inorder(root); 
} 
void bst::inorder(tree_node *ptr) 
{ 
if(ptr!=NULL) 
{ 
inorder(ptr->left); 
cout<<" "<<ptr->data<<" "; 
inorder(ptr->right); 
} 
} 
void bst::postordertrav() 
{ 
postorder(root);
} 
void bst::postorder(tree_node *ptr) 
{ 
if(ptr!=NULL) 
{ 
postorder(ptr->left); 
postorder(ptr->right); 
cout<<" "<<ptr->data<<" "; 
} 
} 
void bst::preordertrav() 
{ 
preorder(root); 
} 
void bst::preorder(tree_node *ptr) 
{ 
if(ptr!=NULL) 
{ 
cout<<" "<<ptr->data<<" "; 
preorder(ptr->left); 
preorder(ptr->right); 
}} 
void main(){ 
bst b; 
b.insert(52); 
b.insert(25); 
b.insert(50); 
b.insert(15); 
b.insert(40); 
b.insert(45); 
b.insert(20); cout<<"inorder"<<endl; 
b.inordertrav(); 
cout<<endl<<"postorder"<<endl; 
b.postordertrav(); 
cout<<endl<<"preorder"<<endl; 
b.preordertrav(); 
getch();}
OUTPUT 
inorder 
15 20 25 40 45 50 52 
postorder 
20 15 45 40 50 25 52 
preorder 
52 25 15 20 50 40 45
EX.NO.13 TREE TRAVERSAL TECHNIQUES 
PROGRAM 
#include<iostream.h> 
#include<conio.h> 
#include<process.h> 
struct tree_node 
{ 
tree_node *left; 
tree_node *right; 
int data; 
} ; 
class bst 
{ 
tree_node *root; 
public: 
bst() 
{ 
root=NULL; 
} 
int isempty() 
{ 
return(root==NULL); 
} 
void insert(int item); 
void inordertrav(); 
void inorder(tree_node *); 
void postordertrav(); 
void postorder(tree_node *); 
void preordertrav(); 
void preorder(tree_node *); 
}; 
void bst::insert(int item) 
{ 
tree_node *p=new tree_node; 
tree_node *parent; 
p->data=item; 
p->left=NULL;
p->right=NULL; 
parent=NULL; 
if(isempty()) 
root=p; 
else 
{ 
tree_node *ptr; 
ptr=root; 
while(ptr!=NULL) 
{ 
parent=ptr; 
if(item>ptr->data) 
ptr=ptr->right; 
else 
ptr=ptr->left; 
} 
if(item<parent->data) 
parent->left=p; 
else 
parent->right=p; 
} 
} 
void bst::inordertrav() 
{ 
inorder(root); 
} 
void bst::inorder(tree_node *ptr) 
{ 
if(ptr!=NULL) 
{ 
inorder(ptr->left); 
cout<<" "<<ptr->data<<" "; 
inorder(ptr->right); 
} 
} 
void bst::postordertrav() 
{ 
postorder(root); 
}
void bst::postorder(tree_node *ptr) 
{ 
if(ptr!=NULL) 
{ 
postorder(ptr->left); 
postorder(ptr->right); 
cout<<" "<<ptr->data<<" "; 
} 
} 
void bst::preordertrav() 
{ 
preorder(root); 
} 
void bst::preorder(tree_node *ptr) 
{ 
if(ptr!=NULL) 
{ 
cout<<" "<<ptr->data<<" "; 
preorder(ptr->left); 
preorder(ptr->right); 
} 
} 
void main() 
{ 
bst b; 
b.insert(52); 
b.insert(25); 
b.insert(50); 
b.insert(15); 
b.insert(40); 
b.insert(45); 
b.insert(20); cout<<"inorder"<<endl; 
b.inordertrav(); 
cout<<endl<<"postorder"<<endl; 
b.postordertrav(); 
cout<<endl<<"preorder"<<endl; 
b.preordertrav(); 
getch();}
OUTPUT 
inorder 
15 20 25 40 45 50 52 
postorder 
20 15 45 40 50 25 52 
preorder 
52 25 15 20 50 40 45
EX.NO.14a) MINIMUM SPANNING TREE USING PRIM’S ALGORITHM 
PROGRAM 
#include<iostream> 
#include<conio.h> 
#include<stdlib.h> 
using namespace std; 
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10],u; 
main() 
{ 
int m,c; 
cout <<"enterno of vertices"; 
cin >> n; 
cout <<"ente no of edges"; 
cin >> m; 
cout <<"nEDGES Costn"; 
for(k=1;k<=m;k++) 
{ 
cin >>i>>j>>c; 
cost[i][j]=c; 
}f 
or(i=1;i<=n;i++) 
for(j=1;j<=n;j++) 
if(cost[i][j]==0) 
cost[i][j]=31999; 
cout <<"ORDER OF VISITED VERTICES"; 
k=1; 
while(k<n) 
{ 
m=31999; 
if(k==1) 
{ 
for(i=1;i<=n;i++) 
for(j=1;j<=m;j++) 
if(cost[i][j]<m) 
{ 
m=cost[i][j]; 
u=i; 
}}e 
lse 
{ 
for(j=n;j>=1;j--) 
if(cost[v][j]<m && visited[j]!=1 && visit[j]!=1) 
{ 
visit[j]=1; 
stk[top]=j; 
top++; 
m=cost[v][j]; 
u=j; 
} 
}c 
ost[v][u]=31999; 
v=u;
cout<<v << " "; 
k++; 
visit[v]=0; visited[v]=1; 
} 
} 
OUTPUT 
enterno of vertices7 
ente no of edges9 
EDGES Cost 
1 6 10 
6 5 25 
5 4 22 
4 3 12 
3 2 16 
2 7 14 
5 7 24 
4 7 18 
1 2 28 
ORDER OF VISITED VERTICES1 6 5 4 3 2
Ex.no.14b) IMPLEMENTATION OF KRUSKAL’S ALGORITHM 
PROGRAM 
#include<iostream.h> 
#include<conio.h> 
typedef struct edge 
{i 
nt node1,node2,wt; 
}edge; 
void sortedge(edge a[],int n) 
{i 
nt i,j; 
edge temp; 
for(i=0;i<n;i++) 
for(j=i+1;j<n;++j) 
if(a[i].wt>a[j].wt) 
{t 
emp=a[i];a[i]=a[j];a[j]=temp; 
}}i 
nt check(int p[],int i,int j) 
{i 
nt v1,v2; 
v1=i; 
v2=j; 
while(p[i]>-1) 
i=p[i]; 
while(p[j]>-1) 
j=p[j]; 
if(i!=j) 
{ 
p[j]=i; 
cout<<"v1->v2n",v1,v2; 
return 1; 
}r 
eturn 0; 
} 
void main() 
{e 
dge e[100]; 
int r[100],n,i,j,k=1,m,cost=0; 
clrscr(); 
cout<<"Kruskal algorithmn"; 
cout<<"Enter the no of nodes:"; 
cin>>n; 
for(i=0;i<n;i++) 
r[i]=-1; 
i=0; 
cout<<"nEnter no of edges:"; 
cin>>m; 
for(i=0;i<m;i++) 
{c 
out<<"nENter the edge and cost of the edge:"; 
cin>>e[i].node1; 
cin>>e[i].node2; 
cin>>e[i].wt;
}s 
ortedge(e,m); 
cout<<"nEdges of the MSTn"; 
i=0; 
while(k<n) 
{i 
f(check(r,e[i].node1,e[i].node2)) 
{ 
k++; 
cost=cost+e[i].wt; 
i++; 
}}c 
out<<"Minimum cost:%d",cost; 
getch(); 
} 
OUTPUT 
Kruskal algorithm 
Enter the no of nodes:4 
Enter no of edges:4 
Enter the edge and cost of the edge:1 
2 
1 
Enter the edge and cost of the edge:1 
3 
3 
Enter the edge and cost of the edge:2 
3 
2 
Enter the edge and cost of the edge:2 
4 
1 
Edges of the MST 
1->2 
2->4 
2->3 
Minimum cost:4
EX.NO.15 IMPLEMENTATION OF DIJKSTRA’S ALGORITHM 
PROGRAM 
#include<iostream.h> 
#include<conio.h> 
#define INFINITY 1000 
int a[10][10],b[10][10]; 
int i,j,k,n; 
void input(); 
void initialize(); 
void spath(); 
void display(); 
void input() 
{c 
out<<"nt *** DIJKSTRA'S ALGORITHM ***"; 
cout<<"n enter the no of vertices:"; 
cin>>n; 
for(i=1;i<=n;i++) 
for(j=1;j<=n;j++){ 
if(i!=j){ 
cout<<"cost between "<<i<<j; 
cin>>a[i][j];}}} 
void initialize() 
{f 
or(i=1;i<=n;i++) 
a[i][j]=0; 
for(i=1;i<=n;i++) 
for(j=1;j<=n;j++){ 
b[i][j]=a[i][j]; 
if(!a[i][j] && (i!=j)){ 
b[i][j]=INFINITY;}}} 
void spath() 
{f 
or(k=1;k<=n;k++) 
for(i=1;i<=n;i++) 
for(j=1;j<=n;j++) 
if((b[i][k] && b[k][j]) && (b[i][k]+b[k][j]<b[i][j])) 
{ 
b[i][j]=b[i][k]+b[k][j];}} 
void display(){ 
i=1; 
if(i<n) 
{f 
or(j=2;j<=n;j++) 
cout<<"Minimum cost FROM source vertex 1 TO is : "<<j<<b[i][j]; 
}} 
void main() 
{c 
lrscr(); 
input(); 
initialize(); 
spath(); 
display(); 
getch();}
OUTPUT 
*** DIJKSTRA’S ALGORITHM *** 
enter the no of vertices: 5 
cost between1—2: 2 
cost between1—3: 1 
cost between1—4: 0 
cost between1—5: 0 
cost between2—1: 2 
cost between2—3: 5 
cost between2—4: 4 
cost between2—5: 0 
cost between3—1: 1 
cost between3—2: 5 
cost between3—4: 3 
cost between3—5: 2 
cost between4—1: 0 
cost between4—2: 4 
cost between4—3: 3 
cost between4—5: 6 
cost between5—1: 0 
cost between5—2: 0 
cost between5—3: 2 
cost between5—4: 6 
minimum cost FROM 1 TO 2 is : 2 
minimum cost FROM 1 TO 3 is : 1 
minimum cost FROM 1 TO 4 is : 4 
minimum cost FROM 1 TO 5 is : 3

More Related Content

Pads lab manual final

  • 1. EX.NO.1a) USE OF CONSTRUCTORS, DESTRUCTORS AND CONSTRUCTOR OVERLOADING PROGRAM #include<iostream.h> #include<conio.h> class stu { private: char name[20],add[20]; int roll,zip; public: stu ( );//Constructor ~stu( );//Destructor void read( ); void disp( ); }; stu :: stu( ) { cout<<"This is Student Details"<<endl; } void stu :: read( ) { cout<<"Enter the student Name"; cin>>name; cout<<"Enter the student roll no "; cin>>roll; cout<<"Enter the student address"; cin>>add; cout<<"Enter the Zipcode"; cin>>zip; } void stu :: disp( ) { cout<<"nStudent Name :"<<name<<endl; cout<<"Roll no is :"<<roll<<endl; cout<<"Address is :"<<add<<endl; cout<<"Zipcode is :"<<zip; } stu :: ~stu( ) { cout<<"Student Detail is Closed" } void main( ) { stu s; s.read ( ); s.disp ( ); getch( ); }
  • 2. OUTPUT This is Student Details Enter the student Name anu Enter the student roll no 101 Enter the student address chennai Enter the Zipcode 620001 Student Name : anu Roll no is : 101 Address is :chennai Zipcode is : 620001 Student Detail is closed
  • 3. EX.NO.1b) COPY CONSTRUCTOR PROGRAM #include<iostream.h> #include<conio.h> class copy { int var,fact; public: copy(int temp) { var = temp; } double calculate() { fact=1; for(int i=1;i<=var;i++) { fact = fact * i; } return fact; } }; void main() { clrscr(); int n; cout<<"ntEnter the Number : "; cin>>n; copy obj(n); copy cpy=obj; cout<<"nt"<<n<<" Factorial is:"<<obj.calculate(); cout<<"nt"<<n<<" Factorial is:"<<cpy.calculate(); getch(); } OUTPUT Enter the Number: 5 Factorial is: 120 Factorial is: 120
  • 4. EX.NO.2a) FRIEND FUNCTION PROGRAM #include<iostream.h> #include<conio.h> class base { int val1,val2; public: void get() { cout<<"Enter two values:"; cin>>val1>>val2; } friend float mean(base ob); }; float mean(base ob) { return float(ob.val1+ob.val2)/2; } void main() { clrscr(); base obj; obj.get(); cout<<"n Mean value is : "<<mean(obj); getch(); } OUTPUT Enter two values: 10, 20 Mean Value is: 15
  • 5. EX.NO.2b) FRIEND CLASS PROGRAM #include <iostream.h> class CSquare; class CRectangle { int width, height; public: int area (void) {return (width * height);} void convert (CSquare a); }; class CSquare { private: int side; public: void set_side (int a) {side=a;} friend class CRectangle; }; void CRectangle::convert (CSquare a) { width = a.side; height = a.side; } int main () { CSquare sqr; CRectangle rect; sqr.set_side(4); rect.convert(sqr); cout << rect.area(); return 0; } OUTPUT The output is: 16
  • 6. EX.NO.3a) INHERITANCE-SINGLE INHERITANCE PROGRAM #include<iostream.h> #include<conio.h> class emp { public: int eno; char name[20],des[20]; void get() { cout<<"Enter the employee number:"; cin>>eno; cout<<"Enter the employee name:"; cin>>name; cout<<"Enter the designation:"; cin>>des; } }; class salary:public emp { float bp,hra,da,pf,np; public: void get1() { cout<<"Enter the basic pay:"; cin>>bp; cout<<"Enter the Humen Resource Allowance:"; cin>>hra; cout<<"Enter the Dearness Allowance :"; cin>>da; cout<<"Enter the Profitablity Fund:"; cin>>pf; } void calculate() { np=bp+hra+da-pf; } void display() { cout<<eno<<"t"<<name<<"t"<<des<<"t"<<bp<<"t"<<hra<<"t"<<da<<"t"<<pf<<"t"<<np<<"n"; } };
  • 7. void main() { int i,n; char ch; salary s[10]; clrscr(); cout<<"Enter the number of employee:"; cin>>n; for(i=0;i<n;i++) { s[i].get(); s[i].get1(); s[i].calculate(); } cout<<"ne_no t e_namet des t bp t hra t da t pf t np n"; for(i=0;i<n;i++) { s[i].display(); } getch(); } OUTPUT Enter the Number of employee:1 Enter the employee No: 150 Enter the employee Name: ram Enter the designation: Manager Enter the basic pay: 5000 Enter the HR allowance: 1000 Enter the Dearness allowance: 500 Enter the profitability Fund: 300 E.No E.name des BP HRA DA PF NP 150 ram Manager 5000 1000 500 300 6200
  • 8. EX.NO.3b) MULTIPLE INHERITANCE PROGRAM #include<iostream.h> #include<conio.h> class student { protected: int rno,m1,m2; public: void get() { cout<<"Enter the Roll no :"; cin>>rno; cout<<"Enter the two marks :"; cin>>m1>>m2; } }; class sports { protected: int sm; // sm = Sports mark public: void getsm() { cout<<"nEnter the sports mark :"; cin>>sm; } }; class statement:public student,public sports { int tot,avg; public: void display() { tot=(m1+m2+sm); avg=tot/3; cout<<"nntRoll No : "<<rno<<"ntTotal : "<<tot; cout<<"ntAverage : "<<avg; } };
  • 9. void main() { clrscr(); statement obj; obj.get(); obj.getsm(); obj.display(); getch(); } OUTPUT Enter the Roll no: 100 Enter two marks 90 80 Enter the Sports Mark : 90 Roll No : 100 Total : 260 Average : 86.66
  • 10. EX.NO.4 POLYMORPHISM - FUNCTION OVERLOADING PROGRAM #include<iostream.h> #include<conio.h> int area(int); int area(int,int); float area(float,int,int); void main() { clrscr(); cout<< "n Area Of Square: "<< area(5); cout<< "n Area Of Rectangle: "<< area(4,4); cout<< "n Area Of Circle: "<< area(4.0,3,3); getch(); } int area(int a) { return (a*a); } int area(int a,int b) { return(a*b); } float area(float r,int a,int b) { return(3.14 * r * r); } OUTPUT Area Of Square: 25 Area Of Rectangle: 16 Area Of Circle: 28.26
  • 11. EX.NO.5a) VIRTUAL FUNCTION PROGRAM #include<iostream.h> #include<conio.h> class B { public: virtual void display() { cout<<"Content Of base class:"; }}; class D1:public B { public: void display() { cout<<"Content of first derived class:"; }}; class D2:public B { public: void display(){ cout<<"Content of second derived class:"; }}; int main() { clrscr(); B *b; D1 d1; D2 d2; b=&d1; b->display(); b=&d2; b->display(); getch(); return 0; } OUTPUT Content of first derived class Content of second derived class
  • 12. EX.NO.5b) PURE VIRTUAL FUNCTION PROGRAM #include<iostream.h> #include<conio.h> class B { public: virtual void display()=0; }; class D1:public B { public: void display() { cout<<"Content of first derived class:"; }}; class D2:public B { public: void display(){ cout<<"Content of second derived class:"; }}; int main() { clrscr(); B *b; D1 d1; D2 d2; b=&d1; b->display(); b=&d2; b->display(); getch(); return 0; } OUTPUT Content of first derived class Content of second derived class
  • 13. EX.NO.6a) UNARY OPERATOR - MEMBER FUNCTION PROGRAM #include<iostream.h> #include<conio.h> class complex { int a,b,c; public: complex(){} void getvalue() { cout<<"Enter the Two Numbers:"; cin>>a>>b; } void operator++() { a=++a; b=++b; } void operator--() { a=--a; b=--b; } void display() { cout<<a<<"+t"<<b<<"i"<<endl; } }; void main() { clrscr();
  • 14. complex obj; obj.getvalue(); obj++; cout<<"Increment Complex Numbern"; obj.display(); obj--; cout<<"Decrement Complex Numbern"; obj.display(); getch(); } OUTPUT Enter the two numbers: 3 6 Increment Complex Number 4 + 7i Decrement Complex Number 3 + 6i
  • 15. EX.NO.6b) UNARY OPERATOR - FRIEND FUNCTION PROGRAM #include<iostream.h> class complex { float real,imag; public: complex() { real=imag=0.0; } void read_data() { cout<<"real part?"; cin>>real; cout<<"image part?"; cin>>imag; } void put_data() { cout<<"("<<real; cout<<","<<imag<<")"; } friend complex operator -(complex c1) { complex c; c.real=c1.real; c.imag=-c1.imag; return(c); } }; void main() { complex c1,c2; cout<<"enter complex c1"<<endl;
  • 16. c1.read_data(); c2=-c1; c2.put_data(); } OUTPUT enter complex c1 real part?6 image part?5 (-6,5)
  • 17. EX.NO.6c) BINARY OPERATOR - MEMBER FUNCTION PROGRAM #include<iostream.h> #include<conio.h> class complex { int a,b; public: void getvalue() { cout<<"Enter the value of Complex Numbers a,b:"; cin>>a>>b; } complex operator+(complex ob) { complex t; t.a=a+ob.a; t.b=b+ob.b; return(t); } complex operator-(complex ob) { complex t; t.a=a-ob.a; t.b=b-ob.b; return(t); } void display() { cout<<a<<"+"<<b<<"i"<<"n"; } }; void main() { clrscr(); complex obj1,obj2,result,result1; obj1.getvalue();
  • 18. obj2.getvalue(); result = obj1+obj2; result1=obj1-obj2; cout<<"Input Values:n"; obj1.display(); obj2.display(); cout<<"Result:"; result.display(); result1.display(); getch(); } OUTPUT Enter the value of Complex Numbers a, b 4 5 Enter the value of Complex Numbers a, b 2 2 Input Values 4 + 5i 2 + 2i Result 6 + 7i 2 + 3i
  • 19. EX.NO.6d) BINARY OPERATOR – FRIEND FUNCTION PROGRAM #include <iostream.h> #include<conio.h> class Values { int a,b; public: Values(){} Values(int aa,int bb) { a=aa; b=bb; } void show() { cout <<a<<" "; cout <<b<<"n"; } friend Values operator+(Values p1 ,Values p2); //friend Values operator-(Values p2); Values operator=(Values p2); Values operator++(); }; //Now,+is overloaded using friend function. Values operator+(Values p1 ,Values p2) { Values temp; temp.a =p1.a +p2.a; temp.b =p1.b +p2.b; return temp; } Values Values::operator-(Values p2) { Values temp; temp.a =a +p2.b; temp.b =a +p2.b; return temp; }
  • 20. Values Values::operator=(Values p2) { a =p2.a; b =p2.a; return *this; } Values Values::operator++() { a++; b++; return *this; } void main() { clrscr(); Values v1 (20,30),v2(15,40); v1.show(); v2.show(); ++v1; v1.show(); v2 =++v1 ; v1.show(); v2.show(); v1=v2; v1.show(); v2.show(); getch(); } OUTPUT 20 30 15 40 21 31 22 32 22 22 22 22 22 22
  • 21. EX.NO.7a) CLASS TEMPLATE PROGRAM #include <iostream> #include <vector> using namespace std; template <typename T> class MyQueue{ std::vector<T> data; public: void Add(T const &); void Remove(); void Print();}; template <typename T> void MyQueue<T> ::Add(T const &d){ data.push_back(d);} template <typename T> void MyQueue<T>::Remove(){ data.erase(data.begin( ) + 0,data.begin( ) + 1);} template <typename T> void MyQueue<T>::Print(){ std::vector <int>::iterator It1; It1 = data.begin(); for ( It1 = data.begin( ) ; It1 != data.end( ) ; It1++ ) cout << " " << *It1<<endl; } //Usage for C++ class templates int main(){ MyQueue<int> q; q.Add(1); q.Add(2); cout<<"Before removing data"<<endl; q.Print(); q.Remove(); cout<<"After removing data"<<endl; q.Print();} OUTPUT Before removing data 1 2 After removing data 2
  • 22. EX.NO.7b) FUNCTION TEMPLATE PROGRAM: #include<iostream.h> #include<conio.h> template<class t> void swap(t &x,t &y) { t temp=x; x=y; y=temp; } void fun(int a,int b,float c,float d) { cout<<"na and b before swaping :"<<a<<"t"<<b; swap(a,b); cout<<"na and b after swaping :"<<a<<"t"<<b; cout<<"nnc and d before swaping :"<<c<<"t"<<d; swap(c,d); cout<<"nc and d after swaping :"<<c<<"t"<<d; } void main() { int a,b; float c,d; clrscr(); cout<<"Enter A,B values(integer):"; cin>>a>>b; cout<<"Enter C,D values(float):"; cin>>c>>d; fun(a,b,c,d); getch(); } OUTPUT: Enter A, B values (integer): 10 20 Enter C, D values (float): 2.50 10.80 A and B before swapping: 10 20 A and B after swapping: 20 10 C and D before swapping: 2.50 10.80 C and D after swapping: 10.80 2.50
  • 23. EX.NO.8a) EXCEPTION HANDLING PROGRAM #include<iostream.h> #include<conio.h> void test(int x) { try { if(x>0) throw x; else throw 'x'; } catch(int x) { cout<<"Catch a integer and that integer is:"<<x; } catch(char x) { cout<<"Catch a character and that character is:"<<x; } } void main() { clrscr(); cout<<"Testing multiple catchesn:"; test(10); test(0); getch(); } OUTPUT Testing multiple catches Catch a integer and that integer is: 10 Catch a character and that character is: x
  • 24. EX.NO.8b) EXCEPTION HANDLING FOR DIVIDE BY ZERO EXCEPTION PROGRAM #include<iostream.h> #include<conio.h> void main() { int a,b,c; float d; clrscr(); cout<<"Enter the value of a:"; cin>>a; cout<<"Enter the value of b:"; cin>>b; cout<<"Enter the value of c:"; cin>>c; try { if((a-b)!=0) { d=c/(a-b); cout<<"Result is:"<<d; } else { throw(a-b); } } catch(int i) { cout<<"Answer is infinite because a-b is:"<<i; } getch(); } OUTPUT Enter the value for a: 20 Enter the value for b: 20 Enter the value for c: 40 Answer is infinite because a-b is: 0
  • 25. EX.NO.9 STANDARD TEMPLATE LIBRARY PROGRAM #include <iostream> #include <vector> #include <string> using namespace std; main() { vector<string> SS; SS.push_back("The number is 10"); SS.push_back("The number is 20"); SS.push_back("The number is 30"); cout << "Loop by index:" << endl; int ii; for(ii=0; ii < SS.size(); ii++) { cout << SS[ii] << endl; } cout << endl << "Constant Iterator:" << endl; vector<string>::const_iterator cii; for(cii=SS.begin(); cii!=SS.end(); cii++) { cout << *cii << endl; } cout << endl << "Reverse Iterator:" << endl; vector<string>::reverse_iterator rii; for(rii=SS.rbegin(); rii!=SS.rend(); ++rii) { cout << *rii << endl; } cout << endl << "Sample Output:" << endl; cout << SS.size() << endl; cout << SS[2] << endl; swap(SS[0], SS[2]); cout << SS[2] << endl; } OUTPUT Loop by index: The number is 10 The number is 20 The number is 30 Constant Iterator: The number is 10 The number is 20 The number is 30 Reverse Iterator: The number is 30 The number is 20 The number is 10 Sample Output: 3 The number is 30 The number is 10
  • 26. EX.NO.10a) FILE STREAM CLASSES PROGRAM #include<fstream.h> #include<stdio.h> #include<ctype.h> #include<string.h> #include<iostream.h> #include<conio.h> void main() { char c,u; char fname[10]; clrscr(); ofstream out; cout<<"Enter File Name:"; cin>>fname; out.open(fname); cout<<"Enter the text(Enter # at end)n"; //write contents to file while((c=getchar())!='#') { u=c-32; out<<u; } out.close(); ifstream in(fname); //read the contents of file cout<<"nnttThe File containsnn"; while(in.eof()==0) { in.get(c); cout<<c; } getch(); } OUTPUT Enter File Name: two.txt Enter contents to store in file (enter # at end) oops programming The File Contains OOPS PROGRAMMING
  • 27. EX.NO.10b) FILE STREAM USING MANIPULATOR PROGRAM #include <iostream> using namespace std; #include <iomanip> void main(void) { int p; cout<<"Enter a decimal number:"<<endl; cin>>p; cout<<p<< " in hexadecimal is: " <<hex<<p<<'n' <<dec<<p<<" in octal is: " <<oct<<p<<'n' <<setbase(10) <<p<<" in decimal is: " <<p<<endl; cout<<endl; } Output: Enter a decimal number : 365 365 in Hexadecimal is : 16d 365 in Octal is : 555 365 in Decimal is : 555
  • 28. EX.NO.11a) APPLICATION OF STACK-CONVERT INFIX TO POSTFIX EXPRESSION PROGRAM #include<stdio.h> #include<ctype.h> #include<string.h> static char str[20]; int top=-1; main() { char in[20],post[20],ch; int i,j,l; clrscr(); printf("enter the string"); gets(in); l=strlen(in); for(i=0,j=0;i<l;i++) if(isalpha(in[i])) post[j++]=in[i]; else { if(in[i]=='(') push(in[i]); else if(in[i]==')') while((ch=pop())!='(') post[j++]=ch; else { while(priority(in[i])<=priority(str[top])) post[j++]=pop(); push(in[i]); }} while(top!=-1) post[j++]=pop(); post[j]='0'; printf("n equivalent infix to postfix is:%s",post); getch(); } priority (char c) {s witch(c) { case'+': case'-': return 1; 176 case'*': case'/': return 2; case'$': return 3; }r eturn 0; } push(char c) {s tr[++top]=c; } pop() {return(str[top--]); }}
  • 29. OUTPUT enter the string(a+b)-(c-d)*e/f equivalent infix to postfix is:ab+cd-e*f/- enter the stringa+b/c*d equivalent infix to postfix is:abc/d*+
  • 30. EX.NO.11b) APPLICATION OF QUEUE PROGRAM #include <iostream.h> #include <stdlib.h> struct node { int info; struct node *ptr; }*front,*rear,*temp,*front1; int frontelement(); void enq(int data); void deq(); void empty(); void display(); void create(); void queuesize(); int count = 0; void main() { int no, ch, e; cout<<"n 1 - Enque"; cout<<"n 2 - Deque"; cout<<"n 3 - Front element"; cout<<"n 4 - Empty";
  • 31. cout<<"n 5 - Exit"; cout<<"n 6 - Display"; cout<<"n7 - Queue size"; create(); while (1) { cout<<"n Enter choice : "; cin>>ch; switch (ch) { case 1: cout<<”Enter data : "; cin>>no; enq(no); break; case 2: deq(); break; case 3: e = frontelement(); if (e != 0) cout<<"Front element : ">> e; else cout<<"n No front element in Queue as queue is empty"; break; case 4: empty(); break; case 5:
  • 32. exit(0); case 6: display(); break; case 7: queuesize(); break; default: cout<<"Wrong choice, Please enter correct choice "; break; } } } /* Create an empty queue */ void create() { front = rear = NULL; } /* Returns queue size */ void queuesize() { Cout<<"n Queue size : %d", count”; } /* Enqueing the queue */ void enq(int data) {
  • 33. if (rear == NULL) { rear = (struct node *)malloc(1*sizeof(struct node)); rear->ptr = NULL; rear->info = data; front = rear; } else { temp=(struct node *)malloc(1*sizeof(struct node)); rear->ptr = temp; temp->info = data; temp->ptr = NULL; rear = temp; } count++; } /* Displaying the queue elements */ void display() { front1 = front; if ((front1 == NULL) && (rear == NULL)) { Cout<<”Queue is empty"; return; }
  • 34. while (front1 != rear) { Cout<< front1->info; front1 = front1->ptr; } if (front1 == rear) cout<< front1->info; } /* Dequeing the queue */ void deq() { front1 = front; if (front1 == NULL) { Cout<<”n Error: Trying to display elements from empty queue"; return; } else if (front1->ptr != NULL) { front1 = front1->ptr; cout<<"n Dequed value : ", front->info; free(front); front = front1; } else {
  • 35. Cout<<”n Dequed value : %d", front->info”; free(front); front = NULL; rear = NULL; } count--; } /* Returns the front element of queue */ int frontelement() { if ((front != NULL) && (rear != NULL)) return(front->info); else return 0; } /* Display if queue is empty or not */ void empty() { if ((front == NULL) && (rear == NULL)) cout<<”n Queue empty"; else cout<<”nQueue not empty"; }
  • 36. OUTPUT 1 - Enque 2 - Deque 3 - Front element 4 - Empty 5 - Exit 6 - Display 7 - Queue size Enter choice : 1 Enter data : 14 Enter choice : 1 Enter data : 85 Enter choice : 1 Enter data : 38 Enter choice : 3 Front element : 14 Enter choice : 6 14 85 38 Enter choice : 7 Queue size : 3 Enter choice : 2 Dequed value : 14 Enter choice : 6 85 38 Enter choice : 7 Queue size : 2 Enter choice : 4 Queue not empty Enter choice : 5
  • 37. EX.NO.12 BINARY SEARCH TREE PROGRAM #include<iostream.h> #include<conio.h> #include<process.h> struct tree_node { tree_node *left; tree_node *right; int data; } ; class bst { tree_node *root; public: bst() { root=NULL; } int isempty() { return(root==NULL); } void insert(int item); void inordertrav(); void inorder(tree_node *); void postordertrav(); void postorder(tree_node *); void preordertrav(); void preorder(tree_node *); }; void bst::insert(int item) { tree_node *p=new tree_node; tree_node *parent; p->data=item; p->left=NULL; p->right=NULL;
  • 38. parent=NULL; if(isempty()) root=p; else { tree_node *ptr; ptr=root; while(ptr!=NULL) { parent=ptr; if(item>ptr->data) ptr=ptr->right; else ptr=ptr->left; } if(item<parent->data) parent->left=p; else parent->right=p; } } void bst::inordertrav() { inorder(root); } void bst::inorder(tree_node *ptr) { if(ptr!=NULL) { inorder(ptr->left); cout<<" "<<ptr->data<<" "; inorder(ptr->right); } } void bst::postordertrav() { postorder(root);
  • 39. } void bst::postorder(tree_node *ptr) { if(ptr!=NULL) { postorder(ptr->left); postorder(ptr->right); cout<<" "<<ptr->data<<" "; } } void bst::preordertrav() { preorder(root); } void bst::preorder(tree_node *ptr) { if(ptr!=NULL) { cout<<" "<<ptr->data<<" "; preorder(ptr->left); preorder(ptr->right); }} void main(){ bst b; b.insert(52); b.insert(25); b.insert(50); b.insert(15); b.insert(40); b.insert(45); b.insert(20); cout<<"inorder"<<endl; b.inordertrav(); cout<<endl<<"postorder"<<endl; b.postordertrav(); cout<<endl<<"preorder"<<endl; b.preordertrav(); getch();}
  • 40. OUTPUT inorder 15 20 25 40 45 50 52 postorder 20 15 45 40 50 25 52 preorder 52 25 15 20 50 40 45
  • 41. EX.NO.13 TREE TRAVERSAL TECHNIQUES PROGRAM #include<iostream.h> #include<conio.h> #include<process.h> struct tree_node { tree_node *left; tree_node *right; int data; } ; class bst { tree_node *root; public: bst() { root=NULL; } int isempty() { return(root==NULL); } void insert(int item); void inordertrav(); void inorder(tree_node *); void postordertrav(); void postorder(tree_node *); void preordertrav(); void preorder(tree_node *); }; void bst::insert(int item) { tree_node *p=new tree_node; tree_node *parent; p->data=item; p->left=NULL;
  • 42. p->right=NULL; parent=NULL; if(isempty()) root=p; else { tree_node *ptr; ptr=root; while(ptr!=NULL) { parent=ptr; if(item>ptr->data) ptr=ptr->right; else ptr=ptr->left; } if(item<parent->data) parent->left=p; else parent->right=p; } } void bst::inordertrav() { inorder(root); } void bst::inorder(tree_node *ptr) { if(ptr!=NULL) { inorder(ptr->left); cout<<" "<<ptr->data<<" "; inorder(ptr->right); } } void bst::postordertrav() { postorder(root); }
  • 43. void bst::postorder(tree_node *ptr) { if(ptr!=NULL) { postorder(ptr->left); postorder(ptr->right); cout<<" "<<ptr->data<<" "; } } void bst::preordertrav() { preorder(root); } void bst::preorder(tree_node *ptr) { if(ptr!=NULL) { cout<<" "<<ptr->data<<" "; preorder(ptr->left); preorder(ptr->right); } } void main() { bst b; b.insert(52); b.insert(25); b.insert(50); b.insert(15); b.insert(40); b.insert(45); b.insert(20); cout<<"inorder"<<endl; b.inordertrav(); cout<<endl<<"postorder"<<endl; b.postordertrav(); cout<<endl<<"preorder"<<endl; b.preordertrav(); getch();}
  • 44. OUTPUT inorder 15 20 25 40 45 50 52 postorder 20 15 45 40 50 25 52 preorder 52 25 15 20 50 40 45
  • 45. EX.NO.14a) MINIMUM SPANNING TREE USING PRIM’S ALGORITHM PROGRAM #include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10],u; main() { int m,c; cout <<"enterno of vertices"; cin >> n; cout <<"ente no of edges"; cin >> m; cout <<"nEDGES Costn"; for(k=1;k<=m;k++) { cin >>i>>j>>c; cost[i][j]=c; }f or(i=1;i<=n;i++) for(j=1;j<=n;j++) if(cost[i][j]==0) cost[i][j]=31999; cout <<"ORDER OF VISITED VERTICES"; k=1; while(k<n) { m=31999; if(k==1) { for(i=1;i<=n;i++) for(j=1;j<=m;j++) if(cost[i][j]<m) { m=cost[i][j]; u=i; }}e lse { for(j=n;j>=1;j--) if(cost[v][j]<m && visited[j]!=1 && visit[j]!=1) { visit[j]=1; stk[top]=j; top++; m=cost[v][j]; u=j; } }c ost[v][u]=31999; v=u;
  • 46. cout<<v << " "; k++; visit[v]=0; visited[v]=1; } } OUTPUT enterno of vertices7 ente no of edges9 EDGES Cost 1 6 10 6 5 25 5 4 22 4 3 12 3 2 16 2 7 14 5 7 24 4 7 18 1 2 28 ORDER OF VISITED VERTICES1 6 5 4 3 2
  • 47. Ex.no.14b) IMPLEMENTATION OF KRUSKAL’S ALGORITHM PROGRAM #include<iostream.h> #include<conio.h> typedef struct edge {i nt node1,node2,wt; }edge; void sortedge(edge a[],int n) {i nt i,j; edge temp; for(i=0;i<n;i++) for(j=i+1;j<n;++j) if(a[i].wt>a[j].wt) {t emp=a[i];a[i]=a[j];a[j]=temp; }}i nt check(int p[],int i,int j) {i nt v1,v2; v1=i; v2=j; while(p[i]>-1) i=p[i]; while(p[j]>-1) j=p[j]; if(i!=j) { p[j]=i; cout<<"v1->v2n",v1,v2; return 1; }r eturn 0; } void main() {e dge e[100]; int r[100],n,i,j,k=1,m,cost=0; clrscr(); cout<<"Kruskal algorithmn"; cout<<"Enter the no of nodes:"; cin>>n; for(i=0;i<n;i++) r[i]=-1; i=0; cout<<"nEnter no of edges:"; cin>>m; for(i=0;i<m;i++) {c out<<"nENter the edge and cost of the edge:"; cin>>e[i].node1; cin>>e[i].node2; cin>>e[i].wt;
  • 48. }s ortedge(e,m); cout<<"nEdges of the MSTn"; i=0; while(k<n) {i f(check(r,e[i].node1,e[i].node2)) { k++; cost=cost+e[i].wt; i++; }}c out<<"Minimum cost:%d",cost; getch(); } OUTPUT Kruskal algorithm Enter the no of nodes:4 Enter no of edges:4 Enter the edge and cost of the edge:1 2 1 Enter the edge and cost of the edge:1 3 3 Enter the edge and cost of the edge:2 3 2 Enter the edge and cost of the edge:2 4 1 Edges of the MST 1->2 2->4 2->3 Minimum cost:4
  • 49. EX.NO.15 IMPLEMENTATION OF DIJKSTRA’S ALGORITHM PROGRAM #include<iostream.h> #include<conio.h> #define INFINITY 1000 int a[10][10],b[10][10]; int i,j,k,n; void input(); void initialize(); void spath(); void display(); void input() {c out<<"nt *** DIJKSTRA'S ALGORITHM ***"; cout<<"n enter the no of vertices:"; cin>>n; for(i=1;i<=n;i++) for(j=1;j<=n;j++){ if(i!=j){ cout<<"cost between "<<i<<j; cin>>a[i][j];}}} void initialize() {f or(i=1;i<=n;i++) a[i][j]=0; for(i=1;i<=n;i++) for(j=1;j<=n;j++){ b[i][j]=a[i][j]; if(!a[i][j] && (i!=j)){ b[i][j]=INFINITY;}}} void spath() {f or(k=1;k<=n;k++) for(i=1;i<=n;i++) for(j=1;j<=n;j++) if((b[i][k] && b[k][j]) && (b[i][k]+b[k][j]<b[i][j])) { b[i][j]=b[i][k]+b[k][j];}} void display(){ i=1; if(i<n) {f or(j=2;j<=n;j++) cout<<"Minimum cost FROM source vertex 1 TO is : "<<j<<b[i][j]; }} void main() {c lrscr(); input(); initialize(); spath(); display(); getch();}
  • 50. OUTPUT *** DIJKSTRA’S ALGORITHM *** enter the no of vertices: 5 cost between1—2: 2 cost between1—3: 1 cost between1—4: 0 cost between1—5: 0 cost between2—1: 2 cost between2—3: 5 cost between2—4: 4 cost between2—5: 0 cost between3—1: 1 cost between3—2: 5 cost between3—4: 3 cost between3—5: 2 cost between4—1: 0 cost between4—2: 4 cost between4—3: 3 cost between4—5: 6 cost between5—1: 0 cost between5—2: 0 cost between5—3: 2 cost between5—4: 6 minimum cost FROM 1 TO 2 is : 2 minimum cost FROM 1 TO 3 is : 1 minimum cost FROM 1 TO 4 is : 4 minimum cost FROM 1 TO 5 is : 3