Você está na página 1de 57

CS2209-Oops Lab

Department of IT

2012-2013

SYLLABUS
CS2209 OBJECT-ORIENTED PROGRAMMING LAB (Common to CSE & IT) 0 0 3 2

1. Design C++ classes with static members, methods with default arguments, friend functions. (For example, design matrix and vector classes with static allocation, and a friend function to do matrix-vector multiplication) 2. Implement complex number class with necessary operator overloadings and type conversions such as integer to complex, double to complex, complex to double etc. 3. Implement Matrix class with dynamic memory allocation and necessary methods. Give proper constructor, destructor, copy constructor, and overloading of assignment operator. 4. Overload the new and delete operators to provide custom dynamic allocation of memory. 5. Develop a template of linked-list class and its methods. 6. Develop templates of standard sorting algorithms such as bubble sort, insertion sort, merge sort, and quick sort. 7. Design stack and queue classes with necessary exception handling. 8. Define Point class and an Arc class. Define a Graph class which represents graph as a collection of Point objects and Arc objects. Write a method to find a minimum cost spanning tree in a graph. 9. Develop with suitable hierarchy, classes for Point, Shape, Rectangle, Square, Circle, Ellipse, Triangle, Polygon, etc. Design a simple test application to demonstrate dynamic polymorphism and RTTI. 10. Write a C++ program that randomly generates complex numbers (use previously designed Complex class) and writes them two per line in a file along with an operator (+, -, *, or /). The numbers are written to file in the format (a + ib). Write another program 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).

List of Equipments and software for a batch of 30 students

Processor 2.0 GHz or higher RAM 256 MB or higher Hard disk 20 GB or higher OS- Windows 2000/ Windows XP/ NT

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

List of Experiments S.NO 1. 2. 3. 4. 5. 6. 7. 8. 9. NAME OF THE PROGRAM Matrix Vector Multiplication Matrix Multiplication Using Class Complex Number Implementation Matrix Constructors And Destructors With Overloading Operator Linked List Using Template Class New And Delete Operator Overloading Bubble Sort Using Template Class Quick Sort Using Template Class Insertion Sort Using Template Class PAGE NO

10. Merge Sort Using Template Class 11. Implementation of Stack Using Class 12. Implementation of Queue Using Class 13. Complex Addition files 14. Runtime Polymorphism

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Ex.No:1 AIM:

MATRIX VECTOR MULTIPLICATION

To write a program to perform matrix vector multiplication using class. ALGORITHM:

Start Create a class matrix assign the values of 2 A[2][2]. Create a class vector assign the values of 2 a[2],b[2] Read the elements and display the matrix values. Using mul() multiply matrix A and vector a Display the RESULTant matrix vector multiplication array. Stop.

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Program: #include<iostream.h> #include<conio.h> class matrix { public: int A[2][2]; friend void mult(matrix,vector); }; class vector { public: int a[2],b[2]; friend void mult(matrix,vector); }; void mult(matrix m,vector v) { for(int i=0;i<2;i++) { v.a[i]=0; for(int j=0;j<2;j++) { v.a[i]+=m.A[i][j]*v.b[i]; } } cout<<"RESULT"; for(i=0;i<2;i++) { cout<<"\n"; cout<<v.a[i]; } } void main() { //clrscr(); int t1[2][2]={{2,2},{2,2}}; int t2[2]={{2},{3}}; matrix m; vector v; for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { m.A[i][j]=t1[i][j]; }} for(i=0;i<2;i++) { v.b[i]=t2[i]; } mult(m,v); getch();
St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

} Login:Tamil Output: Matrix Elements 3 3 3 3 3

Vector Elements 4 RESULT 21 21 RESULT :

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Ex.No:2 AIM:

MATRIX MULTIPLICATION USING CLASS

To write a program to perform matrix multiplication using class. ALGORITHM: Start Create a class matrix assign the values of n a[5][5]. Read the size elements and display the matrix using getsize(),readmatrix(),display() in public. Using nested loop get the element of matrix A. Display the matrix A using for loop Using mul(matrix A,matrix B)declare the variable. Assign the value of matrix c[i][j]=0 using nested for loop. Using nested for loop display the matrix and using A,B display the matrix mul(A,B) Stop.

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Program: #include<iostream.h> #include<conio.h> class matrix { private: int n; int a[5][5]; public: void getsize(); void readmatrix(); void display(); friend void mul(matrix,matrix); }; void matrix::getsize() { cout<<"\n Enter the matrix size:"; cin>>n; } void matrix::readmatrix() { int i,j; cout<<"Enter the elements:"; for(i=0;i<n;i++) { for(j=0;j<n;j++) { cin>>a[i][j]; } } } void matrix::display() { int i,j; for(i=0;i<n;i++) { cout<<"\n"; for(j=0;j<n;j++) { cout<<"\t"<<a[i][j]; } } } void mul(matrix A,matrix B) { int i,j,k,n,c[5][5]; n=A.n; for(i=0;i<n;i++) { for(j=0;j<n;j++) {
St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

c[i][j]=0; for(k=0;k<n;k++) { c[i][j]=c[i][j]+(A.a[i][k]*B.a[k][j]); } } } cout<<"\n output"; for(i=0;i<n;i++) { cout<<"\n"; for(j=0;j<n;j++) { cout<<"\t"<<c[i][j]; } } } void main() { clrscr(); matrix A; A.getsize(); A.readmatrix(); matrix B; B.getsize(); B.readmatrix(); A.display(); cout<<"\n"; B.display(); mul(A,B); getch(); } Login:Tamil OUTPUT: Enter the matrix size:2 Enter the elements: 4 6 5 7

Enter the matrix size:2

Enter the elements:


St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

3 5

2 1

4 6

5 7

3 5 output 37 53

2 1

13 19

RESULT :

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Ex.No:3 AIM:

COMPLEX NUMBER IMPLEMENTATION

To implement complex number class and use necessary constructions to convert on integer into complex number, double to complex and complex to double. ALGORITHM: Start Create a class and declare real, imaginary variable. Using constructors assign the value of real and img. Display the real and img part of complex variable. Stop.

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Program: #include<iostream.h> #include<conio.h> #include<math.h> class complex { private: int real,imag; public: complex() { real=0; imag=0; } complex(int a) { real=a; imag=a; } complex(double d) { real=(int)d; imag=(int)d; } void display() { cout<<"("<<real<<","<<imag<<")"<<endl; } operator double() { double sum=0; sum=sqrt(real*real+imag*imag); return(sum); } }; void main() { int x; double d; clrscr(); cout<<"\n Enter an integer"; cin>>x; complex c1(x); c1.display(); cout<<"\n enter a double value"; cin>>d; complex c2(d); c2.display(); double N1; N1=double(c1); double N2;
St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

N2=double(c2); cout<<"\n"<<N1<<endl<<N2; getch(); } Login:Tamil OUTPUT: Enter an integer6 (6,6)

enter a double value7 (7,7)

8.485281 9.899495

RESULT :

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Ex.No:4 AIM:

MATRIX CONSTRUCTORS AND DESTRUCTORS WITH OVERLOADING OPERATOR

To implement matrix class with necessary methods constructors destructors, copy constructors and overloading of assignment operators. ALGORITHM: Start the program Declare the constructor by passing arguments get the values of matrix using constructor. Display the values of matrix Overload the operation = by passing object to operator function assign the values and display. Declare the destructor and destruct the memory allocated. Read the values of row and column. Display by calling the functions and pass matrix object to constructor and display the matrix. Assign matrix to operator overloaded function. Stop.

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Program: #include<iostream.h> #include<conio.h> class matrix { private: int r,c; int m[5][5]; public: matrix(int,int); ~matrix(); void display(int,int); void operator=(matrix); }; matrix::matrix(int r1,int c1) { int i,j; cout<<"\n Enter the matrix:\n"; for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { cin>>m[i][j]; } } } void matrix::display(int r1,int c1) { int i,j; for(i=0;i<r1;i++) { cout<<"\n\n"; for(j=0;j<c1;j++) { cout<<"\t"<<m[i][j]; } } } void matrix::operator=(matrix A) { int i,j,n,t[5][5]; for(i=0;i<r;i++) { for(j=0;j<c;j++) { t[i][j]=m[i][j]; } } for(i=0;i<r;i++) { cout<<"\n\n";
St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

for(j=0;j<c;j++) { cout<<"\t"<<t[i][j]; } } } matrix::~matrix() { cout<<"\n Matrix block deleted"; } void main() { int r,c; cout<<"Enter row size:"; cin>>r; cout<<"\n Enter col size:"; cin>>c; matrix A(r,c); cout<<"\n A matrix"; A.display(r,c); matrix B(A); cout<<"\n B matrix"; B.display(r,c); matrix C=A; cout<<"\n C matrix"; C.display(r,c); getch(); } Login:Tamil OUTPUT: Enter row size:2

Enter col size:2

Enter the matrix: 7 4 5 8 A matrix 7 4


ISO 9001:2008

St. Josephs College of Engineering

CS2209-Oops Lab

Department of IT

2012-2013

5 B matrix 7 5 C matrix 7 5

4 8

4 8

Matrix block deleted Matrix block deleted Matrix block deleted

RESULT :

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Ex.No:5 AIM:

LINKED LIST USING TEMPLATE CLASS

To write C++ program of implementation of linked list using template class. ALGORITHM: Start Create a template class with variable T Assign the class name as list. Under private specifies declare data and next which stores the address of next data. Declare the first list with no and single parameter insert, delete and display. Using for loop, insert the necessary data. Using for loop display the data. Deletion operation can be anywhere in the list. Insert , delete, display from the list. Display the output. Stop.

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Program: #include<iostream.h> #include<conio.h> #include<process.h> template<class T> class list { private: int data; list *next; public: list() { data=0.0; next=NULL; } list(int dat) { data=dat; next=NULL; } void insert(list *node); void display(list *); void delete1(list *); }; template<class T> void list<T>::insert(list<T> *node) { list *last=this; while(last->next) last=last->next; last->next=node; } template<class T> void list<T>::display(list<T> *first) { list *temp; cout<<"\nThe Elements in the List:"; for(temp=first;temp;temp=temp->next) cout<<temp->data<<"\t"; cout<<endl; } template<class T> void list<T>::delete1(list<T> *first) { int x; list *prev,*pos,*temp; cout<<"\n Enter an element to be deleted"; cin>>x; prev=first; while(prev->next!=NULL&&prev->next->data!=x)
St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

prev=prev->next; if(prev->next!=NULL) { temp=prev->next; prev->next=temp->next; } } void main() { clrscr(); int choice,data; list<int> *first=NULL; list<int> *node; while(1) { cout<<"\n 1.insert\n"; cout<<"\n 2.display\n"; cout<<"\n 3.delete\n"; cout<<"\n 4.quit\n"; cout<<"\n choice[1-4]:\n"; cin>>choice; switch(choice) { case 1: cout<<"\n Enter data:"; cin>>data; node=new list<int>(data); if(first==NULL) first=node; else first->insert(node); break; case 2: first->display(first); break; case 3: first->delete1(first); break; case 4: exit(1); } } } Login:Tamil OUTPUT: 1.insert 2.display 3.delete
St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

4.quit choice[1-4]: 1 Enter data:7 1.insert 2.display 3.delete 4.quit choice[1-4]: 1 Enter data:10 1.insert 2.display 3.delete 4.quit choice[1-4]: 2 The Elements in the List:7 1.insert 2.display 3.delete 4.quit choice[1-4]: 3 Enter an element to be deleted10 1.insert 2.display 3.delete 4.quit choice[1-4]:
St. Josephs College of Engineering ISO 9001:2008

10

CS2209-Oops Lab

Department of IT

2012-2013

2 The Elements in the List:7 1.insert 2.display 3.delete 4.quit choice[1-4]:4 RESULT :

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Ex.No:6 AIM:

NEW AND DELETE OPERATOR OVERLOADING

To write a C++ program to overload the new and delete operators to provide custom dynamic allocation of memory. ALGORITHM: Start Create a class Initialize the variable Display constructor is called Overload new operator Create dynamic memory allocated for pointer Return pointer Overload delete operator Remove memory allocated to pointer by new operator. Stop.

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Program: #include<iostream.h> #include<conio.h> #include<stdlib.h> class test { private: int i; public: test() { cout<<"\n Constructor is called"; } ~test() { cout<<"\n Destructor is called"; } void *operator new(size_t size); void operator delete(void *p); }; void *test::operator new(size_t size) { void *p=malloc(size); if(!p) { cout<<"\n memory allocation failure"; exit(0); } return(p); } void test::operator delete(void *p) { free(p); } void main() { clrscr(); test *tp=new test; delete(tp); } Login:Tamil Output: Constructor is called Destructor is called RESULT :

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Ex.No:7 AIM:

BUBBLE SORT USING TEMPLATE CLASS

To write a C++ program to implement bubble sort using template class. ALGORITHM: Start Create a template class Declare the variables Call the swap function recursively if first no is greater than second. Pass two reference arguments Declare temporary variable Swap the numbers Read five integers float and character values. Call bubble function to sort the elements. Display the sorted elements. Stop.

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Program: #include<iostream.h> #include<conio.h> template<class X> void bubble(X a[],int n) { for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) if(a[i]>a[j]) { swap(a[i],a[j]); } } template<class X> void swap(X &a,X &b) { X temp; temp=a; a=b; b=temp; } void main() { int X[5]; float Y[5]; clrscr(); cout<<"\n Enter 5 integers"; for(int i=0;i<5;i++) cin>>X[i]; cout<<"\n Enter 5 float values"; for(i=0;i<5;i++) cin>>Y[i]; bubble(X,5); bubble(Y,5); cout<<"\n Sorted X array"; for(i=0;i<5;i++) cout<<"\n"<<X[i]; cout<<"\n sorted Y array"; for(i=0;i<5;i++) cout<<"\n"<<Y[i]; getch(); } Login:Tamil OUTPUT: Enter 5 integers9 6 2
St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

5 8 Enter 5 float values 6.3 2.5 1.2 6.8 9.5 Sorted X array 2 5 6 8 9 sorted Y array 1.2 2.5 6.3 6.8 9.5

RESULT :

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Ex.No:8 AIM:

QUICK SORT

To write a C++ program that performs a quick sort. ALGORITHM:

Start Write a routine to display the list Write a routine to swap two numbers. Create a routine to perform quick sort. While the position markers are not yet crossed over continue. Take first element and then assign the position markers. Bring a position such that the pivot element is greater than first but less than second marker then swap and repeat as required. Display the RESULT. Stop.

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Program: #include<iostream.h> #include<conio.h> #define size 5 template<class T> void print(T *a,int n) { for(int i=0;i<n;i++) cout<<" "<<a[i]; cout<<"\n"; } template<class T> void quick(T *a,int first,int last) { T pivot; int i,j; if(first<last) { pivot=a[first]; i=first; j=last; while(i<j) { while(a[i]<=pivot&&i<last) i++; while(a[j]>=pivot&&j>first) j--; if(i<j) swap(a,i,j); } swap(a,first,j); quick(a,first,j-1); quick(a,j+1,last); } } template<class T> void swap(T *a,int i,int j) { T temp; temp=a[i]; a[i]=a[j]; a[j]=temp; } void main() { int a[10]; float b[10]; char c[10]; clrscr(); cout<<"\n Enter 5 integers:"; for(int i=0;i<size;i++)
St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

cin>>a[i]; quick(a,0,size-1); print(a,size); cout<<"\n Enter 5 float values:"; for(i=0;i<size;i++) cin>>b[i]; quick(b,0,size-1); print(b,size); cout<<"\n enter 5 characters:"; for(i=0;i<size;i++) cin>>c[i]; quick(c,0,size-1); print(c,size); getch(); } Login:Tamil OUTPUT:

Enter 5 integers:9 6 2 5 4 24569

Enter 5 float values:3.2 6.9 5.8 2.6 5.7 2.6 3.2 5.7 5.8 6.9 enter 5 characters: r t j g
St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

c cgjrt

RESULT :

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Ex.No:9 AIM:

INSERTION SORT

To write a c++ program that performs an insertion sort. ALGORITHM: Start Create a routine that displays the list Perform sorting using the following steps. Sort through every element of the list. As each element is encountered store it in temp variable. Resort through the list from the next element to the first and if greater element is encountered shift the greater element back and store the temp over the current location. Write the required routine to create the arrays and call sort and display function. Stop.

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Program: #include<iostream.h> #include<conio.h> #define size 5 template<class T> void print(T *a,int n) { for(int i=0;i<n;i++) cout<<" "<<a[i]; cout<<"\n"; } template<class T> void sort(T *a,int n) { for(int i=1;i<n;i++) { T temp=a[i]; for(int j=i;j>0&&a[j-1]>temp;j--) { a[j]=a[j-1]; } a[j]=temp; } } void main() { int a[10]; float b[10]; char c[10]; clrscr(); cout<<"\n Enter 5 integers:"; for(int i=0;i<size;i++) cin>>a[i]; sort(a,size); print(a,size); cout<<"\n Enter 5 float values:"; for(i=0;i<size;i++) cin>>b[i]; sort(b,size); print(b,size); cout<<"\n Enter 5 characters:"; for(i=0;i<size;i++) cin>>c[i]; sort(c,size); print(c,size); getch(); } Login:Tamil OUTPUT: Enter 5 integers:
St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

6 2 3 8 5 23568 Enter 5 float values: 5.6 2.1 3.9 6.5 4.2 2.1 3.9 4.2 5.6 6.5 Enter 5 characters: e t h j i ehijt RESULT :

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Ex.No: 10 AIM:

MERGE SORT

To write a C++ program that performs merge sort. ALGORITHM: Start Write a routine to print the elements. Create a sorting routine that splits the array into two halves and then merge int he merge routine. Compare the elements of both the arrays and send the smaller element to the merged array. After one array is exhausted transfer the remaining elements into the merged array and transfer the merged array to calling function. Create a routine to call function as required and create each way. Stop.

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Program: #include<iostream.h> #include<conio.h> #define size 5 template<class T> void print(T *a,int n) { for(int i=0;i<n;i++) cout<<" "<<a[i]; cout<<"\n"; } template<class T> void sort(T *a,int n) { for(int i=1;i<n;i++) { T temp=a[i]; for(int j=i;j>0&&a[j-1]>temp;j--) { a[j]=a[j-1]; } a[j]=temp; } } void main() { int a[10]; float b[10]; char c[10]; clrscr(); cout<<"\n Enter 5 integers:"; for(int i=0;i<size;i++) cin>>a[i]; sort(a,size); print(a,size); cout<<"\n Enter 5 float values:"; for(i=0;i<size;i++) cin>>b[i]; sort(b,size); print(b,size); cout<<"\n Enter 5 characters:"; for(i=0;i<size;i++) cin>>c[i]; sort(c,size); print(c,size); getch(); } Login:Tamil OUTPUT:
St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Enter 5 integers: 9 5 1 2 5 12559 Enter 5 float values: 5.3 2.6 9.8 7.5 9.1 2.6 5.3 7.5 9.1 9.8 Enter 5 characters: i h d t a adhit RESULT :

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Ex.No:11 AIM:

IMPLEMENTATION OF STACK USING CLASS

To write a c++ program to implement stack using class. ALGORITHM: Start Display the menu Read the choice Assign the push variable Read the element If top is less than max insert element into stack otherwise display the stack as underflow. If there is no element present in stack display stack is empty. If the top is less than or equal to zero assign element as top. If no element is present, display stack underflow otherwise display elements in stack . If top==max then display stack is full. Stop.

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Program: #include<iostream.h> #include<stdlib.h> #define size 5 class stack { private: int a[size]; int top; public: stack(); void push(int); int top; int isempty(); int isfull(); void display(); }; stack::stack() { top=0; for(int i=0;i<size;i++) a[i]=0; } int stack::isempty() { return(top==0?1:0); } int stack::isfull() { return(top==size?1:0); } void stack::push(int i) { try { if(isfull()) { throw"full"; } else { a[top]=i; top++; } } catch(char *msg) { cout<<msg; } }
St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

int stack::pop() { try { if(isempty()) { throw"empty"; } else { return(a[--top]); } } catch(char *msg) { cout<<msg; } return 0; } void stack::display() { if(!isempty()) { for(int i=0;i<top;i++) cout<<a[i]<<endl; } } int main() { stack s; int num,ch=1; while(ch!=0) { cout<<"\n 1.push2.pop3.display4.exit": cout<<"enter the choice"; cin>>ch; switch(ch) { case 1: cout<<"enter the number"; cin>>num; s.push(num); break; case 2: cout<<"number to pop is"<<s.pop(); break case 3: cout<<"the number are"; s.display() break; default:
St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

cout<<"try again"; exit(0); return 0; } } return 0; } Login:Tamil OUTPUT: 1.push2.pop3.display4.exit enter the choice 1 enter the number 2 1.push2.pop3.display4.exit enter the choice 1 enter the number 4 1.push2.pop3.display4.exit enter the choice 2 number to pop is 4 1.push2.pop3.display4.exit enter the choice 2 number to pop is 2 1.push2.pop3.display4.exit enter the choice 2 empty RESULT :

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Ex.No:12 AIM:

IMPLEMENTATION OF QUEUE USING CLASS

To write a c++ program to implement queue using class. ALGORITHM: Start Initialize front=rear=0 Delete the elements in queue. Call function is full to check queue is full. If queue is full, display queue is full return from functions. Otherwise insert element into queue. Increment rear. Check queue is empty using is empty function. If true display queue is empty. Else increment front If front=rear return1 Else return 0. If rear=size return 1 else return 0. Display element in queue using for loop. Stop.

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Program: #include<iostream.h> #include<process.h> #define size 3 class queue { private: int rear; int front; int s[size]; public: queue() { front=-1; rear=0; } void insert(int); void del(); int isempty(); int isfull(); void display(); }; int queue::isempty() { return((rear>front||(front==-1&&rear==0)?1:0); } int queue::isfull() { return(rear==front&&front==size?1:0); } void queue::insert(int item) { try { if(isfull()) { throw"full"; } else { front++ s[front]=item; } catch(char *msg) { cout<<msg; } } void queue::del() { int item;
St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

try { if isempty()) throw"empty"; else { if(rear!=0) item=s[rear]; rear=rear+1; cout<<"\n deleted element is \n"<<item; } } catch(char *msg) { cout<<msg; } } void queue::display() { if(!isempty()) { cout<<"contents of queue"; for(int i=front;i>=rear;i--) { cout<<s[i]<<"\t"; } } } int main() { int ch,item; queue q; do { cout<<"\n1.insert2.delete3.exit"; cout<<"enter the choice"; cin>>ch; switch(ch) { case 1: cout<<"enter the element"; cin>>item; q.insert(item); q.display(); break; case 2: q.del(); q.display(); break; case 3: return 0;
St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

default: cout<<"try again"; return(0); } } while(ch!=3); return 0; } Login:Tamil OUTPUT: 1.insert2.delete3.exit"; enter the choice 1 enter the element 3 contents of queue 3 1.insert2.delete3.exit"; enter the choice 1 enter the element 6 contents of queue 36 1.insert2.delete3.exit"; enter the choice 2 deleted element is 3 1.insert2.delete3.exit"; enter the choice 2 deleted element is 6 1.insert2.delete3.exit"; enter the choice 2 queue is empty 1.insert2.delete3.exit"; enter the choice 3 RESULT :

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Ex.No:13 AIM: ALGORITHM:

COMPLEX ADDITION

To write a c++ program that performs complex addition using operator overloading. Start Using class declare variable and initialize the data member. Using output file stream object create a file. Read two complex number using input file stream insert into the file. Using operator overloading, overload the extraction operator to read real and imaginary values. Add the two complex number in the file and display the RESULT. Stop.

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Program: #include<iostream.h> #include<conio.h> #include<fstream.h> class complex { public: float real,imag; complex() { real=imag=0.0; } void write(ofstream &os) { os.write((char *)&real,sizeof(real)); os.write((char *)&imag,sizeof(real)); } int read(ifstream &is) { is.read((char *)&real,sizeof(real)); is.read((char *)&imag,sizeof(real)); return is.good(); } friend ostream &operator<<(ostream &os,complex &c); friend istream &operator>>(istream &is,complex &c); friend ofstream &operator<<(ofstream &fos,complex &c) { c.write(fos); return fos; } friend ifstream &operator>>(ifstream &fos,complex &c) { c.read(fos); return fos; } friend void add(complex,complex); }; istream &operator>>(istream &is,complex &c) { cout<<"real"; is>>ws>>c.real; cout<<"imag"; is>>ws>>c.imag; return is; } ostream &operator<<(ostream &os,complex &c) { os<<c.real<<"+i"<<c.imag<<"\t"; return os; } void add(complex c1,complex c2)
St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

{ cout<<"\n RESULT is "<<endl; cout<<(c1.real+c2.real)<<"+i"<<(c1.imag+c2.imag); } void main() { complex p_obj; clrscr(); ofstream ofile("complex2.txt",ios::out); char ch; cout<<"\n Enter 2 complex numbers"<<endl; int i=0; do { cin>>p_obj; ofile<<p_obj; i++; } while(i!=2); ofile.close(); ifstream ifile("complex2.txt",ios::in); cout<<"\n object written to the file"<<endl; complex c1,c2; i=0; while(1) { ifile>>p_obj; if(i==0) c1=p_obj; else c2=p_obj; i++; if(ifile.fail()) break; cout<<p_obj; } add(c1,c2); getch(); }

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Login:Tamil

Output: Enter 2 complex numbers real6.5 imag9.5 real6.3 imag2.5

object written to the file 6.5+i9.5 RESULT is 12.8+i12 6.3+i2.5

RESULT :

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Ex.No:14 AIM:

RUNTIME POLYMORPHISM

To write a c++ program to find area using polymorphism. ALGORITHM: Start Get x and y value, overload the << operator to display the points. Shape, using virtual functions display shape is drawn Square: inherits shapes property Read the position and length of the square. Display the position and length of square. Rectangle, read the position of sides of the rectangle. Display the position and length of rectangle. Triangle, read the position of vertices. Display the vertex position. Circle, read and display the position of centre and radius Stop.

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

Program: #include<iostream.h> #include<conio.h> class point { public: int x; int y; point() {} point(int tempx,int tempy) { x=tempx; y=tempy; } int getx() { return x; } int gety() { return y; } friend ostream & operator <<(ostream & tempout, point & temppoint) { tempout<<"("<<temppoint.getx()<<temppoint.gety()<<")"; return tempout; } }; class shape { point position; public: shape() {} virtual void draw() { cout<<"shape is drawn"; } }; class square:public shape { point leftbottom; int length; public: square() {} square(point tleftbottom, int tlength) { leftbottom=tleftbottom; length=tlength;
St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

} void draw() { cout<<"\n square is drawn at "<<leftbottom<<"and with length as"<<length<<"\n"; } }; class rectangle:public shape { point leftbottom,lefttop,rightbottom,righttop; public: rectangle() {} rectangle(point tleftbottom,point tlefttop,point trightbottom, point trighttop) { leftbottom=tleftbottom; lefttop=tlefttop; rightbottom=trightbottom; righttop=trighttop; } void draw() { cout<<"\n rectangle is drawn at("<<leftbottom<<","<< rightbottom<<")"<<"and"<<"("<<lefttop<<","<<righttop<<")\n"; } }; class triangle:public shape { point avertex,bvertex,cvertex; public: triangle() {} triangle(point tavertex,point tbvertex,point tcvertex) { avertex=tavertex; bvertex=tbvertex; cvertex=tcvertex; } void draw() { cout<<"\n triangle is drawn at "<<avertex<<" "<<bvertex<<" " <<cvertex; } }; class circle:public shape { point center; int radius; public: circle() {} circle(point tcenter, int tradius) {
St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

center=tcenter; radius=tradius; } void draw() { cout<<"circle is drawn at"<<" "<<center<<" "<<"and the radius is "<<radius; } }; class ellipses:public shape { point center; int radius; int angle; public: elllipses() { } ellipses(point tcenter, int tradius,int tangle) { center=tcenter; radius=tradius; angle=tangle; } void draw() { cout<<"\n"<<"ellipse is drawn at"<<" "<<center<<" "<<"and the radius is "<< radius<<"with an angle"<<angle<<"\n"; } }; void main() { clrscr(); point p1(10,10); point p2(3,2); square sq(p1,5); sq.draw(); rectangle rect(p1,p2,p1,p2); rect.draw(); circle c(p1,50); c.draw(); ellipses e(p2,34,23); e.draw(); triangle t(p1,p2,p1); t.draw(); shape *s; s=&sq; s->draw(); s=&rect; s->draw(); s=&t; s->draw();
St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab

Department of IT

2012-2013

getch(); } Login:Tamil

OUTPUT:

square is drawn at (1010)and with length as5 rectangle is drawn at((1010),(1010))and((32),(32)) circle is drawn at (1010) and the radius is 50 ellipse is drawn at (32) and the radius is 34with an angle23 triangle is drawn at (1010) (32) (1010) square is drawn at (1010)and with length as5 rectangle is drawn at((1010),(1010))and((32),(32)) triangle is drawn at (1010) (32) (1010) RESULT :

St. Josephs College of Engineering

ISO 9001:2008

CS2209-Oops Lab

Department of IT Object Oriented Programming viva questions 1. List out all object oriented concepts? 2. Define object? 3. Define class? 4. Define encapsulation? 5. Define data abstraction ? 6. Define message passing in oops? 7. Write the difference between class and structure in C++? 8. What do you mean by inheritance? 9. Define polymorphism? 10. List out all C++ access specifiers? 11. Define data member and member function? 12. What is the use of static members? 13. What is function overloading? 14. Define volatile function? 15. What is friend function? 16. Define constant object? 17. What is inline function? 18. How will you create pointer to data members? 19. What is local class? 20. Define constructor? 21. What do you mean by copy constructor? 22. Define default constructor and give example? 23. What is parameterized constructor? 24. Define destructor? 25. What do you mean by explicit constructor? 26. Explain multiple constructor? 27. What is operator overloading? 28. What is the lifetime of an object? 29. List out the operator which can not be overloaded ? 30. Define new operator? 31. Define delete operator? St. Josephs College of Engineering

2012-2013

ISO 9001:2008

CS2209-Oops Lab 32. Define wrapper classes?

Department of IT

2012-2013

33. List out the operators that can not be overloaded as a friend? 34. Define type conversion? 35. Difference between unary and binary operator? 36. Explain insertion and extraction operator? 37. Explain function overloading? 38. List out the user defined conversion? 39. What is function objects? 40. What is function template? 41. List out the draw back of using macros? 42. What is class template? 43. What is the use of export keyword? 44. Explain the use of Type name ? 45. Explain the advantages of template? 46. What is instantiation? 47. Explain the two models for template compilation? 48. Give an example of multi argument template? 49. List out the difference between generic and non generic function templates? 50. Explain exception handling mechanism? 51. Write the need for exception handling? 52. List out the components of exception handling mechanism?

53. What is the use of catch all ? 54. Define Rethrowing an exception? 55. Define uncaught exception? 56. What is the use of terminate()? 57. What is the use of unexpected()? 58. What are the disadvantages of the exception handling mechanism? 59. What is uncaught()? 60. List the different types of inheritance? 61. What is derived class? St. Josephs College of Engineering ISO 9001:2008

CS2209-Oops Lab Department of IT 62. What are the advantages of using inheritance? 63. Explain access control? 64. Define multiple inheritance? 65. What do you mean by virtual base class? 66. Define abstract class? 67. Define composite objects? 68. Define runtime polymorphism? 69. Define compile time polymorphism? 70. Define this pointer? 71. Define virtual function? 72. What do you mean by pure virtual function? 73. What is RTTI? 74. Define static binding? 75. Define dynamic binding? 76. What is cross casting? 77. What is down casting? 78. What is reinterpret_cast? 79. What is the use of typeid operator? 80. What are streams? 81. List all formatted I/O in C++? 82. Define manipulator? 83. Write the difference between manipulators and ios function? 84. Explain setf()? 85. Define Namespace? 86. Explain get() and put() function? 87. Explain read() and write() function? 88. Explain seekg() and seekp() function? 89. Explain tellg() and tellp() function? 90. What is global namespace? 91. Define Koening lookup? 92. List different way of constructing a string object? St. Josephs College of Engineering

2012-2013

ISO 9001:2008

CS2209-Oops Lab 93. Define standard template library? 94. Write some example manipulators in C++?

Department of IT

2012-2013

95. List out the advantages of readymade software components? 96. Compare C++ and C strings? 97. Write functions that can help us in finding out different characteristics of the string object? 98. List out different substring operations for string object? 99. What is the advantages of using generic algorithm? 100. What is meant by Object Oriented Programming?

St. Josephs College of Engineering

ISO 9001:2008

Você também pode gostar