Laboratory Manual: MVJ College of Engineering
Laboratory Manual: MVJ College of Engineering
IV SEMESTER BE
LABORATORY MANUAL
BRANCH :
BATCH :
MVJ COLLEGE OF ENGINEERING
IV SEMESTER BE
LABORATORY MANUAL
CONTENTS
Sl. Page
Name of the Experiment
No. No.
1. Given that an EMPLOYEE class contains following members:
Experiment No: 1
AIM :
PROGRAM :
#include<iostream.h>
#include<conio.h>
class Employee
{
int emp_num;
float basic,da,tax,net_sal;
char emp_name[25];
public:
void Getdata();
void Putdata()
{
cout<<"\n\t\t\tEmployee Name : "<<emp_name;
cout<<"\n\t\t\tEmployee Num : "<<emp_num;
cout<<"\n\t\t\tEmployee basic : "<<basic;
cout<<"\n\t\t\tEmployee DA : "<<da;
cout<<"\n\t\t\tEmployee Netpay: "<<net_sal;
}
void Findnetsal()
{
net_sal=basic+da-(0.3*basic);
}
};
void Employee::Getdata()
{
cout<<"\n\tEnter Employee Name : ";
cin>>emp_name;
cout<<"\n\tEnter Employee Number: ";
cin>>emp_num;
cout<<"\n\tEnter the Basic Pay : ";
cin>>basic;
cout<<"\n\tEnter the DA : ";
cin>>da;
}
void main()
{
int n;
Employee a[10];
clrscr();
cout<<"\n\n\tEnter how many Employees: ";cin>>n;
for(int i=0;i<n;i++)
{
clrscr();
cout<<"\n\tEnter Employee-"<<i+1<<" Details.....\n";
a[i].Getdata();
a[i].Findnetsal();
}
clrscr();
for(i=0;i<n;i++)
{
cout<<"\nEmployee-"<<i+1<<" Details: ";
a[i].Putdata();
cout<<"\n\t\t****************************************";
}
getch();
}
Experiment No: 2
AIM :
Define a STUDENT class with USN, Name, and Marks in 3 tests of a subject. Declare an
array of 10 STUDENT objects. Using appropriate functions, find the average of two better
marks for each student. Print the USN, Name and the average marks of all students.
PROGRAM :
#include<iostream.h>
#include<conio.h>
class stud
{
public:
int usn,t1,t2,t3;
float best;
char name[20];
void getdata()
{
cout<<"\n\nEnter Student USN ";cin>>usn;
cout<<"\nEnter Student Name: ";cin>>name;
cout<<"\nEnter Student marks\n ";
cout<<"\n\t Test 1: ";cin>>t1;
cout<<"\n\t Test 2: ";cin>>t2;
cout<<"\n\t Test 3: ";cin>>t3;
}
void calc();
void putdata()
{
cout<<"\n\n USN: "<<usn;
cout<<"\n Name: "<<name;
cout<<"\n\t Test 1: "<<t1;
cout<<"\n\t Test 2: "<<t2;
cout<<"\n\t Test 3: "<<t3;
cout<<"\n\n Best of 2 Tests: "<<best;
}
};
void stud::calc()
{
if(t1>t2)
{
if(t2>t3) best=(t1+t2)/2;
else best=(t1+t3)/2;
}
else
{
if(t1>t3) best=(t2+t1)/2;
else best=(t2+t3)/2;
}
}
void main()
{
int n,i=0;
stud s[20];
clrscr();
cout<<"Enter how many Students: ";
cin>>n;
clrscr();
for(i=0;i<n;i++)
{
cout<<"\n Enter the Student "<<i+1<<" marks\n";
s[i].getdata();
s[i].calc();
}
clrscr();
Experiment No: 3
AIM :
Write a C++ program to create a class COMPLEX and implement the following
overloading functions ADD that return a COMPLEX number.
(a) ADD (a,s2) - where a in an interger (real part) and s2 is a complex number.
(b) ADD (s1,s2) - where s1 and s2 are complex numbers
PROGRAM :
#include<iostream.h>
#include<conio.h>
class Complex
{
private:
double real,img;
void main()
{
clrscr();
double real,img;
cout<<"\n\tEnter Real part of Complex Number A : ";cin>>real;
cout<<"\n\tEnter Imginary part of Complex Number A : ";cin>>img;
Complex a(real,img);
cout<<"\n\tEnter Real part of Complex Number B : ";cin>>real;
cout<<"\n\tEnter Imginary part of Complex Number B : ";cin>>img;
Complex b(real,img); Complex c;
c=c.Add(a,b); clrscr();
cout<<"\n\n\t\t A: "; a.Putdata();
cout<<"\n\n\t\t B: "; b.Putdata();
cout<<"\n\t********************************\n\t\tA+B: ";
c.Putdata();
cout<<"\n\tEnter a Integer to be added: "; cin>>real;
c=c.Add(real,a);
cout<<"\n\n\t\t A: "; a.Putdata();
cout<<"\n\n\t\t I: "<<real;
cout<<"\n\t********************************\n\t\tA+B: ";
c=c.Add(real,a); c.Putdata();
getch();
}
Experiment No: 4
AIM :
Write a C++ program to create a class called LIST (linked list) with member functions to
insert an element at the front as well as to delete an element from the front of the list.
Demonstrate all the functions after creating a list object.
PROGRAM :
#include<iostream.h>
#include<conio.h>
#define NULL 0
#include<stdlib.h>
struct list
{
int info;
struct list *next;
};
class List
{
LIST *head;
public:
List() { head=NULL;}
void CreateList();
void InsertFront();
void DeleteFront();
void Display();
};
void List::Display()
{
if(head==NULL)
cout<<"\n\n\tSorry List is Empty...........";
else
{
LIST *temp; int i;
cout<<"\n\n\tElements of the List:\n\t\t\t";
for(temp=head,i=0;temp!=NULL;temp=temp->next,i++)
cout<<"\t"<<temp->info;
cout<<"\n\n\tNumber of Elements in the List are: "<<i;
}
}
void List::DeleteFront()
{
if(head==NULL)
cout<<"\n\n\tSorry List is Empty...........";
else
{
cout<<"\n\n\tElement Deleted is: "<<head->info;
if(head->next==NULL)
{
delete head; head=NULL;
}
else
{
LIST *temp; temp=head;
head=head->next; delete temp;
}
}
}
void List::InsertFront()
{
if(head==NULL)
cout<<"\n\n\tSorry,Create List to Insert front..........";
else
{
LIST *node=new LIST; node->next=NULL;
cout<<"\n\n\tEnter an element to insert at Front of the List: ";
cin>>node->info; node->next=head;
head=node; cout<<"\n\n\tElement Successfully Inserted at Front.......";
}
}
void List::CreateList()
{
if(head!=NULL)
cout<<"\n\n\tSorry List is already Created..........";
else
{
LIST *node=new LIST; node->next=NULL;
cout<<"\n\n\tEnter an element to Create a List: ";
cin>>node->info; head=node;
cout<<"\n\n\tList is Successfully Created........";
}
}
void main()
{
List obj; int choice; clrscr();
while(1)
{
cout<<"\n\n\n\t1.Create List\n\t2.Insert Front\n\t3.Delete Front";
cout<<"\n\t4.Display\n\t5.Exit";
cout<<"\n\n\t\tEnter your Choice: ";
cin>>choice;
switch(choice)
{
case 1: clrscr(); obj.CreateList(); break;
case 2: clrscr(); obj.InsertFront(); break;
case 3: clrscr(); obj.DeleteFront(); break;
case 4: clrscr(); obj.Display(); break;
case 5: exit(0);
default: clrscr(); cout<<"\n\n\tEnter Proper Choice...........";
}
}
}
Experiment No: 5
AIM :
Write a C++ program to create a template function for Quick sort and demonstrate
sorting of integers and doubles.
PROGRAM :
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
template<class T>
void partition(T a[],int lb,int ub,int *p)
{
int down=lb,up=ub;
T t,piot;
piot=a[lb];
while(down<up)
{
while(a[down]<=piot&&down<ub) down++;
while(a[up]>piot) up--;
if(down<up)
t=a[down], a[down]=a[up], a[up]=t;
}
a[lb]=a[up];
a[up]=piot;
*p=up;
}
template<class T>
void quicksort(T a[],int lb,int ub)
{
if(lb>=ub) return;
else
{
int p;
partition(a,lb,ub,&p);
quicksort(a,lb,p-1);
quicksort(a,p+1,ub);
}
}
void main()
{
int a[100];
int n,choice,i;
float d[100];
clrscr();
while(1)
{
cout<<"\n\t\t1.Sort Array of Integers....";
cout<<"\n\t\t2.Sort Array of Doubles.....";
cout<<"\n\t\t3.Exit......";
cout<<"\n\n\tEnter Your choice: "; cin>>choice;
switch(choice)
{
case 1 : clrscr();
cout<<"\n\tEnter how many numbers: "; cin>>n;
cout<<"\n\n\tEnter "<<n<<" Integer values: ";
for(i=0;i<n;i++) cin>>a[i];
quicksort(a,0,n-1);
cout<<"\n\n\tSorted Array:\n\n\t\t ";
for(i=0;i<n;i++) cout<<a[i]<<"\n\t\t";
break;
case 2 : clrscr();
cout<<"\n\tEnter how many numbers: "; cin>>n;
cout<<"\n\n\tEnter "<<n<<" Double values: ";
for(i=0;i<n;i++) cin>>d[i];
quicksort(d,0,n-1);
cout<<"\n\n\tSorted Array:\n\n\t\t ";
for(i=0;i<n;i++) cout<<d[i]<<"\n\t\t";
break;
case 3 : exit(0);
default: cout<<"\n\n\tEnter proper choice..........";
}
}
}
Experiment No: 6
AIM :
Write a C++ program to create a class called STACK using an array of integers.
Implement the following operations by overloading the operators + and - .
(a) s1=s2 + element; where s1 is an object of the class STACK empty and element is an
integer to be pushed on the top of the stack.
(b) s1=s1-; where s1 is an object of the class STACK.- operator pops the element.
Handle the STACK empty and STACK full conditions. Also display the contents of the stack
after each operation, by overloading the operator <<.
PROGRAM :
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define SIZE 5
class Stack
{
int items[SIZE];
public: int top;
Stack() { top=-1; }
friend ostream & operator<< (ostream &,Stack &);
friend Stack operator+(Stack &s,int a)
{
s.items[++s.top]=a;
return s;
}
void operator-()
{
if(top==-1)
cout<<"\n\n\tSorry Stack is Empty.........";
else
cout<<"\n\n\tElement Deleted is: "<<items[top--];
}
};
void main()
{
Stack s;
int n,choice;
clrscr();
while(1)
{
cout<<"\n\n\t\t1.Push\n\t\t2.Pop\n\t\t3.Display\n\t\t4.Exit";
cout<<"\n\n\tEnter your choice: "; cin>>choice;
switch(choice)
{
case 1 : clrscr();
if(s.top==SIZE-1) cout<<"Sorry Stack Overflow......";
else
{
cout<<"\n\n\tEnter a number to Push: "; cin>>n;
s=s+n;
}
break;
case 2 : clrscr(); -s;
break;
case 3 : clrscr(); cout<<s;
break;
case 4 : exit(0);
default: clrscr(); cout<<"\n\n\tEnter proper choice.......";
}
}
}
Experiment No: 7
AIM :
Write a C++ program to create a class called DATE. Accept two valid dates in the form
dd/mm/yy. Implement the following operations by overloading the operators + and -. After
every operation display the results by overloading the operator <<.
i. no_of_days = d1-d2;where d1 and d2 are DATE objects,d1>=d2 and
no_of_days is an integer.
ii. d2=d1+no_of_days;where d1 is a DATE object and no_of_days is an integer.
PROGRAM :
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
int daysinmonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
class Date
{
char str[12];
int date,month,year;
public: Date(char *);
friend ostream& operator<<(ostream &,Date &d);
friend int operator-(Date &,Date &);
Date operator+(int);
};
Date Date::operator+(int x)
{
while(x)
{
if((year%4==0&&year%100!=0)||(year%400==0)) daysinmonth[1]=29;
else daysinmonth[1]=28;
x--; date++;
if(date>daysinmonth[month-1])
{ date=1; month++; }
if(month>12)
{ year++; month=1; }
}
return *this;
}
while(d1.year!=d2.year||d1.month!=d2.month||d1.date!=d2.date)
{
if((d2.year%4==0&&d2.year%100!=0)||(d2.year%400==0)) daysinmonth[1]=29;
else daysinmonth[1]=28;
days++; d2.date++;
if(d2.date>daysinmonth[d2.month-1])
{ d2.date=1; d2.month++; }
if(d2.month>12)
{ d2.month=1; d2.year++; }
}
return days;
}
Date::Date(char c[12])
{
char a[5];int i,j;
for(i=0,j=0;c[i]!='\0';str[i++]=c[j++]); str[i]='\0';
for(i=0,j=0;str[i]!='/';a[j++]=str[i++]); a[j]='\0';
date=atoi(a); i++;
for(j=0;str[i]!='/';a[j++]=str[i++]); a[j]='\0';
month=atoi(a); i++;
for(j=0;str[i]!='\0';a[j++]=str[i++]); a[j]='\0';
year=atoi(a);
}
void main()
{
clrscr();
char a[12]; int n;
cout<<"\n\tEnter First Date (dd/mm/yy): "; cin>>a; Date d1(a);
cout<<"\n\tEnter Second Date (dd/mm/yy): "; cin>>a; Date d2(a);
cout<<"\n\n\tDate1:"<<d1; cout<<"\n\n\tDate2:"<<d2;
n=d1-d2;
if(n==-1) cout<<"\n\n\tSecond Date is Bigger than First.....";
else cout<<"\n\n\tNumber of days between Date1 and Date2 is: "<<n;
cout<<"\n\n\tEnter Number of Days to be added: ";cin>>n;
d1=d1+n;
cout<<d1;
getch();
}
Experiment No: 8
AIM :
Write a C++ program to create called MATRIX using a two dimensional array of
integers. Implement the following operations by overloading the operator == which checks the
compatibility of two matrices to be added and subtracted. Perform the addition and subtraction
by overloading the operators + and - respectively. Display the results by overloading the
operator<<.
if (m1==m2)
{
m3=m1+m2;
m4=m1-m2;
}
else
display error
PROGRAM :
#include<iostream.h>
#include<conio.h>
#include<math.h>
class Matrix
{
int row,col,ele[10][10];
public:
Matrix() { row=0,col=0; }
Matrix(int a,int b) { row=a; col=b; }
int operator==(Matrix& m);
Matrix operator+(Matrix &);
Matrix operator-(Matrix &);
friend ostream& operator<<(ostream& dout,Matrix& m);
};
int Matrix::operator==(Matrix& m)
{
if(row==m.row&&col==m.col)
{
cout<<"\n\tEnter "<<row*col<<" elements of Matrix1: ";
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
cin>>ele[i][j];
cout<<"\n\tEnter "<<m.row*m.col<<" elements of Matrix2: ";
for(i=0;i<m.row;i++)
for(j=0;j<m.col;j++)
cin>>m.ele[i][j];
return 1;
}
else return 0;
}
Matrix Matrix::operator-(Matrix& m)
{
Matrix r;
r.row=row; r.col=col;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
r.ele[i][j]=ele[i][j]-m.ele[i][j];
return r;
}
Matrix Matrix::operator+(Matrix& m)
{
Matrix r;
r.row=row; r.col=col;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
r.ele[i][j]=ele[i][j]+m.ele[i][j];
return r;
}
void main()
{
clrscr();
int r,c;
cout<<"\n\t\tEnter number of Rows of Matrix1 : "; cin>>r;
cout<<"\n\t\tEnter number of Columns of Matrix1: "; cin>>c;
Matrix m1(r,c);
cout<<"\n\t\tEnter number of Rows of Matrix2 : "; cin>>r;
cout<<"\n\t\tEnter number of Columns of Matrix2: "; cin>>c;
Matrix m2(r,c);
if(m1==m2)
{
clrscr();
Matrix add=m1+m2;
Matrix sub=m1-m2;
cout<<"\n\tMatrix m1:\n\t\t\t"<<m1;
cout<<"\n\tMatrix m2:\n\t\t\t"<<m2;
cout<<"\n\tMatrix m1+m2:\n\t\t\t"<<add;
cout<<"\n\tMatrix m1-m2:\n\t\t\t"<<sub;
}
else
cout<<"\n\tSorry Neither Addition Nor Subtraction\n\t\t "
<<"can be done with these matrices......";
getch();
}
Experiment No: 9
AIM :
Write a C++ program to create a class called OCTAL which has the characteristics of an
octal number. Implement the following operations by writing an appropriate constructer and
overloaded operator +.
i. OCTAL h=x; where x is an integer.
ii. int y=h+k; where h is an OCTAL object and k is an integer.
Display the OCTAL result by overloading the operator <<. Also display the values of h and y
PROGRAM :
#include<iostream.h>
#include<conio.h>
#include<math.h>
class Octal
{
int x;
public:
Octal() { x=0; }
Octal(int n);
int operator+(int n);
friend ostream& operator<<(ostream&,Octal&);
};
int Octal::operator+(int n)
{
int a=x;
int d=0,i=0;
while(a/10!=0)
{
d=d+pow(8,i)*(a%10);
a=a/10; i++;
}
d=d+pow(8,i)*a;
int sum=n+d;
return sum;
}
Octal::Octal(int n)
{
int o=0,i=0;
while(n>=8)
{
o=o+pow(10,i)*(n%8);
n=n/8;i++;
}
o=o+pow(10,i)*n;
x=o;
}
void main()
{
int n;
clrscr();
cout<<"\n\n\tEnter a Decimal Number to convert "
<<"\n\tinto its equivalent Octal Number: ";
cin>>n;
Octal o(n);
cout<<"\n\n\t Octal Equivalent of Decimal "<<n<<" is: "<<o;
cout<<"\n\n\tEnter a Decimal Number to Add: "; cin>>n;
int x=o+n;
cout<<"\n\n\tSum of Decimal "<<n<<" and Octal "<<o<<" is: Decimal "<<x;
getch();
}
Experiment No: 10
AIM :
Write a C++ program to create a class called QUEUE with member functions to add an
element and to delete an element from queue. Using these member functions, implement a queue
of integer and double. Demonstrate the operations by displaying the content of the queue after
every operation.
PROGRAM :
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 3
template<class DT>
class QUEUE
{
private:
DT a[10],ele;
int front,rear;
public:
QUEUE();
void add();
void del();
void display();
};
template<class DT>
void QUEUE<DT>::add()
{
if(rear===MAX_SIZE-1)
{
cout<<"\n queue overflow\n";
return;
}
cout<<"\n enter element to be inserted:";
cin>>ele;
rear=rear+1;
a[rear]=ele;
if(front==-1)
front=0;
}
template<class DT>
void QUEUE<DT>::del()
{
if(front==-1)
{
cout<<"\n queue underflow.....";
return;
}
cout<<"\n\n element "<<a[front]<<"is deleted from queue";
if(front==rear)
front=rear=-1;
else
front=front+1;
}
template<class DT>
void QUEUE<DT>::display()
{
int i;
{
cout<<"\n contents of queue are:\n";
if(front==-1)
{
cout<<"\n queue empty...\n";
return;
}
for(i=0;i<=rear;i++)
cout<<a[i]<<"\n";
}
}
void main()
{
QUEUE <double> q;
int choice;clrscr();
while(1)
{
cout<<"\n\n 1.insert \n 2.del \n 3.display \n 4.exit\n";
cout<<"\n enter your choice:\n";
cin>>choice;
switch(choice)
{
case 1:q.add();break;
case 2:q.del();break;
case 3:q.display();break;
case 4:exit(0);
default: return;
}
getch();
}
}
Experiment No: 11
AIM :
Write a C++ program to create a class called DLST(Doubly Linked List) with member
functions to insert a node at a specified position and delete a node from a specified position of
the list. Demonstrate the operations by displaying the content of the list after every operation.
PROGRAM :
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
struct dlist
{
int info;
struct dlist *rt;
struct dlist *lt;
}typedef DLIST;
class Dlist
{
DLIST *head;
int noe;
public:
Dlist() { head=NULL; noe=0; }
void Insert(); void Create();
void Delete(); void Display();
};
void Dlist::Display()
{
if(head==NULL) cout<<"\n\n\tList is Empty.........";
else
{
cout<<"\n\nElements of the List are:\n\t\t\t";
for(DLIST *temp=head;temp!=NULL;temp=temp->rt)
cout<<"\t"<<temp->info;
cout<<"\n\n\tnumber of Elements are: "<<noe;
}
}
void Dlist::Delete()
{
if(head==NULL)
cout<<"\n\n\tList is Empty.........";
else
{
int pos;
cout<<"\n\n\tEnter the position of Deletion: "; cin>>pos;
if(pos<1||pos>noe) cout<<"\n\n\tInvalid Position......";
else
{
DLIST *temp; int i;
if(pos==1) //delete at front...
{
if(head->rt==NULL)
{
cout<<"\n\n\tElement Deleted is: "<<head->info; head=NULL;
}
else
{
cout<<"\n\n\tElement Deleted is: "<<head->info;
head=head->rt; head->lt=NULL; delete temp;
}
}
else if(pos==noe) //delete at end...
{
for(temp=head;temp->rt!=NULL;temp=temp->rt);
cout<<"\n\n\tElement Deleted is: "<<temp->info;
temp->lt->rt=NULL; delete temp;
}
else
{
for(temp=head,i=1;i!=pos;temp=temp->rt,i++);
cout<<"\n\n\tElement Deleted is: "<<temp->info;
temp->lt->rt=temp->rt;
temp->rt->lt=temp->lt;
delete temp;
}
noe--;
}
}
}
void Dlist::Insert()
{
if(head==NULL)
cout<<"\n\n\tCreate a list before Insertion.........";
else
{
int pos;
06CSL47 –Object Oriented Programming Lab Page 31/44
Information Science & Engineering MVJCE
void Dlist::Create()
{
if(head==NULL)
{
DLIST *node=new DLIST; node->rt=node->lt=NULL;
cout<<"\n\n\tEnter a number to Create List: "; cin>>node->info;
head=node; noe++;
}
else cout<<"\n\n\tList is already exist........";
}
void main()
{
int choice; Dlist d; clrscr();
while(1)
{
cout<<"\n\n\n\t1. Create\n\t2. Insert\n\t3. Delete\n\t4. Display"
<<"\n\t5. Exit\n\n\tEnter Your Choice: "; cin>>choice;
switch(choice)
{
case 1: clrscr(); d.Create(); break;
case 2: clrscr(); d.Insert(); break;
06CSL47 –Object Oriented Programming Lab Page 32/44
Information Science & Engineering MVJCE
Experiment No: 12
AIM :
Write a C++ program to create a class called STUDENT with data members USN, Name
and Age. Using inheritance, create the classes UGSTUDENT and PGSTUDENT having fields
as semester, Fees and Stipend. Enter the data for at least 5 students. Find the semester wise
average age for all UG and PG students separately.
PROGRAM :
#include<iostream.h>
#include<conio.h>
class Student
{
int age;
char usn[10],name[25];
public:
void getdata()
{
cout<<"\n\n\tEnter Name : "; cin>>name;
cout<<"\n\tEnter USN: "; cin>>usn;
cout<<"\n\t Enter Age : "; cin>>age;
}
int getage()
{
return age;
}
};
int getsem()
{
return sem;
}
};
void main()
{
UGS a[10]; PGS b[10];
float avg[8];
int i,j,npg,nug;
clrscr();
cout<<"\n\n\tEnter number of UG Students: "; cin>>nug;
for(i=0;i<nug;i++)
{
clrscr();
cout<<"\n\nEnter details of UG Student "<<(i+1)<<": ";
a[i].getdata();
a[i].getUGdata();
}
cout<<"\n\n\tEnter number of PG Students: "; cin>>npg;
for(i=0;i<npg;i++)
{
clrscr();
cout<<"\n\nEnter details of PG Student "<<(i+1)<<": ";
b[i].getdata();
b[i].getPGdata();
}
for(i=0;i<8;i++)
avg[i]=0;
clrscr();
cout<<"\n\nAverage ages of UG Students:";
for(i=0;i<4;i++)
{
int k=0;
for(j=0;j<nug;j++)
{
if(a[j].getsem()==i+1)
{
avg[i]=avg[i]+a[j].getage();
k++;
}
}
if(k!=0)
{
avg[i]=avg[i]/k;
cout<<"\n\n\t\t\tAverage Age of Sem "<<(i+1)<<" Students: "<<avg[i];
}
}
for(i=0;i<8;i++)
avg[i]=0;
cout<<"\n\nAverage ages of PG Students:";
for(i=0;i<8;i++)
{
int k=0;
for(j=0;j<npg;j++)
{
if(b[j].getsem()==i+1)
{
avg[i]=avg[i]+b[j].getage();
k++;
}
}
if(k!=0)
{
avg[i]=avg[i]/k;
cout<<"\n\n\t\t\tAverage Age of Sem "<<(i+1)<<" Students: "<<avg[i];
}
}
getch();
}
Experiment No: 13
AIM :
Write a C++ program to create a class called STRING and implement the following
operations. Display the results after every operation by overloading the operator <<.
i. STRING s1= "VTU"
ii. STRING s2= "BELGAUM"
iii. STRING s3= s1+s2; (Use Copy constructor).
PROGRAM :
#include<iostream.h>
#include<conio.h>
class STRING
{
private:
char *str;
public:
STRING(char *p) { str=p; }
STRING(STRING& s) { str=s.str; }
friend STRING operator+(STRING &s1,STRING& s2);
friend ostream& operator<<(ostream& dout,STRING& s)
{
dout<<s.str;
return dout;
}
};
void main()
{
clrscr();
cout<<"\n\n\t\t";
STRING s1="VTU"; cout<<" String s1: "<<s1<<"\n\n\t\t";
STRING s2="BELGAUM"; cout<<" String s2: "<<s2<<"\n\n\t\t";
STRING s3=s1+s2; cout<<" S1+S2 is : "<<s3<<"\n\n\t\t";
getch();
}
06CSL47 –Object Oriented Programming Lab Page 37/44
Information Science & Engineering MVJCE
Experiment No: 14
AIM :
Write a C++ program to create a class called BIN_TREE(Binary tree) with member
functions to perform inorder, preorder and postorder traversals. Create a BIN_TREE object and
demonstrate the traversals.
PROGRAM :
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct Tree
{
int info;
Tree *left,*right;
};
class BIN_TREE
{
public:
Tree *head;
BIN_TREE()
{
head=NULL;
}
void Insert();
void Inorder(Tree*);
void Preorder(Tree*);
void Postorder(Tree*);
};
void BIN_TREE::Insert()
{
Tree *newnode=new Tree;
newnode->left=newnode->right=NULL;
cout<<"\n\n\tEnter a number to be Inserted: ";
cin>>newnode->info;
if(head==NULL)
head=newnode;
else
{
Tree *temp,*p;
temp=head;
while(temp!=NULL)
{
p=temp;
if(newnode->info>temp->info)
temp=temp->right;
else
temp=temp->left;
}
if(newnode->info>p->info)
p->right=newnode;
else
p->left=newnode;
}
}
void BIN_TREE::Inorder(Tree*p)
{
if(p!=NULL)
{
Inorder(p->left);
cout<<"\t"<<p->info;
Inorder(p->right);
}
}
void main()
{
BIN_TREE obj;
clrscr();
int choice;
while(1)
{
cout<<"\n\n\n\t1.Insert\t2.Display\t3.exit\n";
cout<<"\n\n\tEnter Your choice: "; cin>>choice;
switch(choice)
{
case 1: clrscr();
obj.Insert();
break;
case 2: clrscr();
if(obj.head==NULL)
cout<<"\n\n\tSorry no nodes in BIN_TREE....";
else
{
cout<<"\n\nInorder Traversal : ";
obj.Inorder(obj.head);
cout<<"\n\nPreorder Traversal : ";
obj.Preorder(obj.head);
cout<<"\n\nPostorder Traversal: ";
obj.Postorder(obj.head);
}
break;
case 3: exit(0);
default: clrscr();
cout<<"\n\n\tEnter Proper choice.....";
}
}
}
Experiment No: 15
AIM :
Write a C++ program to create a class called EXPRESSION. Using appropriate member
functions convert a given valid Infix expression into prefix form. Display the Infix and Postfix
expressions.
PROGRAM :
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
struct Stack
{
char items[25];
int top;
};
class Expression
{
char infix[50],postfix[50];
Stack s;
public:
Expression() { s.top=-1; }
int Convert();
int Precedence(char,char);
void GetInfix()
{
cout<<"\n\n\n\tEnter valid Infix Expression: ";
cin>>infix;
}
void Display()
{
clrscr();
int flag;
clrscr();
flag=Convert();
if(flag==1)
{
cout<<"\n\n\tThe Infix Expression: "<<infix;
cout<<"\n\n\tThe Postfix Expression is: "<<postfix;
}
else cout<<"\n\n\n\tEnter Valid Infix Expression.......";
}
};
else
{
x=Pop();
while(x!='(')
{
postfix[j++]=x;
if(s.top==-1) return 0;
else x=Pop();
}
}
break;
default: cout<<"\n\n\tInvalid Character: "<<c;
return 0;
}// Switch ends...
}// else ends...
}
}
while(s.top!=-1)
{
char y=Pop();
if(y=='(') return 0;
else postfix[j++]=y;
}
postfix[j]='\0';
return 1;
}
void main()
{
clrscr();
Expression Exp;
int choice;
while(1)
{
cout<<"\n\n\t1. Infix to Postfix Conversion..."
<<"\n\n\t2. Exit";
cout<<"\n\n\n\tEnter Your Choice: ";
cin>>choice;
switch(choice)
{
case 1: clrscr();
Exp.GetInfix();
Exp.Display();
break;
case 2: exit(0);
default: clrscr();
cout<<"\n\n\tEnter Proper Choice....";
}
}
}