Object Oriented Programming - Lab Manual

Add to Favourites
Post to:

Infant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 1 Ex.No.1 Matrix-Vector Multiplication using friend function Aim: To write C++ program to define matrix and vector class, to use function with default argument and to do matrix-vector multiplication using friend function. Algorithm: 1. Declare vector Class 2. Define matrix Class 3. Declare friend function multiply() inside the matrix class 4. Define vector Class 5. Declare friend function multiply() inside the vector class 6. Define getvector() function with for loop to get the elements for vector 7. Define disvector() function with for loop to display the contents of vector 8. Define getmatrix() function with nested for loops to get the matrix elements 9. Define dismatrix() function with nested for loops to display the matrix 10. Define the multiply() to multiply matrix and vector a. No of columns in the matrix should be equal to no. of elements in the vector b. Apply the matrix-vector multiplication mechanism: þ ý ü î í ì++ = þ ý ü î í ì þ ý ü î í ì) * ( ) * ( ) * ( ) * ( y d x c y b x a yx d c b a c. Display the resultant matrix in the screen 11. Define main() to create objects and to call the defined functions. Program: #include #include class vector; //class declaration – needed, because this class referred before it is defined in the friend function. class matrix { int m[3][3]; public: void getmatrix(void); void dismatrix(void); friend void multiply(matrix &, vector &); };class vector { int v[10]; public: //default argument is 3 void getvector(int n=3); void disvector(void); friend void multiply(matrix &, vector &); }; void vector::getvector(int n) { int i; cout<<"\nEnter elements for vector one by one...\n"; for(i=0;i>v[i]; }void vector::disvector()Infant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 2 { int i; cout<<"\nThe vector elements are...\n"; for(i=0;i<3;i++) cout<>m[i][j]; }void matrix::dismatrix() { int i, j; cout<<"\nEntered matrix is...\n"; for(i=0;i<3;i++) { for(j=0;j<3;j++) cout<> and << operators 5. Declare other operator overloading functions 6. Define all the functions. a. Addition: (a+bi) + (x + yi) = ((a+x)+(b+y)i) b. Subtraction: (a+bi) -(x + yi) = ((a-x)+(b-y)i) c. Multiplication: (a+bi) * (x + yi) = (((a*x)-(b*y)) + ((a*y) + (x*b))i) d. Division : i. d=(x*x) + (y*y) ii. (a+bi) /(x + yi) = (((a*x)+(b*y))/d) + (((b*x)-(a*y))/d)i 7. Create objects for complex class in main() function. 8. The arithmetic operators will invoke the overloaded operator automatically and returns the result 9. Display the result using overloaded operators. Program: #include #include #include class complex { private:float real; float imag; public: complex() { real=imag=0.0; }complex(int r, int i) { real = r; imag = i; }complex(double r, double i) { real = r; imag = i; }friend istream& operator>>(istream &, complex &);Infant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 4 friend ostream& operator<<(ostream &, complex &); complex operator+(complex); complex operator-(complex); complex operator*(complex); complex operator/(complex); friend double condou(complex t); };double condou(complex t) { return t.real; }istream& operator >>(istream &in, complex &c) { cout<<"\nReal Part:"; in>>c.real; cout<<"Imag Part:"; in>>c.imag; return in; }ostream& operator<<(ostream &out, complex &c) { if (c.imag<0) out<>c1; cout<<"\nEnter complex number 2: "; cin>>c2; cout<<"\nEnter complex numbers are:"; cout<<"\nComplex 1: "<complex:"<complex:"< #include class matrix { int **m; int row, col; public: matrix() { row=col=0; m=NULL; }matrix(int r ,int c); ~matrix(); void getmatrix(); void showmatrix(); matrix(matrix &m2); //copy constructor matrix& operator=(matrix &m2); };matrix::~matrix() { for(int i=0;i>m[i][j]; }void matrix::showmatrix() { for(int i=0;i>r>>c; matrix m1(r,c); cout<<"\nEnter the matrix elements one by one..."; m1.getmatrix(); cout<<"\nEntered matrix is...\n"; m1.showmatrix(); //invoking copy constructor matrix m2=m1; cout<<"\nResult of copy constructor is...\n"; m2.showmatrix(); matrix m3; m3=m1; cout<<"\nResult of assignment operator overloading...\n"; m3.showmatrix();Infant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 8 getch(); }Infant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 9 Ex.No.4 Overloading new and delete operator Aim: To write C++ program to overload new and delete operators to provide custom dynamic memory allocation. Algorithm: 1. Define vector Class. 2. Define new overloaded method a. Accepts the size as input b. Memory is created using malloc() c. If there is no memory available, execution will be stopped. d. Address is returned. 3. Define delete overloaded method a. Object is removed from the memory using free() 4. Declare the functions read(), max() and sum() 5. Define all the functions. 6. Create objects for vector class in main() function. 7. The memory for the objects is dynamically allocated. 8. Invoke the functions dynamically. 9. Display the results. 10. Invoke the delete operator. Program : /*Overloading new and delete operator using malloc and free*/#include #include #include class vector { private:int *array; public: void *operator new(size_t size) { void *v; cout<<"\nOperator new invoked..."; v=malloc(size); if(!v) { cout<<"Unable to allocate memory"; exit(0); }return v; }void operator delete(void* v) { cout<<"\nOperator delete invoked..."; free (v); }void read(int); int max(int); int sum(int); };Infant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 10 void vector::read(int s) { for(int i=0; i>array[i]; } }int vector::sum(int s) { int tot=0; for(int i=0; imax) max = array[i]; return max; }void main() { int s; clrscr(); cout<<"\nEnter how many elements..."; cin >> s; vector *vec = new vector; cout<<"Enter vector data...\n"; vec->read(s); cout<<"\nThe max is..."<max(s); cout<<"\nThe sum is..."<sum(s); delete vec; getch(); }Infant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 11 Ex.No.5 Template for Linked List Class and its Methods Aim: To write C++ program to develop a template for linked list class and its methods. Algorithm: 1. Define struct node 2. Define list class with declaration of constructor and functions. 3. Define all the constructor and functions. 4. Length function will return the size of the list. 5. Makeempty() function will delete all the items in the list. 6. Insert() function will insert the item at the first position of the list. 7. Remove() function will remove item in the list which is entered by the user. 8. Create list object of list class in main() function. 9. Get the choice from the user 10. Invoke the functions appropriately. 11. Display the results. Program : #include #include #include template struct node { type data; node* next; }; template class list { public: list(); int length(void) const; void makeempty(void); void insert(void); void remove(void); void display(void); private:node* linklist; int count; }; template void list::display(void) { node* cur = linklist; cout<<"\nThe linked list is...\n"; while(cur!=NULL) { cout<data<<"->"; cur=cur->next; }cout<<"NULL\n"; }Infant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 12 template list::list() { count=0; linklist=NULL; }template int list::length(void) const { return count; }template void list::makeempty(void) { node* temp; while(linklist !=NULL) { temp=linklist; linklist=linklist->next; delete temp; }count=0; cout<<"\nNow List is empty..."; }template void list::insert(void) { node* temp; type item; cout<<"\nEnter the item to insert..."; cin>>item; temp=new node; temp->data = item; temp->next = linklist; linklist=temp; count++; }template void list::remove(void) { node* cur = linklist; type item; cout<<"\nEnter the item to remove..."; cin>>item; node* temp; if(item==linklist->data) { temp = cur; linklist=linklist->next; }else {Infant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 13 while(!(item==(cur->next->data))) cur=cur->next; temp = cur->next; cur->next = (cur->next)->next; }delete temp; count--; }void main() { int ch; list list1; clrscr(); while(1) { cout<<"\n Single Linked List -Menu\n"; cout<<"\n 1.Insert \n2.Delete\n 3.Empty\n 4.Exit\n"; cout<<"\nEnter your Choice... "; cin>>ch; switch(ch) { case 1: list1.insert(); list1.display(); break; case 2: if(list1.length()>0) { list1.remove(); list1.display(); }else cout<<"\nList Empty"; break; case 3: list1.makeempty(); break; case 4: exit(0); default:cout<<"\nInvalid Choice\n"; } } }Infant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 14 Ex.No.6 Sorting Algorithms Using Templates (6a) Merge sort using templates Aim: To write a program to do merge sort using templates. Algorithm: 1. Get the array length. 2. Get the array elements. 3. If the list is of length 0 or 1, then it is already sorted. Otherwise: 4. Divide the unsorted list into two sublists of about half the size. 5. Sort each sublist recursively by re-applying merge sort. 6. Merge the two sublists back into one sorted list. 7. Display the sorted list. Program: #include #include #include template class sort { t a[10]; public: void get(int); void merge(int,int); void mergesort(int,int,int); void display(int); }; template void sort ::get(int n) { int i; cout<<"\n\n Enter the array elements:"; for(i=1;i<=n;i++) cin>>a[i]; }template void sort ::display(int n) { int i; cout<<"\n The sorted array is\n"; for(i=1;i<=n;i++) cout< void sort ::merge(int low,int high) { int mid; if(low void sort::mergesort(int low,int mid,int high) { t b[10]; int h,i,j,k; h=low; i=low; j=mid+1; while((h<=mid)&&(j<=high)) { if(a[h]<=a[j]) { b[i]=a[h]; h=h+1; }else { b[i]=a[j]; j=j+1; }i=i+1; }if(h>mid) { for(k=j;k<=high;k++) { b[i]=a[k]; i=i+1; } }else { for(k=h;k<=mid;k++) { b[i]=a[k]; i=i+1; } }for(k=low;k<=high;k++) a[k]=b[k]; }void main() { int n; clrscr(); cout<<"\n\t\t MERGE SORT USING TEMPLATES"; cout<<"\n\t\t ~~~~~ ~~~~ ~~~~~~~~~~~"; sortn1;Infant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 16 sortn2; cout<<"\n Enter the array size:"; cin>>n; n1.get(n); n1.merge(1,n); n1.display(n); n2.get(n); n2.merge(1,n); n2.display(n); getch(); } (6b) Quick sort using templates Aim: To write a C++ program to do quick sort using templages. Algorithm: 1. Get the array length. 2. Get the array elements. 3. If the list is of length 0 or 1, then it is already sorted. Otherwise: 4. Choose a key element in the list which is called a pivot. 5. Reorder the list with the rule that all elements which are less than the pivot come before the pivot and so that all elements greater than the pivot come after it. 6. After the partitioning, the pivot is in its final position. 7. Recursively reorder two sub-lists: the sub-list of lesser elements and the sub-list of greater elements. 8. Display the sorted list. Program: #include #include template class quick { w a[50]; int n; public: void get(); void sort(int,int); int partition(int,int); void put(); }; template void quick ::get() { int i; cout<<"\n Enter the no of terms:"; cin>>n; cout<<"\n Enter the values:\n"; for(i=1;i<=n;i++) cin>>a[i]; sort(1,n); }template Infant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 17 void quick ::sort(int p,int q) { int j; if(p int quick ::partition(int m,int p) { int i,j,t; w v; v=a[m]; i=m;j=p; do { do i++; while(a[i]v); if(i void quick::put() { int i; for(i=1;i<=n;i++) cout<q1; quickq2; cout<<"\n\t\t QUICK SORT USING TEMPLATES"; cout<<"\n\t\t ~~~~~ ~~~~ ~~~~~ ~~~~~~~~~"; q1.get(); cout<<"\n\n Sorted array of integer values:-\n"; q1.put(); q2.get(); cout<<"\n\n Sorted array of floating values:-\n";Infant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 18 q2.put(); getch(); } (6c) Bubble Sort using Templates Aim: To write a C++ program to do Bubble sort using templages. Algorithm: 1. Get the array length. 2. Get the array elements. 3. If the list is of length 0 or 1, then it is already sorted. Otherwise: 4. Compare the first element with other elements in that list. 5. If it is bigger, swap them. 6. Continue the step 4 and 5 until all the elements in the list is compared. 7. Display the sorted list. Program: #include #include #include template class bubble { t a[25]; public: void get(int); void sort(int); void display(int); }; template void bubble ::get(int n) { int i; cout<<"\nEnter the array elements:"; for(i=0; i>a[i]; }template void bubble ::display(int n) { int i; cout<<"\n The sorted array is...\n"; for(i=0;i void bubble ::sort(int n) { int i,j; t temp; for(i=0;ia[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } }void main() { int n; bubble b1; bubble b2; clrscr(); cout<<"\n Bubble Sort on Integer Values..."; cout<<"\n Enter the size of array:\n"; cin>>n; b1.get(n); b1.sort(n); b1.display(n); cout<<"\n Bubble Sort on Float Values..."; cout<<"\n Enter the size of array:\n"; cin>>n; b2.get(n); b2.sort(n); b2.display(n); getch(); } (6d) Insertion Sort using templates Aim: Write a C++ program to do insertion sort using templates. Algorithm: 1. Get the array length. 2. Get the array elements. 3. If the list is of length 0 or 1, then it is already sorted. Otherwise: 4. Consider an arbitrary key element from the list. 5. Compare it with other elements in the list. 6. Place it in the correct position. 7. Repeat the steps 4 to 6 until all the elements are positioned 8. Display the sorted list. Program: #include #include #include template class insertionInfant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 20 { t a[25]; public: void get(int); void sort(int); void display(int); }; template void insertion::get(int n) { int i; cout<<"\nEnter the array elements:"; for(i=0; i>a[i]; }template void insertion ::display(int n) { int i; cout<<"\n The sorted array is...\n"; for(i=0;i void insertion ::sort(int n) { int i,j; t temp; for(i=1;i=1) { if(a[j] i1; insertion i2; clrscr(); cout<<"\n Insertion Sort on Integer Values..."; cout<<"\n Enter the size of array:\n"; cin>>n; i1.get(n); i1.sort(n); i1.display(n); cout<<"\n Insertion Sort on Float Values..."; cout<<"\n Enter the size of array:\n";Infant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 21 cin>>n; i2.get(n); i2.sort(n); i2.display(n); getch(); }Infant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 22 Ex.No.7 Stack and Queue Operations with Exception Handling (7a) Stack class with Exception Handling Aim: To write a C++ program to create a stack class and to do stack operations with exception handling. Algorithm 1. Define the stack class. 2. Define dummy class for FULL and EMPTY 3. Get the capacity of stack from the user. 4. Get the choice of operation to do in stack 5. If push is selected 1. Check for stack overflow 2. If not push the item and increment top 3. If overflow throw FULL object 6. If pop is selected 1. Check for stack underflow 2. If not pop an item from the stack 3. If underflow throw EMPTY object 7. If stack show is selected display the stack. 8. Continue until user select Exit option Program #include #include #include using namespace std; class stack { private: int *s; int max; int top; public: class FULL{}; //for exception handling class EMPTY{}; //for exception handling stack(int); void push(int); int pop(void); void display(void); }; stack::stack(int m) { s=new int[m]; top=-1; max=m; }void stack::push(int item) { if(top=0) return s[top--]; else throw EMPTY(); //EMPTY object is thrown }void stack::display(void) { if(top>=0) for(int i=top; i>=0;i--) cout<<"\n\t|"<>size; stack s1(size); cout<<"\nStack with Exception Handling"; cout<<"\n\n\tMENU\n1.PUSH\n2.POP\n3.SHOW STACK\n4.EXIT"; cout<<"\nEnter your choice..."; cin>>ch; do { switch(ch) { case 1: cout<<"\nEnter the item to push..."; cin>>item; try { s1.push(item); } catch(stack::FULL) //FULL object is caught { cout<<"\n***Stack Overflow***\n"; } break; case 2: try { cout<<"\nPoped Item is..."<>ch; }while(ch<5); return 0; } (7b) Queue class with Exception Handling Aim: To write a C++ program to create a Queue class and to do queue operations with exception handling. Algorithm 1. Define the queue class. 2. Define dummy class for FULL and EMPTY 3. Get the capacity of queue from the user. 4. Get the choice of operation to do in queue 5. If enqueue is selected 1. Check for queue fullness 2. If not full insert the item and increment top 3. If full throw FULL object 6. If dequeue is selected 1. Check for queue emptiness 2. If not empty, remove an item from the stack 3. If empty throw EMPTY object 7. If show queue is selected display the queue. 8. Continue until user select Exit option Program #include #include using namespace std; class queue { private: int *q; int max, front, rear, int cnt;Infant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 25 public: class FULL{}; //for exception handling class EMPTY{}; //for exception handling queue(int); void enqueue(int); int dequeue(void); void display(void); };queue::queue(int m) { q=new int[m]; rear=0; front=0; cnt=0; max=m; }void queue::enqueue(int item) { if(cnt0) { cnt--; rear = rear %max; return q[rear++]; } else throw EMPTY(); //EMPTY object is thrown }void queue::display(void) { if(cnt>0) for(int i=0, j=front; i>size; queue q(size); cout<<"\nQueue Operations using Exception Handling"; cout<<"\n\n\tMENU\n1.ENQUEUE\n2.DEQUEUE\n 3.SHOW QUEUE\n4.EXIT"; cout<<"\nEnter your choice..."; cin>>ch; do { switch(ch) { case 1: cout<<"\nEnter the item to insert in to the queue..."; cin>>item; try { q.enqueue(item); } catch(queue::FULL) //FULL object is caught { cout<<"\n***Queue Full***\n"; } break; case 2: try { cout<<"\nRemoved Item from the Qis..."<>ch; }while(ch<5); return 0; }Infant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 27 Ex.No. 8 Operations on Complex Number using Files Aim: To write a C++ program that randomly generates complex numbers and write them two per line in a file along with an operator (+, -, *, or /) in the format (a + ib). To read one line at a time from this file, perform the corresponding operation on the two complex numbers read, and write the result to another file (one per line). Algorithm: 1. Define complex class 2. Generate random numbers 3. Store the random numbers as complex number 4. Create an object to work with file 5. Open a file to write. 6. Write the complex numbers with the operator in a file. 7. Close the file. 8. Create an anther object to read the file. 9. Read the content and do the operation 10. Store the resultant matrix in another file. Program: #include #include #include #include #include class complex { public: int real; int imag; complex(int r,int i) { real = r; imag = i; }complex() { real=imag=0; } void display(void); }; void complex::display(void) { cout<>no; icom>>real; icom>>ch; icom>>no; icom>>imag; imag=(ch=='+')?imag:-imag; icom>>no; icom>>op; complex a(real,imag); icom>>no; icom>>real; icom>>ch; icom>>no; icom>>imag; imag=(ch=='+')?imag:-imag; complex b(real,imag); //complex b is created complex c; switch(op) { case '+':c.real=a.real+b.real; c.imag=a.imag+b.imag; break; case '-':c.real=a.real-b.real; c.imag=a.imag-b.imag; break; case '*':c.real = (a.real*b.real)-(a.imag*b.imag); c.imag = (a.real*b.imag)+(a.imag*b.real); break; case '/':float qt; qt = b.real*b.real+b.imag*b.imag; c.real = (a.real*b.real+a.imag*b.imag)/qt; c.imag = (a.imag*b.real-a.real*b.imag)/qt; break; default:cout<<"\nInvalid Operator"; }cout<<"\nComplex 1:"; a.display(); cout<<"\nComplex 2:"; b.display(); cout<<"\nResultant Complex:"; c.display(); ofstream out("result.txt"); out<<"("<classes. 6. Use typeid to identify the class name from the given object. 7. Invoke the area and display functions from the polygon class using RTTI 8. Define the main() function to create the shape objects and polygon object 9. Invoke the displayarea() function of polygon class which in turn invoke the corresponding object’s method. Program: #include using namespace std; class square {private:float side; public: square(float x) { side = x; }square () {} void display() { cout<<"\n\tSide... "<Infant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 32 class polygon {private:T shape; public: polygon(T x) { shape = x; }void displayarea() { char type[20]; strcpy(type, typeid(shape).name()); cout<<"\nThe shape is belongs to "< poly1(s); polygon poly2(r); polygon poly3(c); polygon poly4(t); poly1.displayarea(); cout<<"\n=================\n"; poly2.displayarea() ; cout<<"\n=================\n"; poly3.displayarea() ; cout<<"\n=================\n"; poly4.displayarea (); cout<<"\n=================\n"; }Infant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 33 Ex.No. 10 Finding Minimum Cost Spanning Tree Aim: To define point class (node) and an arc class. To define a Graph class which represents graph as a collection of Point objects and Arc objects. And to write a method to find a minimum cost spanning tree in a graph. Algorithm: 1. Define node class 2. Define arc class 3. Define graph class with a collection of node and arc class objects. 4. Get the no. of nodes 5. Get the edges with weight from the user. 6. Generate adjacency matrix 7. Apply prims algorithm to find minimum cost spanning tree 8. Display the minimum cost spanning tree Program: #include using namespace std; #define MAX 50 #define TRUE 1 #define FALSE 0 #define MAXINT 250 class node {public: int no; node() {} node(int a) { no=a; } };class arc {public: int adj; int weight; arc(){} arc(int a) { adj = a; } };class graph {public: node nodes[MAX]; arc arcs[MAX][MAX];Infant Jesus College of Engineering OOP Lab Manual : Prepared by – G Roy Antony Arnold Page 34 graph(int n) { for(int i=1;i<=n;i++) { nodes[i].no=0; for(int j=1;j<=n;j++) arcs[i][j].adj=FALSE; } }void join(node n1, node n2, int w) { arcs[n1.no][n2.no].adj = w; arcs[n2.no][n1.no].adj=w; }void displayadj(int n) { cout<<"\nThe adjacency matrix...\n"; for(int i=1;i<=n;i++) { for(int j=1; j<=n; j++) cout<<"\t"<"<>n; graph g(n); cout<<"\nAssigning number for each node..."; for (int i=1; i<=n; i++) g.nodes[i].no = i; char ch='y'; int w; do { node a, b; cout<<"Create path between the nodes.."; cout<<"\nEnter the source node..."; cin>> a.no; cout<<"\nEnter the destination node..."; cin>>b.no; cout<<"\nEnter the weight"; cin>>w; g.join(a,b,w); cout<<"\nWant to continue... [y]es [n]o"; cin>>ch; }while(ch=='y'); g.displayadj(n); g.shortpath(n); cin>>n; return 0; }

Description
Object Oriented Programming - Lab Manual

Anna University, B.E. CSE

C++ Language

Object Oriented Programming - Lab Manual

Anna University, B.E. CSE

C++ Language

Object Oriented Programming - Lab Manual

Anna University, B.E. CSE

C++ Language

Object Oriented Programming - Lab Manual

Anna University, B.E. CSE

C++ Language

Comments

Want to learn?

Sign up and browse through relevant courses.

Name:
Your Email:
Password:
Country:
Contact no:


Area code Number
Subjects you are interested in:
Word verification: (Enter the text as in image)


Sign Up Already a member? Sign In
I agree to WizIQ's User Agreement & Privacy Policy

Your Facebook Friends on WizIQ

Explore Similar Courses

Program in C++

Price:$149

Give live classes, create & sell online courses

Try it free Plans & Pricing

Connect