06csl47 Oops Lab

56
Engineered for Excellence in education MVJ College of MVJ College of Engineering Engineering (Affiliated to Visvesvaraya Technological (Affiliated to Visvesvaraya Technological University) University) (An ISO 9001:2000 Certified Institution) (An ISO 9001:2000 Certified Institution) Channasandra, Near ITPL, Bangalore-560 067. Channasandra, Near ITPL, Bangalore-560 067. DEPARTMENT OF INFORMATION SCIENCE AND ENGINEERING IV SEMESTER BE 06CSL47 –OBJECT ORIENTED PROGRAMMING LAB LABORATORY MANUAL NAME OF THE STUDENT : BRANCH :

Transcript of 06csl47 Oops Lab

Page 1: 06csl47 Oops Lab

Engineered forExcellence in education

MVJ College of EngineeringMVJ College of Engineering(Affiliated to Visvesvaraya Technological University)(Affiliated to Visvesvaraya Technological University)

(An ISO 9001:2000 Certified Institution)(An ISO 9001:2000 Certified Institution)

Channasandra, Near ITPL, Bangalore-560 067.Channasandra, Near ITPL, Bangalore-560 067.

DEPARTMENT OF INFORMATION SCIENCE AND ENGINEERING

IV SEMESTER BE

06CSL47 –OBJECT ORIENTED PROGRAMMING LAB

LABORATORY MANUAL

NAME OF THE STUDENT :

BRANCH :

UNIVERSITY SEAT NO. :

SEMESTER & SECTION :

BATCH :

Page 2: 06csl47 Oops Lab

MVJ COLLEGE OF ENGINEERING

INFORMATION SCIENCE & ENGINEERING

IV SEMESTER BE

06CSL47 –OBJECT ORIENTED PROGRAMMING LAB

LABORATORY MANUAL

Page 3: 06csl47 Oops Lab

CONTENTS

Sl. No.

Name of the ExperimentPage No.

1. Given that an EMPLOYEE class contains following members:

Data members : Employee_Number, Employee_Name, Basic, DA, IT, Net_Sal

Member functions : to read the data, to calculate Net_Sal and to print date members.

Write a C++ program to read the data of N employees and compute Net_Sal of each employee ( DA=52% of Basic and Income Tax(IT)=30% of the gross salary).

06

2. 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.

08

3. 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 integer (real part) and s2 is a complex number.

(b) ADD (s1, s2) - where s1 and s2 are complex numbers.

10

4. 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.

12

5. Write a C++ program to create a template function for Quick sort and demonstrate sorting of integers and doubles. 15

6. 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 <<.

17

Page 4: 06csl47 Oops Lab

Sl. No.

Name of the ExperimentPage No.

7. 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.

19

8. 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;}elsedisplay error

22

9. 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 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

25

10. 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.

27

11. 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.

30

12. 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.

34

Page 5: 06csl47 Oops Lab

Sl. No.

Name of the ExperimentPage No.

13. 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).

37

14. 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.

38

15. 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.

41

Page 6: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

Experiment No: 1

AIM :

Given that an EMPLOYEE class contains following members:-Data members : Employee_Number, Employee_Name, Basic, DA, IT, Net_SalMember functions : to read the data, to calculate Net_Sal and to print date members.

Write a C++ program to read the data of N employees and compute Net_Sal of each employee ( DA=52% of Basic and Income Tax(IT)=30% of the gross salary).

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;}

06CSL47 –Object Oriented Programming Lab Page 6/44

Page 7: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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();}

06CSL47 –Object Oriented Programming Lab Page 7/44

Page 8: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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; }

};

06CSL47 –Object Oriented Programming Lab Page 8/44

Page 9: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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();

cout<<"Student List :-\n"; for(i=0;i<n;i++) { s[i].putdata(); } getch();}

06CSL47 –Object Oriented Programming Lab Page 9/44

Page 10: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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;

public: Complex() {real=img=0;} Complex(double a,double b) { real=a; img=b; } void Putdata() { cout<<real<<" + "<<img<<"i"; }

Complex Add(double a,Complex &c) { Complex z; z.real=c.real+a; z.img=c.img; return z; }

Complex Add(Complex &a,Complex &b) { Complex z; z.real=a.real+b.real; z.img=a.img+b.img; return z; }

};

06CSL47 –Object Oriented Programming Lab Page 10/44

Page 11: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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();}

06CSL47 –Object Oriented Programming Lab Page 11/44

Page 12: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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; };

typedef struct list LIST;

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; } }

06CSL47 –Object Oriented Programming Lab Page 12/44

Page 13: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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........"; }}

06CSL47 –Object Oriented Programming Lab Page 13/44

Page 14: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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..........."; } }}

06CSL47 –Object Oriented Programming Lab Page 14/44

Page 15: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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); }}

06CSL47 –Object Oriented Programming Lab Page 15/44

Page 16: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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.........."; }

}}

06CSL47 –Object Oriented Programming Lab Page 16/44

Page 17: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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 5class 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--]; }

};

06CSL47 –Object Oriented Programming Lab Page 17/44

Page 18: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

ostream& operator<<(ostream &dout,Stack &s){ if(s.top==-1) dout<<"\n\n\tSorry Stack is Empty........."; else { dout<<"\n\nElements of the Stack are: "; for(int i=0;i<=s.top;i++) dout<<"\n\t\t\t\t"<<s.items[i]; dout<<"\n\n\tTotal Number of elements are: "<<s.top+1; } return dout;}

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......."; } }}

06CSL47 –Object Oriented Programming Lab Page 18/44

Page 19: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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;}

06CSL47 –Object Oriented Programming Lab Page 19/44

Page 20: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

int operator-(Date &d1,Date &d2){ int days=0; if(d1.year<d2.year||(d1.year==d2.year&&d1.month<d2.month)|| (d1.year==d2.year&&d1.month==d2.month&&d1.date<d2.date)) return -1;

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;}

ostream& operator<<(ostream &dout,Date &d){ dout<<"\n\t\tDate : "<<d.date; dout<<"\n\t\tMonth: "<<d.month; dout<<"\n\t\tYear : "<<d.year; return dout;}

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);}

06CSL47 –Object Oriented Programming Lab Page 20/44

Page 21: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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();}

06CSL47 –Object Oriented Programming Lab Page 21/44

Page 22: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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;}elsedisplay 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;}

06CSL47 –Object Oriented Programming Lab Page 22/44

Page 23: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

ostream& operator<<(ostream& dout, Matrix& m){ for(int i=0;i<m.row;i++) { for(int j=0;j<m.col;j++) dout<<m.ele[i][j]<<"\t"; dout<<"\n\t\t\t"; } return dout;}

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);

06CSL47 –Object Oriented Programming Lab Page 23/44

Page 24: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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();

}

06CSL47 –Object Oriented Programming Lab Page 24/44

Page 25: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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&);

};

ostream& operator<<(ostream& dout, Octal& o){ dout<<o.x; return dout;}

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;}

06CSL47 –Object Oriented Programming Lab Page 25/44

Page 26: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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();}

06CSL47 –Object Oriented Programming Lab Page 26/44

Page 27: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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>QUEUE <DT>::QUEUE(){

front=rear=-1;}

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;

06CSL47 –Object Oriented Programming Lab Page 27/44

Page 28: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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;

06CSL47 –Object Oriented Programming Lab Page 28/44

Page 29: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

switch(choice){

case 1:q.add();break;case 2:q.del();break;case 3:q.display();break;case 4:exit(0);default: return;

}getch();}

}

06CSL47 –Object Oriented Programming Lab Page 29/44

Page 30: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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; }}

06CSL47 –Object Oriented Programming Lab Page 30/44

Page 31: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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

Page 32: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

cout<<"\n\n\tEnter the position of Insertion: "; cin>>pos; if(pos<1||pos>noe+1)

cout<<"\n\n\tInvalid Position......"; else { DLIST *temp; int i; DLIST *node=new DLIST; node->rt=node->lt=NULL; cout<<"\n\n\tEnter a Number to Insert: "; cin>>node->info; if(pos==1) //insert at front... { node->rt=head; head->lt=node; head=node; } else if(pos==noe+1) //insert at end... { for(temp=head;temp->rt!=NULL;temp=temp->rt); temp->rt=node; node->lt=temp; } else { for(temp=head,i=1;i!=pos;temp=temp->rt,i++); node->rt=temp; node->lt=temp->lt; temp->lt->rt=node; temp->lt=node; } noe++; }

}}

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

Page 33: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

case 3: clrscr(); d.Delete(); break; case 4: clrscr(); d.Display(); break; case 5: exit(0); default: clrscr(); cout<<"\n\n\tEnter proper choice....."; } }}

06CSL47 –Object Oriented Programming Lab Page 33/44

Page 34: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE 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;}

};

class PGS: public Student{

int sem;float stipend,fee;

public:void getPGdata(){

cout<<"\n\t Enter Semester: "; cin>>sem;cout<<"\n\t Enter Stipend: "; cin>>stipend;cout<<"\n\t Enter Fees: "; cin>>fee;

}

06CSL47 –Object Oriented Programming Lab Page 34/44

Page 35: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

int getsem(){

return sem;}

};

class UGS: public Student{

int sem; float stipend,fee;

public: void getUGdata() {

cout<<"\n\t Enter Semester: "; cin>>sem;cout<<"\n\t Enter Stipend: "; cin>>stipend;cout<<"\n\t Enter Fees: "; cin>>fee;

} 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();

}

06CSL47 –Object Oriented Programming Lab Page 35/44

Page 36: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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();}

06CSL47 –Object Oriented Programming Lab Page 36/44

Page 37: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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; }

};

STRING operator+(STRING &s1,STRING& s2){ STRING x(s1); int i,j=0; for(i=0;x.str[i]!='\0';i++); x.str[i]=' '; while((x.str[++i]=s2.str[j++])!='\0'); x.str[i]='\0'; return x;}

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

Page 38: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE 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;

06CSL47 –Object Oriented Programming Lab Page 38/44

Page 39: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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 BIN_TREE::Preorder(Tree *p) { if(p!=NULL) {

cout<<"\t"<<p->info;Preorder(p->left);Preorder(p->right);

} }

void BIN_TREE::Postorder(Tree *p) { if(p!=NULL) {

Postorder(p->left);Postorder(p->right);cout<<"\t"<<p->info;

} }

06CSL47 –Object Oriented Programming Lab Page 39/44

Page 40: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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.....";}

}}

06CSL47 –Object Oriented Programming Lab Page 40/44

Page 41: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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.......";

}

06CSL47 –Object Oriented Programming Lab Page 41/44

Page 42: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

void Push(char c) { s.items[++s.top]=c; } char Pop() { return s.items[s.top--]; } int isoperator(char);

};

int Expression:: isoperator(char c){ if(c=='+'||c=='-'||c=='*'||c=='/'||c=='^') return 1; else return 0;}

int Expression:: Convert(){ int i=0,j=0; char x,y,c='\0',c2; while(infix[i]!='\0') { c2=c; c=infix[i++]; if((isalpha(c)&&isalpha(c2))||(isoperator(c)&&isoperator(c2))) return 0; else { if(isalpha(c))

postfix[j++]=c; else { switch(c)

{ case '+': case '-': case '*': case '/': case '^': case '(': if(s.top==-1) Push(c);

else if(Precedence(c,s.items[s.top])){

postfix[j++]=Pop(); Push(c);

} else Push(c); break;

case ')': if(s.top==-1) return 0;

06CSL47 –Object Oriented Programming Lab Page 42/44

Page 43: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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;}

int Expression:: Precedence(char a,char b){ if(a!='^'&&b=='^')

return 1; else if((a=='*'||a=='/'||a=='+'||a=='-')&&(b=='*'||b=='/'))

return 1; else if((a=='+'||a=='-')&&(b=='+'||b=='-'))

return 1; else return 0;}

06CSL47 –Object Oriented Programming Lab Page 43/44

Page 44: 06csl47 Oops Lab

Information Science & EngineeringInformation Science & Engineering MVJCE MVJCE

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...."; } }}

06CSL47 –Object Oriented Programming Lab Page 44/44