C++ Lab Manual Children’s Education Society (Regd.) THE OXFORD COLLEGE OF ENGINEERING Hosur Road, Bommanahalli, Bangalore Department of Master of Computer Applications A Laboratory Manual For Object Oriented Programming with C++ Laboratory- 10MCA27 2 nd Semester, MCA 2 nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 1 of 56
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
C++ Lab Manual
Children’s Education Society (Regd.)THE OXFORD COLLEGE OF ENGINEERINGHosur Road, Bommanahalli, Bangalore
Department ofMaster of Computer Applications
A Laboratory Manual
For
Object Oriented Programming with C++ Laboratory- 10MCA27
2nd Semester, MCA
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 1 of 48
1. Given that an EMPLOYEE class contains the following members: a. Data Members : Employee_Number, Employee_Name, Basic, DA, IT, Net_Salb. Member Functions : to read data, to calculate Net_Sal and to print data members
2. Write a C++ program to read data on N employees and compute the Net_Sal of each employee (DA = 52% of Basic and Income Tax = 30% of the gross salary)
3. 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 the two better marks for each student. Print the USN, Name and the average marks of all the students.
4. Write a C++ program to create a class called COMPLEX and implement the following overloading functions ADD that return a complex number:
a. ADD(a, s2) – where ‘a’ is an integer (real part) and s2 is a complex numberb. ADD(s1, s2) – where s1 and s2 are complex numbers
5. 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.
6. Write a C++ program to create a template function for Quicksort and demonstrate sorting of integers and doubles.
7. 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 = s1 + element; where s1 is an object of the class STACK 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 full conditions. Also display the contents of the stack after each operation, by overloading the << operator.
8. 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 <<.
a. no_of_days = d1 – d2; where d1 and d2 are DATE objects, and no_of_days is an integerb. d2 = d1 + no_of_days; where d1 is a DATE object and no_of_days is an integer
9. Create a class called MATRIX using 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 + and – operators respectively. Display the results by overloading the operator <<.
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 2 of 48
C++ Lab Manual
If (m1==m2) then m3 = m1+m2 and m4 = m1-m2 else display error.
10. 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 constructor and an overloaded operator +.
a. OCTAL h = x; where x is an integer.b. 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.
11. Write a C++ program to create a class called QUEUE with member functions to add an element and to delete an element from the queue. Using the member functions, implement a queue of integers and double. Demonstrate the operations by displaying the contents of the queue after every operation.
12. Write a C++ program to create a class called DLIST (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.
13. 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.
14. 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 <<.
15. Write a C++ program to create a class called BIN_TREE (Binary Tree) with member functions to perform in-order, preorder and post-order traversals. Create a BIN_TREE object and demonstrate the traversals.
16. Write a C++ program to create a class called EXPRESSION. Using appropriate member functions convert a given valid Infix expression into postfix form. Display the infix and postfix expressions.
Note: In the examination each student picks one question from a lot of all the 16 questions.
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 3 of 48
C++ Lab Manual
1. Given that an EMPLOYEE class contains the following members: a. Data Members : Employee_Number, Employee_Name, Basic, DA, IT, Net_Salb. Member Functions : to read data, to calculate Net_Sal and to print data members
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 4 of 48
C++ Lab Manual
e1.write_data();getch();}
OUTPUTEnter name : ashaenter the ID :111enter Basic : 40000Name : ashaID : 111Basic : 40000DA : 20800Gross : 60800IT : 18240Net_salary : 42560
2. Write a C++ program to read data on N employees and compute the Net_Sal of each employee (DA = 52% of Basic and Income Tax = 30% of the gross salary)
#include<iostream.h>#include<conio.h>#include<stdio.h>class EMPLOYEE{char name[10];int id;int basic;float it,net,gross,da;public:void readdata();void cal_net();void write();};void EMPLOYEE::readdata(){cout<<"enter the name\n";cin>>name;cout<<"enter the id\n";cin>>id;cout<<"enter the basic\n";cin>>basic;}void EMPLOYEE::cal_net(){da=0.52*basic;gross=basic+da;
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 5 of 48
C++ Lab Manual
it=0.30*gross;net=gross-it;}void EMPLOYEE ::write(){printf("\n%s\t %d\t %d\t %1.2f %1.2f %1.2f %1.2f",name,id,basic,da,it,gross,net);}void main(){EMPLOYEE e[10];int n,i;clrscr();cout<<"how many records u want";cin>>n;cout<<"enter the records\n";for(i=0;i<n;i++){e[i].readdata();e[i].cal_net();}cout<<"name\t id\t basic\t da\t it\t gross\t net\n";for(i=0;i<n;i++){e[i].write();}getch();}
Outputhow many records u want 3enter the recordsenter the nameashaenter the id111enter the basic30000enter the namelaxmienter the id222enter the basic10000enter the namefizaenter the id333enter the basic
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 6 of 48
3. 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 the two better marks for each student. Print the USN, Name and the average marks of all the students.
#include<iostream.h>#include<conio.h>class student{private:int usn,m1,m2,m3;char name[20];public:void getdata();void putdata();};void student::getdata(){int i;cout<<"enter the usn number:";cin>>usn;cout<<"enter the name:";cin>>name;cout<<"\n enter the 3 marks:\n";cin>>m1>>m2>>m3;}void student::putdata(){float avg,min;min=m1;if(m2<min)min=m2;if(m3<min)min=m3;avg=(m1+m2+m3-min)/2;cout<<endl<<"usn:"<<usn;cout<<endl<<"name:"<<name;cout<<endl<<"average marks:"<<avg;}main(){
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 7 of 48
C++ Lab Manual
student s[10];int i,n;clrscr();cout<<endl<<"enter number of students n:";cin>>n;for(i=0;i<n;i++){cout<<"enter"<<i+1<<"students data:"<<endl;s[i].getdata();}cout<<endl<<"students information:"<<endl;for(i=0;i<n;i++)s[i].putdata();getch();return 0;} OUTPUT
Enter the no.of students : 2
Enter the 1st student data
Enter the usn number : 101Enter the name : RAJESHEnter the 3 marks :
50 60 50
Enter the 2nd student data
Enter the usn number : 102Enter the name : RAMKUMAREnter the 3 marks :
60 50 60
Calculated students average marks :
Usn : 101Name : RAJESHAverage : 55
Usn : 102Name : RAMKUMARAverage : 60
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 8 of 48
C++ Lab Manual
4. Write a C++ program to create a class called COMPLEX and implement the following overloading functions ADD that return a complex number:
1. ADD(a, s2) – where ‘a’ is an integer (real part) and s2 is a complex number2. ADD(s1, s2) – where s1 and s2 are complex numbers
}//Function to display the complex numbervoid complex::display(){
cout<<real<<"+i"<<imag<<endl;}void main(){
clrscr();
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 9 of 48
C++ Lab Manual
complex s1, s2, s3, s4;int a;cout<<"Enter the first complex value for real, imaginary"<<endl;s1.getdata();cout<<"Enter the second complex value for real, imaginary"<<endl;s2.getdata();cout<<"1st complex no:";s1.display();cout<<"2nd complex no:";s2.display();cout<<"enter a integer value(real part)"<<endl;cin>>a;s3=add(a, s2);cout<<"S3 complex no(a+s2.real)+imaginary:";s3.display();s4=add(s1, s2);cout<<"s4 complex no[s1+s2]:";s4.display();getch();
}
OUTPUT :Enter the first complex value for real, imaginary54Enter the second complex value for real, imaginary431st complex no:5+i42nd complex no:4+i3enter a integer value(real part)2S3 complex no(a+s2.real)+imaginary:6+i3s4 complex no[s1+s2]:9+i7
5. 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.
cout<<"enter array size\n";cin>>n;cout<<"Enter the elements of integer array\n";
for(i=0;i<n;i++)cin>>a[i];
cout<<"Enter the elements of double array\n";for(i=0;i<n;i++)
cin>>b[i];low=0;high=n-1;quicksort(a, low, high);quicksort(b, low, high);cout<<"The sorted list of integer:\n";for(i=0;i<n;i++)
cout<<"\t"<<a[i];cout<<"\nThe sorted list of double:\n";for(i=0;i<n;i++)
cout<<"\t"<<b[i];getch();
}
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 15 of 48
C++ Lab Manual
OUTPUT:QUICK SORT USING FUNCTION TEMPLATE(T0 perform int, double array )Enter array size : 3
Enter the elements of integer array 213
Enter the elements of doubly array2.11.13.1The sorted list of integer array : 1 2 3
The sorted list of double array : 1.1 2.1 3.1
7. Write a C++ program to create a class called STACK using an array of integers. Implement the following operations by overloading the operators ‘+’ and ‘-‘:
c. s1 = s1 + element; where s1 is an object of the class STACK and element is an integer to be pushed on the top of the stack
d. s1 = s1- ; where s1 is an object of the class STACK. ‘-‘ operator pops the element.Handle the STACK empty and full conditions. Also display the contents of the stack after each operation, by overloading the << operator.
#include<iostream.h>#include<conio.h>#include<stdlib.h>//#include<PROCESS.H>#define size 5class stack{ private: int s[5];
ostream & operator <<(ostream & print,stack s1){ if (s1.top == -1) cout<<"\nstack empty"; else { cout<<"\nelements of the stack\n"; for(int i=s1.top;i>=0;i--) { cout<<"\t"; print<<s1.s[i]; cout<<"\n"; } } return print;}
main()
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 17 of 48
C++ Lab Manual
{stack s1;int element,ch= 1;clrscr();
while(1){cout<<"\n 1:push\n 2:pop \n 3:quit";cout<<"\nenter choice ";cin >>ch;switch(ch){ case 1: cout <<"enter element ";
cin>>element; s1 = s1 +element; cout<<s1; break;
case 2: s1 = -s1 ; cout<<s1; break;
default: exit(0); } }// return(0);}
OUTPUT:
STACK USING OPERATOR OVERLOADING
1. PUSH2. POP3. QUIT
Enter your choice: 1Enter element :3
31. PUSH2. POP3. QUIT
Enter your choice: 1Enter element :22 31. PUSH
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 18 of 48
C++ Lab Manual
2. POP3 QUIT
Enter your choice: 1Enter element :11 2 3
1. PUSH2. POP3. QUIT
Enter your choice: 1 Enter element: 11. PUSH2. POP3. QUIT
Stack full.
1. PUSH2. POP3. QUIT
Enter your choice: 2 2 3
1. PUSH2. POP4. QUIT
Enter your choice: 2 3
1. PUSH2. POP3. QUIT
Enter your choice: 2 Stack empty.
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 19 of 48
C++ Lab Manual
8. 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 <<.
c. no_of_days = d1 – d2; where d1 and d2 are DATE objects, and no_of_days is an integerd. d2 = d1 + no_of_days; where d1 is a DATE object and no_of_days is an integer
int dd,mm,yyyy;// int month[13]; long days; date(int d,int n,int y); date getdata(int ,int); date operator +(long n); long operator-(date); friend ostream &operator<<(ostream& ,date);
ostream &operator<<(ostream &ot, date d){ ot<<d.dd<<"/"<<d.mm<<"/"<<d.yyyy; return ot;}
/*void isless(date d1,date d2){ int dy=d1.yyyy-d2.yyyy; long dd=d1.days-d2.days; if(dy<0) { cout<<"Date2 is greater \n"; getch(); exit(0); } if(dy<=0&&dd<0) { cout<<"Date2 is greater\n"; getch(); exit(0); }} */
date date :: getdata(int d,int year){ int feb=((year%4==0&&year%100!=0)||(year%400==0))?29:28; int month[]={0,31,feb,31,30,31,30,31,31,30,31,30,31}; date dt(0,1,year); for(int i=1;i<=d;i++) { dt.dd++; if(dt.dd>month[dt.mm]) { dt.mm++;
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 21 of 48
C++ Lab Manual
dt.dd=1; } } return dt;}
date date ::operator + (long d) { long dys=this->days; int yr=yyyy; for(int i=1;i<=d;i++) { dys++; int t=((yr%4==0&&yr%100!=0)||(yr%400==0))?366:365;
if(dys>t){dys=1;yr++;}
} return getdata(dys,yr); }
void main(){ int dd ,mm,yyyy; clrscr(); cout<<"First Date :\n"; cout<<"Day: ";cin>>dd; cout<<"Month: ";cin>>mm; cout<<"Year: ";cin>>yyyy;
date d1(dd,mm,yyyy); valid(dd,mm,yyyy); cout<<"enter the second date:\n"; cout<<"Day: ";cin>>dd; cout<<"month: ";cin>>mm; cout<<"year: ";cin>>yyyy;
date d2(dd,mm,yyyy); valid(dd,mm,yyyy);// isless(d1,d2); clrscr(); cout<<"\n Date1: "<<d1; cout<<"\n Date2: "<<d2; long no_of_days=d1-d2; cout<<"\nno of days :"<<no_of_days; cout<<"\nEnter the days to be added to date1:";cin>>no_of_days;
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 22 of 48
C++ Lab Manual
d2=d1+no_of_days; cout<<"Result date is : "<<d2; getch();}
9. Create a class called MATRIX using 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 + and – operators respectively. Display the results by overloading the operator <<.
If (m1==m2) then m3 = m1+m2 and m4 = m1-m2 else display error.
} return print;}void main(){ matrix m1,m2,m3,m4; int r,c,ch; clrscr(); cout<<"MATRIX ADDITION AND SUBTRACTION"<<endl; cout<<"**********************************"<<endl; cout<<"ENTER THE ORDER OF THE MATRIX1:"; cin>>r>>c; m1.initialize(r,c); cout<<"ENTER THE ORDER OF THE MATRIX2:"; cin>>r>>c; m2.initialize(r,c); if(m1 == m2) {
cout<<"Matrix addition and subtraction not possible"<<endl;
getch();}
OUTPUT
MATRIX ADDITION AND SUBTRACTIONENTER THE ORDER OF THE MATRIX1 :2 2
ENTER THE ORDER OF THE MATRIX2 :2 1
Matrix addition and subtraction not possible
ENTER THE ORDER OF THE MATRIX1 :2 2
ENTER THE ORDER OF THE MATRIX2 :2 2
Enter M1 matrix elements :1234Enter M2 matrix elements : 1234Matrix Addition
2 46 8
Matrix Subtraction0 0
0 0
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 25 of 48
C++ Lab Manual
10. 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 constructor and an overloaded operator +.
c. OCTAL h = x; where x is an integer.d. 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.
void main() { int x,k,y,oct; //cout<<"enter the octal number"<<endl; //cin>>oct; octal h(8); clrscr(); cout<<"number in base 8 is"<<h<<endl; cout<<"\n ENTER THE DECIMAL NUMBER"; cin>>k; cout<<"\n THE equivalent in octal is:"<<k<<endl;
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 26 of 48
C++ Lab Manual
y=h+k; cout<<" RESULT OF Y=h+k IS "<<y<<"\n"; getch();
}
OUTPUT
Number in base 8 is : 10Enter Decimal Number :5
The equivalent in octal is : 5
Result of y = h + k is : 15
11. Write a C++ program to create a class called QUEUE with member functions to add an element and to delete an element from the queue. Using the member functions, implement a queue of integers and double. Demonstrate the operations by displaying the contents of the queue after every operation.
#include<iostream.h>#include<conio.h>#include<stdlib.h>#define max 5
template <class T>class queue{
T a[max];int front,rear;
public:queue(){
front=-1;rear=-1;
}void insert(T);void delets();void display();
};template <class T>void queue<T>::insert(T f){
if(rear==(max-1)){
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 27 of 48
C++ Lab Manual
cout<<"\n queue is full::";return;
}else{
rear++;a[rear]=f;if(front==-1)
front=0;}
}
template <class T>void queue<T>::delets(){
if(front==-1){
cout<<"\n queue is empty::";return;
}else{
cout<<"\n deleted element is"<<a[front]<<"\n";front++;if(front>rear){
front=-1;rear=-1;
}}
}template <class T>void queue<T>::display(){
if(front==-1){
cout<<"\n queue is empty";return;
}else{
for(int i=front;i<=rear;i++)cout<<a[i]<<"\t";
}}void main(){
clrscr();
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 28 of 48
cout<<"\n Queue using template:-";cout<<"\n==========================";cout<<"\n\t 1.insert int\n\t 2.delete int \n\t 3.inert double\n\t 4.delete double\n\t
5.exit";cout<<"\n==========================";cout<<"\n Enter your choice:-";cin>>choice;cout<<"\n==========================";switch(choice){
case 1:cout<<"\n enter element to insert:-";cin>>item;iq.insert(item);iq.display();break;
case 2:iq.delets();iq.display();break;
case 3:cout<<"\n Enter element to insert:-";cin>>fitem;dq.insert(fitem);dq.display();break;
case 4:dq.delets();dq.display();break;
case 5:exit(0);break;
default:cout<<"\n Enter appropriate choice:-";break;
}getch();
}
OUTPUTQueue using template:-
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 29 of 48
C++ Lab Manual
========================== 1.insert int 2.delete int 3.inert double 4.delete double 5.exit========================== Enter your choice:-1========================== enter element to insert:-11 Queue using template:-========================== 1.insert int 2.delete int 3.inert double 4.delete double 5.exit========================== Enter your choice:-3========================== Enter element to insert:-3.563.56 Queue using template:-========================== 1.insert int 2.delete int 3.inert double 4.delete double 5.exit========================== Enter your choice:-3========================== Enter element to insert:-6.83.56 6.8 Queue using template:-========================== 1.insert int 2.delete int 3.inert double 4.delete double 5.exit========================== Enter your choice:-1enter element to insert:-71 7
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 30 of 48
C++ Lab Manual
Queue using template:-========================== 1.insert int 2.delete int 3.inert double 4.delete double 5.exit========================== Enter your choice:-2========================== deleted element is17 Queue using template:-========================== 1.insert int 2.delete int 3.inert double 4.delete double 5.exit========================== Enter your choice:-4========================== deleted element is3.566.8 Queue using template:-========================== 1.insert int 2.delete int 3.inert double 4.delete double 5.exit========================== Enter your choice:-2========================== deleted element is7 queue is empty
12. Write a C++ program to create a class called DLIST (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.
next=cur->rlink;prev->rlink=next;next->llink=prev;cout<<"\ndeleted element is:"<<cur->data<<endl;delete cur;return;
}else
cout<<"\ninvlid position\n";return;
}void Dlist::display()
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 33 of 48
C++ Lab Manual
{node *temp;if(start==NULL){
cout<<"\nlist is empty\n";return;
}cout<<"\ncontents of the list\n";for(temp=start;temp!=NULL;temp=temp->rlink){
cout<<temp->data<<endl;}
}void main(){Dlist d;int item, pos, ch;clrscr();
while(1){
cout<<"\nDOUBLY LINKED LIST"<<endl;cout<<"*************************"<<endl;cout<<"1.insert at position\n";cout<<"2.delete from position\n";cout<<"3.display\n4.exit\n";cout<<"Enter your choice"<<endl;cin>>ch;switch(ch){
case 1:cout<<"\nEnter the item:";cin>>item;cout<<"Enter the position:";cin>>pos;d.insert_pos(pos, item);break;
case 2:cout<<"\nEnter the position:"<<endl;cin>>pos;d.delete_pos(pos);break;
case 3:d.display();break;
case 4: exit(0);default:
cout<<"Enter correct choice\n";getch();
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 34 of 48
C++ Lab Manual
}}
}
OUTPUT
1.Insert at position2.Delete from position3.Display 4.Exit
Enter your choice :1Enter the item : 5Enter the position: 2Invalid position
1. Insert at position2.Delete from position3.Display 4.Exit
Enter your choice :1Enter the item : 1Enter the position: 1
1. Insert at position2.Delete from position3.Display 4.Exit
Enter your choice :1Enter the item : 2Enter the position: 2
1..Insert at position2.Delete from position3.Display 4.Exit
Enter your choice :2Enter the position: 2The deleted element is : 2
1..Insert at position2.Delete from position3.Display 4.Exit
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 35 of 48
C++ Lab Manual
Enter your choice :2Enter the position: 1The deleted element is : 1
13. 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.
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 38 of 48
C++ Lab Manual
} }}
OUTPUT
1. UG student2. PG student3. Exit
Enter your choice: 1Enter the no.of students : 2Enter name, usn, and age :
Raj1ox07mca1125
Enter fees, semester, and stipend :
1200011000
1. UG student2. PG student3. Exit
Enter name, usn, and age :
ram1ox07mca1224
Enter fees, semester, and stipend :
1200011000
The average age of semester 1 is : 24 . 5
14. 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 <<.
a. STRING s1 = “VTU”b. STRING s2 = “BELGAUM”
c . STRING s3 = s1 + s2 (Use copy constructor)
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 39 of 48
}//overloaded function for operator + to concatenate two stringsstring operator +(string s1,string s2){ string temp(s1); //calling the copy constructor strcat(temp.s, s2.s); return temp;}
/*string string:: operator +(string s2){ string t=strcat(s, s2.s); return t;}*///overloaded operator << function to display the stringostream &operator <<(ostream &print,string x){ print<<x.s<<endl; return print;}
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 40 of 48
First String : VTUSecond String : BELGAUMThe Resulted String is : VTUBELGAUM.
15. Write a C++ program to create a class called BIN_TREE (Binary Tree) with member functions to perform in-order, preorder and post-order traversals. Create a BIN_TREE object and demonstrate the traversals.
} } void tree::call(int ch) { switch(ch) { case 1:
preorder(root); break;
case 2: inorder(root); break;
case 3: postorder(root); break;
} } void main() { clrscr(); tree t; int choice; while(1) { cout<<"\n1.insert\n"; cout<<"2.preorder\n3.inorder\n4.postorder\n"; cout<<"5.exit\n"; cout<<"Enter your choice"; cin>>choice; switch(choice) {
case 1:t.insert();break;
case 2:
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 43 of 48
C++ Lab Manual
cout<<"Preorder traversal"; t.call(1); break;
case 3: cout<<"Inorder traversal"; t.call(2); break; case 4: cout<<"postorder traversal:"; t.call(3); break; case 5: exit(0); default:
cout<<"Enter correct choide\n"; }
} }OUTPUT
1.insert2.preorder3.inorder4.postorder5.exitEnter your choice1Enter the element to be inserted19
1.insert2.preorder3.inorder4.postorder5.exitEnter your choice1Enter the element to be inserted15Current:0x8fdd000015<19
1.insert2.preorder3.inorder4.postorder5.exitEnter your choice1Enter the element to be inserted21Current:0x8fdd000021<19
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 44 of 48
C++ Lab Manual
1.insert2.preorder3.inorder4.postorder5.exitEnter your choice1Enter the element to be inserted40Current:0x8fdd0efeCurrent:0x8fdd000040<21
1.insert2.preorder3.inorder4.postorder5.exitEnter your choice2Preorder traversal19 15 21 401.insert2.preorder3.inorder4.postorder5.exitEnter your choice3Inorder traversal15 19 21 401.insert2.preorder3.inorder4.postorder5.exitEnter your choice4postorder traversal:15 40 21 191.insert2.preorder3.inorder4.postorder5.exitEnter your choice5
16. Write a C++ program to create a class called EXPRESSION. Using appropriate member functions convert a given valid infix expression into postfix form. Display the infix and postfix expressions.PROGRAM/*infix to postfix*/#include<iostream.h>#include<conio.h>#include<string.h>class EXP{
2nd sem ‘C’ section Dept of MCA, TOCE, Bangalore Page 45 of 48
C++ Lab Manual
char infix[50],postfix[50],stack[50]; int top; public: