Sie sind auf Seite 1von 59

INDEX-I S.

NO
1

NAME OF THE EXPERIMENT


To create the program to find total, average of given two numbers by using function with default arguments, static data members and friend function. The program to implement complex number class with necessary operator overloading and type conversion such as integer to complex, complex to double. To implement matrix class with dynamic memory allocation and necessary methods. With proper constructor, destructor, copy constructor and overloading of assignment operator. Overload the new and delete operators to provide custom dynamic allocation of memory. Develop a template of linked-list class and its methods. Develop templates of standard sorting algorithms such as bubble sort, insertion sort, merge sort and quick sort. Design stack and queue classes with necessary exception handling.

PAGE NO
1

10

15

5 6

19 24

31

INDEX-II S.NO
8

NAME OF THE EXPERIMENT


Define point class and an Arc class. Define a Graph class which represents graph as collection of point objects and Arc objects. Write a method to find a minimum cost spanning tree in a graph.

PAGE NO
40

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.

48

10

Write a C++ program that randomly generates complex numbers 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 two complex numbers read, and write the result to another file ( one per line ).

54

EX. NO. 1

AIM: To create the program to find total, average of given two numbers by using function with default arguments, static data members and friend function. ALGORITHM: 1. Include the required header files. 2. Declare the class with one static data member, two integer data members in private access specifier. 3. Declare one default arguments function to store two values for integer variables, one function to count number of object created by static data member and friend function to find total, average and to display it in public access specifier. 4. Define static member variable outside the class defination. 5. Define the member functions, non-member function (friend function) of the class outside the class declaration. 6. For defining member function use scope resolution operator(::), for friend function not needed. 7. In main ( ) function declare object of the class. 8. Call the public member of the class through object. 9. Friend function without object, by calling friend function send object as argument. 10. Close the main( ) function.

PROGRAM: #include<iostream.h> #include<conio.h> class prog

{ private: static int count; int v1,v2; public: void getdata(int n1=35,int n2=89); void getcount(); friend void average(prog p); }; int prog :: count; void prog :: getcount() { count++; cout<<endl<<count<<" OBJECT : "<<endl; } void prog :: getdata(int val1,int val2) { v1=val1; v2=val2; } void average(prog p) { cout<<" GIVEN VALUES : "<<p.v1<<" "<<p.v2<<endl; cout<<"Total = "<<(p.v1+p.v2)<<endl; cout<<"Average = "<<(p.v1+p.v2)/2<<endl; } void main() { prog p1,p2; clrscr(); p1.getcount(); p1.getdata(); cout<<endl; average(p1); cout<<endl; p2.getcount(); p2.getdata(100,350);

cout<<endl; average(p2); cout<<endl; getch(); }

OUTPUT: 1 OBJECT : GIVEN VALUES : 35 89 Total = 124 Average = 62

2 OBJECT : GIVEN VALUES : 100 350 Total = 450 Average = 225

RESULT: Thus the program to find total, average of given two numbers by using function with default arguments, static data members and friend function is developed and executed successfully.

EX. NO. 2 AIM: The program to implement complex number class with necessary operator overloading and type conversion such as integer to complex, complex to double. ALGORITHM: 1. Include the required header files for the program. 2. Declare the class complex with two float variable in private access

specifier. 3. In public access specifier declare default constructor, constructor overloading, Operator function for operator overloading, conversion function for converting class to basic type.Constructor with one argument used for converting basic to class type, argument is converted to class type. 4. Declare display function with no argument and return type to display result. 5. Define constructors, operator function, conversion function after class declaration with scope resolution operator( : : ). 6. In main function first declare two objects of class complex c1, c2 with two float arguments, that will create two object and store float values in each object. 7. Then create another object complex c3=c1+ c2 to call operator function for operator overloading. The real parts of c1, c2 and imaginary part of c1, c2 added separately put to c3 object. 8. Display real, imaginary value of objects cl,c2 and c3 by calling display function.

9. Create another object of the class complex, c4 then declare and assign value for the integer variable value, after that the statement c4 = value, this statement call the constructor with one integer argument for converting integer to complex. 10. Then display the value of the object c4 by calling display function, c4.display(). 11. Create another object complex c5, after declare and assign the value for the variable of double, double num=c5, this statement call the conversion function to complex to double type.

12. After that display value of num. 13. Close the main( ) function,

PROGRAM: #include<iostream.h> #include<conio.h> class complex { private: float real,imag; public: complex(); complex(float,float); complex(int); complex operator+(complex); operator double(); void display(); }; complex :: complex() { real=0.0;

imag=0.0; } complex :: complex(float x,float y) { real=x; imag=y; } complex complex :: operator+(complex c) { complex temp; temp.real=real+c.real; temp.imag=imag+c.imag; return temp; } void complex :: display(void) { cout<<endl; cout<<real<<" +j "<<imag; } complex :: complex(int v) //constructor to convert basic to class type { real=v; imag=v; }

complex :: operator double() //conversion function for class to basic type { double val=8.9; real=val; imag=val; val=real+imag; return val; } int main() { complex c1(7.9,8.6); complex c2(5.6,7.4); clrscr(); cout<<endl<<" Complex Numbers with operator overloading "<<endl; complex c3=c1+c2; c1.display(); c2.display(); c3.display(); cout<<endl<<endl<<" Type Conversion : integer to complex: "<<endl;

complex c4; int value=9; c4 = value; c4.display(); cout<<endl; cout<<endl<<endl<<" Type Conversion : complex to double : "<<endl; complex c5; double num=c5; cout<<endl<<num<<endl; getch(); return 0; }

OUTPUT: Complex Numbers with operator overloading 7.9 +j 8.6 5.6 +j 7.4 13.5 +j 16 Type Conversion : integer to complex: 9 +j 9 Type Conversion : complex to double : 17.799999

RESULT: Thus the program to implement complex number class with necessary operator overloading and type conversion such as integer to complex, complex to double is successfully developed and executed.

EX. NO. 3 AIM: To implement matrix class with dynamic memory allocation and necessary methods. With proper constructor, destructor, copy constructor and overloading of assignment operator. ALOGORITHM: 1. Include the required header files and one global variable, initialize to zero. 2. Declare class matrix with two integer data members in public access specifier and one default constructor, constructor with argument, copy constructor with reference of the object as argument, destructor and display function in public access specifier. 3. Define constructors, destructor and display function outside the class using scope resolution operator ( : : ) . In display function increment global variable value before showing which object of the class. In destructor decrement global variable after showing which object deleted.

4. In main( ) function create and initialize object of class matrix, m1 by parameterized constructor. Then create one pointer object of the class matrix, *ptr1 and assign address of the object m1, to the pointer object *ptr1. 5. Call the display function by pointer object using ->( arrow operator) to show the values of the object, m1. 6. Then create another object of the class matrix, m2 and initialize value of m1 to m2 by calling copy constructor, matrix m2(m1). 7. Create another pointer object of the class matrix, *ptr2 and assign address of the object m2, to the pointer object *ptr2. 8. Call the display function by pointer object using -> ( arrow operator) to show the values of the object, m2. 9. Create another object of the class matrix m3, the assign m3 = m2. Now copy constructor not called. The assignment operator ( = ) copy the member of the object m2 to m1 one by one, here overloading of assignment operator ( = ) happening. 10. Call the display function by the object m3 using dot operator, to show the values of object m3. 11. Close the main( ) function.

PROGRAM: #include<iostream.h> #include<conio.h> int count=0; class matrix { private: int m,n; public: matrix() { } matrix(int,int); matrix(matrix &x); ~matrix(); void display(); }; matrix :: matrix(int x,int y) { m=x;n=y; } matrix :: matrix(matrix &x) { m=x.m; n=x.n; } matrix :: ~matrix() { cout<<count<<" Object destroyed "<<endl; count--; }

void matrix :: display() { cout<<++count<<": OBJECT VALUES"<<endl; cout<<"m= "<<m<<endl; cout<<"n="<<n<<endl; }

void main() { clrscr(); matrix m1(45,68); matrix *ptr1=&m1; cout<<endl; ptr1->display(); matrix m2(m1); matrix *ptr2=&m2; cout<<endl; ptr2->display(); cout<<endl<<endl; matrix m3; m3=m2;//Overloading of assignment operator( = ) m3.display(); cout<<endl<<endl; cout<<" DELETION OF OBJECTS BY DESTRUCTOR : "<<endl<<endl; }

OUTPUT: 1: OBJECT VALUES m= 45 n=68 2: OBJECT VALUES m= 45 n=68 3: OBJECT VALUES m= 45 n=68 DELETION OF OBJECTS BY DESTRUCTOR : 3 Object destroyed 2 Object destroyed 1 Object destroyed

RESULT: Thus the program to implement matrix class with dynamic memory allocation and necessary methods. With proper constructor, destructor, copy constructor and overloading of assignment operator.

EX. NO. 4 AIM: To overload the new and delete operators to provide custom dynamic allocation of memory. ALGORITHM: 1. Include the required header files in the program. 2. Declare the class dynamic with integer variable in private access specifier, constructor, destructor, operator function, operator new with one argument of type size_t which is size of the class and void pointer as return type to overload the new operator. 3. Include another operator function, to overload delete operator with no return type and void pointer as argument. 4. In constructor initialize the value of integer variable, and display the statement, constructor is called. 5. In operator function, void * operator new(size_t size), declare one void pointer and allocate memory through malloc( ) function by giving size of the class as argument. And check memory allotted or not. Then return void pointer. 6. In operator function, void operator delete(void *p), delete the allocated memory by using function free( ) by giving void pointer as argument. 7. In destructor display the statement, destructor is called. 8. Define all these functions, constructor, destructor, operator function inside the class itself. 9. In main( ) function, create one pointer object and allocate memory through new operator, dynamic *dptr = new dynamic, this statement call the operator function, void * operator new(size_t size) and it returns void pointer.

10. Next delete(dptr), this function call the operator function, void operator delete(void *p) to overload delete operator. 11. Close the main( ) function.

PROGRAM: #include<stdlib.h>

#include<conio.h> #include<iostream.h> class dynamic { int i; public: dynamic() { i=1; cout<<" Constructor is called .. "<<endl; } void * operator new(size_t size) { void *p=malloc(size); if(p==0) { cout<<"Memory allocation failure . . "<<endl; exit(0); } else cout<<"Memory allocation success . . "<<endl; return p; } void operator delete(void *p) { free(p); } ~dynamic() { cout<<" Destructor is called . . "<<endl; } }; void main() { clrscr(); dynamic *dptr=new dynamic; delete(dptr); getch(); }

OUTPUT: Memory allocation success . . Constructor is called ..

Destructor is called . .

RESULT: Thus the program to overload the new and delete operators to provide custom dynamic allocation of memory is developed and executed successfully.

EX. NO .5 AIM: Program to develop a template of linked-list class and its methods. ALGORITHM:

1. Include the required header files in the program. 2. Declare the class linkedlist, with one variable of integer type, to store the value in each node and one pointer of class type to store address of the next node in each node in private access specifier. 3. In public access specifier declare three methods, one for to create nodes at runtime and to store the value, address of another node in each node. 4. Another function to display the value stored in each node. 5. Another function to count the number of nodes created. 6. All these three member functions of linkedlist class, have one argument, pointer object. 7. Close the class linkedlist. 8. All the member function of the template class linkedlist is defined outside the class. It is done by template function, so all the member functions declare in the class are template functions. 9. In main( ) function declare two pointer objects. Initialize one pointer to null, for another pointer allocate dynamic memory by new operator. 10. Call all the member function by pointer object initialized to null by arrow operator ( -> ) with another pointer object, which is dynamically memory allocated.

11. First call the create function, next print function to display the value stored in each node. Finally call count function , this function return integer value to display the number of nodes created 12. Close the main( ) function.

PROGRAM: #include<iostream.h> #include<conio.h> template<class T> class linkedlist { private: int number; linkedlist *next; public: void create(linkedlist *node); void print(linkedlist *node);

int count(linkedlist *node); }; template<class T> void linkedlist<T>::create(linkedlist<T> *node) { cout<<"Enter the number "<<endl; cout<<"(Enter -999 at an end):"<<endl; cin>>node->number; if(node->number==-999) { node->next=0; } else { node->next=new linkedlist<int>; create(node->next); } } template<class T> void linkedlist<T>::print(linkedlist<T> *node) { if(node->next!=0) { cout<<node->number<<"-->"; if(node->next->next==0) cout<<node->next->number; print(node->next); }}

template<class T> int linkedlist<T>::count(linkedlist<T> *node) { if(node->next==0) return 0; else return(1+(count(node->next))); } void main() { linkedlist <int> *node1=0;clrscr(); linkedlist <int> *node2; node2=new linkedlist<int>; node1->create(node2); cout<<endl<<endl;

node1->print(node2); cout<<endl<<endl; cout<<"Number of nodes: "<<node1->count(node2); getch(); }

OUTPUT: Enter the number (Enter -999 at an end): 45 Enter the number (Enter -999 at an end): 89 Enter the number (Enter -999 at an end): 76 Enter the number (Enter -999 at an end): 234 Enter the number (Enter -999 at an end): 99

Enter the number (Enter -999 at an end): 67 Enter the number (Enter -999 at an end): 75 Enter the number (Enter -999 at an end): -999 45-->89-->76-->234-->99-->67-->75-->-999 Number of nodes: 7

RESULT: The program to develop a template of linked-list class and its methods is developed, executed successfully.

EX. NO. 6 AIM:

To develop templates of standard sorting algorithms such as bubble sort, insertion sort, merge sort and quick sort. ALGORITHM: 1. Include the required header files in the program 2. Create the template function for bubble sort with two arguments, one is array of type T, another one is integer argument and define the function. 3. Create another template function for insertion sort with two arguments, one is array of type T, another one is integer argument and define the function. 4. Create another template function for merge sort with three arguments, one is pointer of type T, another two is integer argument and define the function. 5. Create another template function, sort with two argument, one is pointer of type T, another one is integer type and define the function. Recursion happening in this function, merge function is called from this function. 6. Create another template function for quick sort with three arguments, one is pointer of type T, another two is integer argument and define the function. 7. Create another template function, swap with three arguments, one is pointer of type T and other two arguments of type integer. This function is called from quick sort to swap the values. 8. Create another template function, print with two arguments, one is pointer of type T and another is integer type. This function going to display the values before and after sorting for both merge and quick sort.

9. In main() function, first declare float array,x1[5] with five elements and initialize the values at design time itself. 10. Display the values of the array x1 before and after calling bubble sort using for loop.

11. Next declare integer array, x2[5] with five elements and initialize the values at design time itself. 12. Display the values of the array x1 before and after calling insertion sort using for loop. 13. Next declare double array, x3 with five elements and initialize the values at design time itself. 14. Display the values of the array x3 before sorting by calling print() function with corresponding arguments. 15. Call the sort() function with corresponding arguments to execute the sort function, from the sort function, merge sort function is called. Display the values of the array x3 after merge sort function execution by calling print() function with corresponding arguments. 16. Finally create char array, x4 with five elements and initialize the values at design time itself. 17. Display the values of the array x4 before quick sorting by calling print() function with corresponding arguments. 18. Call the quick() function with corresponding arguments, to execute the quick sort and display the values by calling print() function with corresponding arguments. 19. Close the main () function.

PROGRAM: #include<iostream.h> #include<conio.h>

template<class T> void bubble(T a[],int n) { T temp; for(int i=0;i<n-1;i++) { for(int j=n-1;i<j;j--) { if(a[j]<a[j-1]) { temp=a[j]; a[j]=a[j-1]; a[j-1]=temp; } }}} template <class T1> void insertion(T1 num[],int n) { for(int i=1;i<n;i++) { int temp=num[i]; int j=i-1; while((num[j]>temp)&&(j>=0)) { num[j+1]=num[j]; j--; } num[j+1]=temp; } } template<class T> void merge(T *a,int n1,int n2) { T *temp=new T[n1+n2]; int i=0,j1=0,j2=0; while(j1<n1&&j2<n2) temp[i++]=(a[j1]<=a[n1+j2]?a[j1++]:a[n1+j2++]); while(j1<n1) temp[i++]=a[j1++];

while(j2<n2) temp[i++]=(a+n1)[j2++]; for(i=0;i<n1+n2;i++) a[i]=temp[i]; delete[] temp;

} template<class T> void sort(T *a,int n) { if(n>1) { int n1=n/2; int n2=n-n1; sort(a,n1); sort(a+n1,n2); merge(a,n1,n2); } } template<class T> void quick(T *a,int first,int last) { int i,j,pivot; 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>last) 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;

} template<class T> void print(T *a,int n) { cout<<a[0]; for(int i=1;i<n;i++) { cout<<" "<<a[i]; } cout<<endl; } int main() { int i;clrscr(); float x[5]={8.9,6.7,5.9,9.8,4.9}; cout<<" BEFORE BUBBLE SORT : "<<endl<<endl; for(i=0;i<5;i++) cout<<x[i]<<" "; bubble(x,5); cout<<endl<<endl<<" AFTER BUBBLE SORT : "<<endl<<endl; for(i=0;i<5;i++) cout<<x[i]<<" "; int x2[5]={78,42,59,87,21}; cout<<endl<<endl; cout<<" BEFORE INSERTION SORT : "<<endl<<endl; for(i=0;i<5;i++) cout<<x2[i]<<" "; insertion(x2,5); cout<<endl<<endl; cout<<" AFTER INSERTION SORT : "<<endl<<endl; for(i=0;i<5;i++) cout<<x2[i]<<" "; double x3[5]={12.7865,11.6545,15.9990,25.1789,2.333}; cout<<endl<<endl;

cout<<" BEFORE MERGE SORT: "<<endl<<endl; print(x3,5); sort(x3,5); cout<<endl<<endl; cout<<" AFTER MERGE SORT: "<<endl<<endl; print(x3,5); char x4[5]={'h','d','a','c','f'}; cout<<endl<<endl; cout<<" BEFORE QUICK SORT: "<<endl<<endl;

print(x4,5); quick(x4,0,4); cout<<endl<<endl; cout<<" AFTER QUICK SORT: "<<endl<<endl; print(x4,5); getch(); return 0; }

OUTPUT: BEFORE BUBBLE SORT :

8.9 6.7 5.9 9.8 4.9 AFTER BUBBLE SORT : 4.9 5.9 6.7 8.9 9.8 BEFORE INSERTION SORT : 78 42 59 87 21 AFTER INSERTION SORT : 21 42 59 78 87 BEFORE MERGE SORT: 12.7865 11.6545 15.999 25.1789 2.333 AFTER MERGE SORT: 2.333 11.6545 12.7865 15.999 25.1789 BEFORE QUICK SORT: h d a c f AFTER QUICK SORT: a d c f h

RESULT: Thus the program to develop templates of standard sorting algorithms such as bubble sort, insertion sort, merge sort and quick sort is developed, executed successfully.

EX. NO. 7

AIM: Design stack and queue classes with necessary exception handling ALGORITHM: 1. Start the program. 2. Include the required header files. 3. Declare the global variables needed for the program. 4. Declare the class stack with one array in private and five function for push, pop, display, length and isempty in public access specifier. 5. Close the class stack. Define function members outside the class. 6. Declare the class queue with four member functions, enqueue, dequeue, display and length in public access specifier. 7. Close the class queue. Define function members outside the class. 8. Open the main( ) function. Declare the object of stack class, and one integer variable count, another variable i=1. 9. Display the required statements for stack operation. 10. Inside the try block , use while loop check i<12, then continue the operations of stack. 11. In the body while loop, enter your choice 1 for push, 2 for pop, 3 to display the elements, 4 for to display the length. Each choice going to call the required member functions for required operation. 12. If choice is more than four, throw the exception. 13. If i>12, display the statement Stack operation is stopped. 14. Close the while loop and then try block.

15. Thrown exception is caught by catch block and it takes appropriate action.

16. Initialize i =1, then create object of the class queue, display the required statements for queue operation. 17. Inside the try block, use while loop check i<12, then continue the operations of queue. 18. In the body while loop, enter your choice 1 to add elements, 2 to delete the elements, 3 to display the elements, 4 to display the length. Each choice going to call the required member functions for required operation. 19. If choice is more than four, throw the exception. 20. If i>12, display the statement Queue operation is stopped. 21. Close the while loop and then try block. 22. Thrown exception is caught by catch block and it takes appropriate action. 23. Close the program.

PROGRAM: #include<iostream.h>

#include<stdio.h> #include<conio.h> #include<stdlib.h> #define size 10 int q[size],front=0,rear=0; int top=-1; class stack { private: int stk[size]; public: void push(); void pop(); int length(); int isempty(); void display(); }; void stack::push() { int no; if(top==size-1) cout<<endl<<" Stack is full "<<endl; else { cout<<endl<<"Enter a Number : "; cin>>no; top++; stk[top]=no; } } void stack::pop() { int no; if(isempty()) { cout<<endl<<" Stack is empty "<<endl; top=-1; }

else { no=stk[top]; cout<<endl<<no<<" is Popped from the stack "<<endl; }

top--; } void stack::display() { int i; if(isempty()) { cout<<endl<<" Stack empty "<<endl; } cout<<endl<<" Elements in the stack"<<endl; for(i=top;i>-1;i--) cout<<stk[i]<<"\t"; } int stack::isempty() { return(top==-1); } int stack::length() { return top+1; } class queue { public: void enqueue(); void dequeue(); void display(); int length(); }; void queue::enqueue() { int no; if(rear==size&&front==0) cout<<endl<<"Queue is full "<<endl;

else { cout<<endl<<"Enter Number : "; cin>>no; q[rear]=no; rear++; } }

void queue::dequeue() { int no,i; if(front==rear) cout<<endl<<" Queue is empty "<<endl; else { no=q[front]; front++; cout<<no<<" is removed from the queue "<<endl; } } void queue::display() { int i,temp=front; if(front==rear) cout<<endl<<"queue is empty "<<endl; else { cout<<"Elements in the queue "<<endl; for(i=temp;i<rear;i++) cout<<q[i]<<"\t"; } } int queue::length() { return rear-front; }

void main() { stack s1; int choice,=1; clrscr(); cout<<" STACK OPERATION : "; cout<<"1.PUSH 2.POP 3.DISPLAY 4.LENGTH"<<endl<<endl; try { while(i<12) { cout<<" Enter your choice : ";

cin>>choice; if(choice==1) s1.push(); else if(choice==2) s1.pop(); else if(choice==3) s1.display(); else if(choice==4) cout<<endl<<" Number of elements in the stack: "<<s1.length(); else if(choice>4) throw choice; i++; if(i>12) { cout<<Stack operation is stopped . . . <<end; int i; i=getchar( ) ; } } catch(int n1) { cout<< Caught an exception <<n1<<endl; } clrscr(); i=1; queue q1; cout<<endl;

cout<<" QUEUE OPERATION : "; cout<<"1.ADD ELEMENT 2.REMOVE ELEMENT 3.DISPLAY 4.LENGTH "; cout<<endl<<endl; try { while(i<=12) { cout<<" Enter your choice : "; cin>>choice; if(choice==1) q1.enqueue(); else if(choice==2) q1.dequeue(); else if(choice==3) q1.display();

else if(choice==4) cout<<endl<<" Number of elements in the queue "<<q1.length(); else if(choice>4) throw choice; if(i>12) cout<<Queue Operation is stopped . . .<<endl; }} catch(int n2) { cout<< Caught an exception <<n2<<endl; }}

OUTPUT: STACK OPERATION : 1.PUSH 2.POP 3.DISPLAY 4.LENGTH

Enter your choice : 1 Enter a Number : 89 Enter your choice : 1 Enter a Number : 45 Enter your choice : 1 Enter a Number : 90 Enter your choice : 1 Enter a Number : 39 Enter your choice : 1 Enter a Number : 72 Enter your choice : 3 Elements in the stack 72 39 90 45 89 Enter your choice : 4

Number of elements in the stack: 5 Enter your choice : 2 72 is Popped from the stack Enter your choice : 2 39 is Popped from the stack Enter your choice : 3 Elements in the stack 90 45 89 Enter your choice : 1 Enter a Number : 1000 Enter your choice : 3 Elements in the stack 1000 90 45 89 Stack operation is stopped . . .

QUEUE OPERATION : 1.ADD ELEMENT 2.REMOVE ELEMENT 3.DISPLAY 4.LENGTH Enter your choice : 1

Enter Number : 67 Enter your choice : 1 Enter Number : 890 Enter your choice : 1 Enter Number : 244 Enter your choice : 1 Enter Number : 81 Enter your choice : 1 Enter Number : 39 Enter your choice : 3 Elements in the queue 67 890 244 81

39

Enter your choice : 4

Number of elements in the queue 5 Enter your choice : 2 67 is removed from the queue Enter your choice : 2 890 is removed from the queue Enter your choice: 9 Caught an exception 9

RESULT: The program to design stack and queue classes with necessary exception handling successfully developed and executed.

EX. NO. 8 AIM:

To develop the method to find a minimum cost spanning tree in graph by using Kruskals Algorithm in graph class. Graph class is derived publicly from the point class and Arc class. ALGORITHM: 1. Start the program, include required header files and constant using simple macro substitutions. 2. Declare one global array by using one constant to denote number of values it will store. 3. Declare the class point, declare one function wait( ) with no arguments and return value in public access specifier. Define inside the class itself. 4. Close the point class. 5. Declare the class arc, define one function makeset(int) with one integer argument and no return value in public access specifier. Define inside the class itself. Close the arc class. 6. The class graph is publicly derived from the class point and arc. 7. In this derived class, graph declare and define the fallowing methods i. void initial(int n), ii. int find(int i), iii. void merge(int p,int q), iv. int equal(int p,int q) and void testuniv(void) in public. 8. Close the graph class, open the main( ) function. 9. In main function declare and initialize one two dimensional integer array with constant inside brackets indicating rows and columns. 10. Next declare other two, two dimensional integer array with constants indicating rows and give value 3 for indicating columns.

11. Declare required variables, by using object of graph class call the required methods from point, arc and graph class since graph is derived publicly from two base class, point and arc.

12. Do the necessary coding as per kruskals algorithm to find minimum cost spanning tree. 13. In coding show the set of edges before sorting and after sorting. 14. Show all the disjoint subsets. 15. After this, show Minimal Spanning Tree Edges. 16. Finally show the Minimal Spanning Tree Weight. 17. Close the program.

PROGRAM: #include<iostream.h> #include<stdio.h> #include<conio.h>

#include<string.h> #include<stdlib.h> #define N 6 #define M 15 int u[N]; class point { public: void wait(void) { int i; cout<<"Press Enter to continue . . . "<<endl; i=getchar(); } }; class arc { public: void makeset(int i) { u[i]=i; } }; class graph:public point,public arc { public: void initial(int n) { int i; for(i=0;i<n;i++) makeset(i); } int find(int i) { int j; j=i;

while(u[j]!=j) { j=u[j]; } return j; } void merge(int p,int q)

{ if(p<q) u[q]=p; else u[p]=q; } int equal(int p,int q) { if(p==q) return 1; else return 0; } void testuniv(void) { int i; cout<<endl; cout<<"The disjoint subsets are : "; for(i=0;i<N;i++) cout<<" "<<u[i]; cout<<endl; } }; int main() { graph g; int w[N][M]={0,2,4,1,3,2, 2,0,6,4,5,1, 4,6,0,4,2,1, 1,4,4,0,5,4, 3,5,2,5,0,6, 2,1,1,4,6,0}; int E[M][3]; int F[N-1][3];

int numedges=0; int nextedges=0; int weight=0; int a,b,c,i,j,k; k=0;clrscr(); for(i=0;i<N;i++) for(j=0;j<N;j++) if(j>i) {

E[k][0]=i; E[k][1]=j; E[k][2]=w[i][j]; k++; } cout<<endl<<"Set of edges - before sort "<<endl; for(i=0;i<M;i++) { for(j=0;j<3;j++) cout<<E[i][j]<<"\t"; cout<<endl; } g.wait(); for(i=M-1;i>0;i--) for(j=0;j<i;j++) if(E[j][2]>E[j+1][2]) { a=E[j][0]; b=E[j][1]; c=E[j][2]; E[j][0]=E[j+1][0]; E[j][1]=E[j+1][1]; E[j][2]=E[j+1][2]; E[j+1][0]=a; E[j+1][1]=b; E[j+1][2]=c; } cout<<endl<<"Set of edges - after sorting "<<endl; for(i=0;i<M;i++) { for(j=0;j<3;j++) cout<<E[i][j]<<"\t"; cout<<endl; } g.wait(); g.initial(N); for(i=0;i<N-1;i++) for(j=0;j<3;j++) F[i][j]=-1; g.testuniv(); while(numedges<N-1) { a=E[nextedges][0]; b=E[nextedges][1]; i=g.find(a);

j=g.find(b); if(!g.equal(i,j)) { g.merge(i,j); F[numedges][0]=E[nextedges][0]; F[numedges][1]=E[nextedges][1]; F[numedges][2]=E[nextedges][2]; numedges++; g.testuniv(); } nextedges++; } cout<<"\n Minimal Spanning Tree Edges: "<<endl; cout<<"F =("; for(i=0;i<N-1;i++) { cout<<"(V"<<F[i][0]<<","<<"V"<<F[i][1]<<")"; if(i<N-2) cout<<","; weight=weight+F[i][2]; } cout<<")"<<endl; cout<<"Minimal Spanning Tree Weight ="<<weight<<endl; getch(); return 0; }

OUTPUT: Set of edges before sort 0 1 2 0 2 4 0 3 1

0 4 3 0 5 2 1 2 1 1 3 1 1 4 4 1 5 4 2 3 4 2 4 6 2 5 0 3 4 0 3 5 0 4 5 0 Press Enter to continue . . . Set of edges - after sorting 2 5 0 3 4 0 3 5 0 4 5 0 0 3 1 1 2 1 1 3 1 0 1 2 0 5 2 0 4 3 0 2 4 1 4 4 1 5 4 2 3 4 2 4 6 Press Enter to continue . . .

The disjoint subsets are : 0 1 2 3 4 5 The disjoint subsets are : 0 1 2 3 4 2 The disjoint subsets are : 0 1 2 3 3 2 The disjoint subsets are : 0 1 2 2 3 2

The disjoint subsets are : 0 1 0 2 3 2 The disjoint subsets are : 0 0 0 2 3 2 Minimal Spanning Tree Edges: F =((V2,V5),(V3,V4),(V3,V5),(V0,V3),(V1,V2)) Minimal Spanning Tree Weight =2

RESULT: The program to develop the method to find a minimum cost spanning tree in graph by using Kruskals Algorithm in graph class. Graph class is derived publicly from the point class and Arc class is developed, executed sucessfully.

EX. NO. 9 AIM:

To develop the program 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. ALGORITHM: 1. Open the program, include required header files. 2. Create the base class shape, declare and define the virtual function void show( ) in public access specifier, this function prototype is using by all the intermediate base class and derived class. Close the base class. 3. From the base class point, two derived class, shape, rectangle is publicly derived. 4. In both this derived class void show( ) method is declared and defined. 5. From the derived class shape, two other derived class, square, circle is publicly derived. So the derived class, shape becomes intermediate base class. 6. In both this derived class, square, circle declare and define void show( ) method. 7. From the derived class rectangle, two other derived class, ellipse, triangle is publicly derived. So the derived class, rectangle becomes intermediate base class. 8. In both this derived class, ellipse, triangle declare and define void show( ) method.

9. From the derived class square, two other derived class, polygon, oval is publicly derived. So the derived class, square becomes intermediate base class. 10. In both this derived class, polygon, oval declare and define

void show( ) method. 10. Open the main( ) function, declare object, pointer object of the base class point and declare object of all the intermediate base class and derived class. 11. Assign the address of the object of base class to the base class pointer object. And show base class pointer object pointing which object by typeid operator with name( ) function. Call the show( ) method by base class pointer object. 12. Assign object address of the intermediate base class, derived class to the base class pointer object. And show base class pointer object pointing which object by typeid operator with name( ) function. Call the show( ) method by base class pointer object. 14. Close the main( ) function. Stop the program.

PROGRAM: #include<iostream.h> #include<typeinfo.h> #include<conio.h> class point

{ public: virtual void show() { cout<<"Point is drawn "<<endl<<endl; } }; class shape:public point { public: void show() { cout<<"Shape is drawn "<<endl<<endl; } }; class rectangle:public point { public: void show() { cout<<" Rectangle is drawn "<<endl<<endl; } }; class square:public shape { public: void show() { cout<<" Square is drawn "<<endl<<endl; } };

class circle:public shape { public: void show() { cout<<" Circle is drawn "<<endl<<endl; } }; class triangle:public rectangle {

public: void show() { cout<<" Triangle is drawn "<<endl<<endl; } }; class ellipse:public rectangle { public: void show() { cout<<" Ellipse is drawn "<<endl<<endl; } }; class polygon:public square { public: void show() { cout<<" Polygon is drawn "<<endl<<endl; } }; class oval:public square { public: void show() { cout<<" Oval is drawn "<<endl<<endl; } };

void main() { point *pointptr,p; shape sh;rectangle rt;square sq;circle cr; triangle tr;ellipse ell; polygon pol;oval ov;clrscr(); pointptr=&p; cout<< Base class pointer object now pointing <<typeid(*pointptr).name( ) <<endl; pointptr->show( ); pointptr=&sh; cout<< Base class pointer object now pointing <<typeid(*pointptr).name( ) <<endl; pointptr->show( );

pointptr=&rt; cout<< Base class pointer object now pointing <<typeid(*pointptr).name( ) <<endl; pointptr->show( ); pointptr=&sq; cout<< Base class pointer object now pointing <<typeid(*pointptr).name( ) <<endl; pointptr->show( ); pointptr=&cr; cout<< Base class pointer object now pointing <<typeid(*pointptr).name( ) <<endl; pointptr->show( ); pointptr=&ell; cout<< Base class pointer object now pointing <<typeid(*pointptr).name( ) <<endl; pointptr->show( ); pointptr=&tr; cout<< Base class pointer object now pointing <<typeid(*pointptr).name( ) <<endl; pointptr->show( ); pointptr=&pol; cout<< Base class pointer object now pointing <<typeid(*pointptr).name( ) <<endl; pointptr->show( ); pointptr=&ov; cout<< Base class pointer object now pointing <<typeid(*pointptr).name( ) <<endl; pointptr->show( ); getch(); }

OUTPUT: Base class pointer object now pointing class point Point is drawn Base class pointer object now pointing class shape

Shape is drawn Base class pointer object now pointing class rectangle Rectangle is drawn Base class pointer object now pointing class square Square is drawn Base class pointer object now pointing class circle Circle is drawn Base class pointer object now pointing class ellipse Ellipse is drawn Base class pointer object now pointing class triangle Triangle is drawn Base class pointer object now pointing class polygon Polygon is drawn Base class pointer object now pointing class oval Oval is drawn RESULT: Thus the program 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 is successfully developed and executed.

EX. NO. 10 AIM: To develop a program for reading complex numbers and writes them two per line in a file along with an operator. Read the complex numbers from the file and perform the corresponding operations.

ALGORITHM: 1. Open the program, include required header files. 2. Declare the class, complex. In public access specifier declare two float variables real, imag. Declare and define the default constructor. 3. After the default constructor, declare and define write method, this method as output file stream class object reference as argument and no return type. 4. Next declare and define read method, this as input file stream class object reference as argument, integer value as a return type. 5. Next declare operator function, operator<< as friend function with two argument one is reference of object of output file stream class, another one is reference of the object of the complex class. And it returns reference of object of output file stream class. Define this method outside the class. 6. Next declare operator function, operator>> as friend function with two argument one is reference of object of input file stream class, another one is reference of the object of the complex class. And it returns reference of object of input file stream class. Define this method outside the class.

7. Next declare and define another operator function, operator<< as friend function with two argument one is reference of object of output file stream class, another one is reference of the object of the complex class. And it returns reference of object of output file stream class. 8. Next declare and define another operator function, operator>> as friend function with two argument one is reference of object of input

file stream class, another one is reference of the object of the complex class. And it returns reference of object of input file stream class. 9. Next declare Add function as friend function. With two arguments and no return value. The two arguments are object of the class complex. Define this function outside the class. Close the class. 10. Open the main function, declare variables, objects required. Create the file complex.txt with constructor of output file stream class. 11. Enter two complex numbers into the file by using do-while loop. 12. Close the file. 13. Get the complex numbers given to the file complex.txt by opening the file by input file stream class. 14. Call the add( ) function with two objects as argument of the complex class to do the addition of the two complex numbers. 15. Close the program.

PROGRAM: #include<iostream.h> #include<conio.h> #include<fstream.h> class complex { public:

float real,imag; complex() { real=0;imag=0; } void write(ofstream &os) { os.write((char *)&real,sizeof(real)); os.write((char *)&imag,sizeof(imag)); } int read(ifstream &is) { is.read((char *)&real,sizeof(real)); is.read((char *)&imag,sizeof(imag)); 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<<endl;

return os } void add(complex c1,complex c2) { cout<<"Result is "<<endl; cout<<(c1.real+c2.real)<<" +i "<<(c1.imag+c2.imag); } void main() { complex pobj; clrscr(); ofstream ofile("Complex.txt",ios::out); char ch; cout<<"Enter two complex numbers "<<endl; int i=0; do { cin>>pobj; ofile<<pobj; i++; } while(i!=2) ofile.close(); ifstream ifile("complex.txt",ios::in); cout<<" Objects written to the file are..."<<endl; complex c1,c2; i=0;

while(1) { ifile>>pobj; if(i==0) c1=pobj; else c2=pobj; i++; if(ifile.fail()) break; cout<<pobj; } add(c1,c2) getch();

OUTPUT: Enter 2 complex numbers Real? 1.3 Imag?: 1.4 Real? 4.3 Imag?: 2.3 objects written to the file were 1.3 +i 1.4 4.3 +i 2.3 Result is 5.6 +i 3.7

RESULT: The program for reading complex numbers and writes them two per line in a file along with an operator. Read the complex numbers from the file and perform the corresponding operations is successfully developed, executed.

Das könnte Ihnen auch gefallen