You are on page 1of 49

CR ENGINEERING COLLEGE

Ex. No 1

Implementation of Static Data member, Default Argument and Friend Function

Aim To Write a C++ program for Students mark analysis using Static Data member, Default Argument and Friend Function. Algorithm Step 1: Start the Process. Step 2: Get the Students detail and calculate the Result analysis. Step 3: Default value is assigned for pass in a function and the function is assigned with default value when value for pass is not given in checking for pass status. Step 4: Declare the integer count as Static member each time when the student is pass it will be incremented and finally specify pass count. Step 5: The friend function display is used to access the private variables from two classes and acting as the bridge between two classes. Step 6: Stop the Process. Program #include<iostream.h> #include<conio.h> int cp(int,int,int,int=50); void printline(char ch='*',int range= 70); class per; class acc { int rno,tot; static int count; int m1,m2,m3; char name[20],grade; float avg; public: void getdata() { cout<<"Enter id:"; cin>>rno; cout<<"Enter name:"; cin>>name; cout<<"Enter m1"; cin>>m1; cout<<"Enter m2"; cin>>m2; cout<<"Enter m3"; cin>>m3;

DEPT OF CSE OOPS LAB

Page 1

CR ENGINEERING COLLEGE

} friend void display(acc,per); void cal() { tot=0; if(cp(m1,m2,m3)) { cout<<"Result=pass"<<endl; tot=m1+m2+m3; avg=tot/3; count=count+1; if(avg>=90) grade='O'; if(avg>=70&&avg<90) grade='A'; if(avg>=50&&avg<70) grade='B'; } else { cout<<"Result=Fail"<<endl; } } static void showcount(void) { cout<<endl; cout<<"count:"<<count;}}; int acc::count; class per { char add[25]; long tel; public: void getdata1() { cout<<"Enter address"; cin>>add; cout<<"Enter phoneno:"; cin>>tel; } friend void display(acc a,per p) { cout<<endl; cout<<"Name:"<<a.name<<endl; cout<<"ID:"<<a.rno<<endl; cout<<"Address:"<<p.add<<endl; cout<<"Phone no.:"<<p.tel<<endl; cout<<"M1:"<<a.m1<<endl;

DEPT OF CSE OOPS LAB

Page 2

CR ENGINEERING COLLEGE

cout<<"M2:"<<a.m2<<endl; cout<<"M3:"<<a.m3<<endl; if(cp(a.m1,a.m2,a.m3)) { cout<<"Total:"<<a.tot<<endl; cout<<"Average:"<<a.avg<<endl; cout<<"Grade:"<<a.grade<<endl; }}}; int cp(int m1,int m2,int m3,int pass1) { if(m1>=pass1&&m2>=pass1&&m3>=pass1) return(1); else return(0); } void main() { int i,n; clrscr(); per p1[20]; acc a1[20]; cout<<"Enter no of students:"; cin>>n; for(i=0;i<n;i++) { a1[i].getdata(); p1[i].getdata1(); a1[i].cal(); } for(i=0;i<n;i++) { printline('-'); display(a1[i],p1[i]); printline(); } acc::showcount(); getch(); } void printline(char ch,int range) { int i; cout<<endl; for(i=0;i<range;i++) cout<<ch;} Output Enter no of students:2

DEPT OF CSE OOPS LAB

Page 3

CR ENGINEERING COLLEGE

Enter id:2 Enter name:rahul Enter m1:78 Enter m2:90 Enter m3:67 Enter address:coimbatore Enter phoneno:230000 Result=pass Enter id:5 Enter name:aravind Enter m1:56 Enter m2:45 Enter m3:78 Enter address:madurai Enter phoneno:456789 Result=Fail ----------------------------------------------------------Name:rahul ID:2 Address:coimbatore Phone no.:230000 M1:78 M2:90 M3:67 Total:235 Average:78 Grade:A *********************************************************** ----------------------------------------------------------Name:aravind ID:5 Address:madurai Phone no.:456789 M1:56 M2:45 M3:78 *********************************************************** count:1 Result Thus the program to perform Students mark analysis using Static Data member, Default Argument and Friend Function was implemented.

DEPT OF CSE OOPS LAB

Page 4

CR ENGINEERING COLLEGE

Ex. No 2

Implementation of complex number class with operator overloading and type conversions

Aim To write a C++ program to implement complex number class with operator overloading and type conversions such as integer to complex, double to complex, complex to double. Algorithm Step 1: Start the program. Step 2: Create a class with necessary data members and member functions. Step 3: Create a constructor to initialize the variables. Step 4: Perform addition of two numbers overloading the necessary operators. Step 5: Create an object for the Complex class and call the required functions to perform the action. Step 6: Perform the type conversion from double to complex, integer to complex and complex to double. Step 7: Display the result. Step 8: Stop the program. Program #include<iostream.h> #include<conio.h> #include<math.h> class complex { float real,img,temp; public: complex() { real=img=0; } complex(int a) { real=a; img=0; } complex(double a1) { real=a1; img=0.0; } void outdata() { if(img>=0.0) {

DEPT OF CSE OOPS LAB

Page 5

CR ENGINEERING COLLEGE

cout<<real<<"+"<<img<<"i"; } else { cout<<real<<img<<"i"; } } complex operator+(complex c) { complex temp; temp.real=real+c.real; temp.img=img+c.img; return(temp); } complex operator-(complex c) { complex temp1; temp1.real=real-c.real; temp1.img=img-c.img; return(temp1); } complex operator*(complex c) { complex temp2; temp2.real=real*c.real-img*c.img; temp2.img=real*c.img+img*c.real; return(temp2); } complex operator/(complex c) { complex temp3; temp3.real=(((real*c.real)+(img*c.img))/((c.real*c.real)+(c.img*c.img))); temp3.img=(((img*c.real)-(real*c.img))/((c.real*c.real)+(c.img*c.img))); return(temp3); } operator double() { double magnitude; magnitude=sqrt(pow(real,2)+pow(img,2)); return magnitude; } }; void main() { clrscr(); complex c1,c2,c3,c4,c5,c6; int real; double real1; cout<<"Enter the real number"; cin>>real;
DEPT OF CSE OOPS LAB Page 6

CR ENGINEERING COLLEGE

c1=real; cout<<"Integer to complex conversion"<<endl; cout<<"Enter the real number"; cin>>real1; c2=real1; cout<<"Double to complex conversion"<<endl; c3=c1+c2; c4=c1-c2; c5=c1*c2; c6=c1/c2; cout<<"\n\n"; cout<<"addtion result is:"; c3.outdata(); cout<<"\n\n"; cout<<"subraction result is:"; c4.outdata(); cout<<"\n\n"; cout<<"multiplication result is:"; c5.outdata(); cout<<"\n\n"; cout<<"division result is:"; c6.outdata(); cout<<"Conversion from complex to double"<<endl; double mag=c3; cout<<"Magnitude of a complex number"<<mag; getch(); } Output Enter the real number:2 Integer to complex conversion Enter the real number:2.0 Double to complex conversion addtion result is:4+0i subraction result is:0+0i multiplication result is:4+0i division result is:1+0i Conversion from complex to double Magnitude of a complex number:4 Result Thus the program to implement complex number class with operator overloading and type conversions such as integer to complex, double to complex, complex to double is implemented.

DEPT OF CSE OOPS LAB

Page 7

CR ENGINEERING COLLEGE

Ex. No 3 Aim

Implement the Matrix Class using Constructor, Destructor,Copy Constructor, Overloading assignment operator

To Write a C++ program to Implement Matrix Class using Constructor, Destructor, Copy Constructor, Overloading assignment operator Algorithm Step 1: Start the Process. Step 2: Create the class name as MATRIX. Step 3: Declare the data member and member function. Step 4: Declare constructor, destructor and copy constructor Step 5: Display the result. Step 6: Stop the process. Program #include<iostream.h> #include<conio.h> class matrix { private: int row; int col; int **p; public: matrix() //Constructor { row=col=0; p=NULL; } matrix(int r,int c); ~matrix(); //Destructor void read(); void show(); void add(matrix &a,matrix &b); void sub(matrix &a,matrix &b); void operator =(matrix &m1) { row=m1.row; col=m1.col; p=new int *[m1.row]; //dynamic allocation for(int i=0;i<m1.row;i++) { p[i]=new int[m1.col];//dynamic allocation } } matrix(matrix &m2)//copy construtor
DEPT OF CSE OOPS LAB Page 8

CR ENGINEERING COLLEGE

{ row=m2.row; col=m2.col; p=new int *[m2.row]; //dynamic allocation for(int i=0;i<m2.row;i++) { p[i]=new int[m2.col];//dynamic allocation } for(i=0;i<m2.row;i++) for(int j=0;j<m2.col;j++) { p[i][j]=m2.p[i][j]; } } }; matrix::matrix(int r,int c) { row=r; col=c; p=new int *[row]; //dynamic allocation for(int i=0;i<row;i++) p[i]=new int[col];//dynamic allocation } matrix::~matrix() { for(int i=0;i<row;i++) delete p[i]; delete p; } void matrix::add(matrix &a,matrix &b) { int i,j; row=a.row; col=b.col; for(i=0;i<row;i++) for(j=0;j<col;j++) p[i][j]=a.p[i][j]+b.p[i][j]; } void matrix::sub(matrix &a,matrix &b) { int i,j; row=a.row; col=b.col; for(i=0;i<row;i++) for(j=0;j<col;j++) p[i][j]=a.p[i][j]-b.p[i][j]; }

DEPT OF CSE OOPS LAB

Page 9

CR ENGINEERING COLLEGE

void matrix::read() { int i,j; for( i=0;i<row;i++) for( j=0;j<col;j++) { cout<<"Matrix["<<i<<","<<j<<"]="; cin>>p[i][j]; } } void matrix::show() { int i,j; for(i=0;i<row;i++) { cout<<endl; for( j=0;j<col;j++) { cout<<p[i][j]<<"\t"; } } } void main() { int m,n,p,g,q; clrscr(); cout<<"Enter the A matrix"<<endl; cout<<"How many rows?"; cin>>m; cout<<"How many col?"; cin>>n; matrix a(m,n); a.read(); matrix b; b=a;//overloading assignment operator cout<<Overloading assignment operator Invoked<<endl; cout<<Enter the B matrix; b.read(); cout<<"Matrix A is .."; a.show(); cout<<endl<<"Matrix B is.."; b.show(); matrix c(m,n);

DEPT OF CSE OOPS LAB

Page 10

CR ENGINEERING COLLEGE

c.add(a,b); cout<<endl<<"c=a+b.."; c.show(); matrix d(m,n); d.sub(a,b); cout<<endl<<"d=a-b.."; d.show(); cout<<"\nCopy constructor invoked"<<endl; matrix e(d);//Copy Constructor e.show(); getch(); } Output Enter the A matrix How many rows?2 How many col?2 Matrix[0,0]=2 Matrix[0,1]=2 Matrix[1,0]=2 Matrix[1,1]=2 Overloading assignment operator Invoked Enter the B matrix Matrix[0,0]=1 Matrix[0,1]=1 Matrix[1,0]=1 Matrix[1,1]=1 Matrix A is .. 2 2 2 2 Matrix B is.. 1 1 1 1 c=a+b.. 3 3 3 3 d=a-b.. 1 1 1 1 Copy constructor invoked 1 1 1 1

Result Thus the C++ program to Implement Matrix Class using Constructor, Destructor, Copy Constructor, Overloading assignment operator is implemented.

DEPT OF CSE OOPS LAB

Page 11

CR ENGINEERING COLLEGE

Ex. No 4 Overloading of new and delete operators for addition of vector elements Aim To write a C++ program to Overload the new and delete operators for addition of vector elements to provide custom dynamic allocation of memory. Algorithm Step 1: Start the program. Step 2: Declare the necessary function prototype. Step 3: Define void *operator new (), void operator delete () functions to allocate storage and to delete the memory with the help of malloc and free functions respectively. Step 4: read () and sum () functions to get and calculate the vector addition. Step 5: Stop the program. Program #include<iostream.h> const int asize=10; class vector { int *a; public: void * operator new(size_t ) { vector *v; v=::new vector; v->a=new int[asize]; return v; } void operator delete(void *vec) { vector *v; v=(vector *)vec; delete(int*) v->a; ::delete vec; } void read() { for(int i=0;i<asize;i++) { cin>>a[i]; } } int sum()

DEPT OF CSE OOPS LAB

Page 12

CR ENGINEERING COLLEGE

{ int sum=0; for(int i=0;i<asize;i++) { sum+=a[i]; } return sum; } }; void main() { vector *v=new vector; cout<<Enter the vector elements; v->read(); cout<<the sum of vector elements<<v->sum(); delete v; } Output Enter the vector elements 10 10 10 10 10 10 10 10 10 10 The sum of vector elements: 100

Result Thus the program to Overload the new and delete operators for addition of vector elements to provide custom dynamic allocation of memory is implemented.

DEPT OF CSE OOPS LAB

Page 13

CR ENGINEERING COLLEGE

Ex. No 5

Implementation of Template for Linked List Class with necessary methods

Aim To write a C++ program to implement the template of linked list class. Algorithm 1.start the program 2.create the class list and declare the data members 3.create node with template based variable and link field 4.Then insert the data and display using friend function 5.Then get the list and finally display.

Program #include<iostream.h> #include<stdlib.h> template <class T> class List { private: T data; List *next; public: List(); List(T dat); int getData() {

DEPT OF CSE OOPS LAB

Page 14

CR ENGINEERING COLLEGE

return data; } void insertData(List<T> *); friend void displayData(List<T> *); }; template<class T> List<T>::List() { data=0; next=NULL; } template<class T> List<T>::List(T dat) { data=dat; next=NULL; } template<class T> void List<T>::insertData(List<T> *node) { List<T> *last=this; while(last->next) { last=last->next; } last->next=node; } template<class T>
DEPT OF CSE OOPS LAB Page 15

CR ENGINEERING COLLEGE

void displayData(List<T> *first) { List<T> *start; cout<<"List elements are: "; for(start=first;start;start=start->next) { cout<<start->data<<"\t"; } cout<<endl; } int main() { int choice; int data; List<int> *first=NULL; List<int> *node; while(1) { cout<<"Linked list"<<endl; cout<<"1. Insert"<<endl; cout<<"2. Display"<<endl; cout<<"3. Exit"<<endl; cout<<"Enter your choice :"; cin>>choice; switch(choice) {

DEPT OF CSE OOPS LAB

Page 16

CR ENGINEERING COLLEGE

case 1: cout<<"Enter a data :"; cin>>data; node=new List<int>(data); if(first==NULL) { first=node; } else { first->insertData(node); } break; case 2: displayData(first); break; case 3: exit(1); default: cout<<"Incorrect option"<<endl; continue; } } } Result Thus the program to implement the template of linked list class is implemented.
DEPT OF CSE OOPS LAB Page 17

CR ENGINEERING COLLEGE

Ex. No 6

Generating Templates for standard sorting algorithms

Aim To write a C++ program to implement the Templates of standard sorting algorithms such as bubble sort, insertion sort, merge sort, and quick sort Algorithm
Step 1: Start the Process. Step 2: Create the template to handle bubble sort, insertion sort, and quick sort, merge sort. Step 3: Get the element using for loop. Step 4: Call the template function for sorting. Step 5: Display the result. Step 6: Stop the process.

Program: MERGE SORT: #include<iostream.h> #include<conio.h> template<class T> class Sort { private: int n,l,r; T *Array; T *b; public: Sort(); void get(); void callMerge(); void mergeSort(T a[],int l,int r); void merge(T c[],T d[],int l,int m,int r); void copy(T b[],T a[],int l,int r); void show(); ~Sort(); };
DEPT OF CSE OOPS LAB Page 18

CR ENGINEERING COLLEGE

template <class T> Sort<T>::Sort() { cout<<"\nEnter the size of the array:"; cin>>n; Array=new T[n]; b=new T[n]; l=0; r=n-1; } template <class T> void Sort<T>::get() { cout<<"\nEnter the Elements:\n"; for(int i=0;i<n;i++) cin>>Array[i]; } template <class T> void Sort<T>::callMerge() { (*this).mergeSort(Array,l,r); } template <class T> void Sort<T>::mergeSort(T a[],int l,int r) { if(l<r) { int i=(l+r)/2; mergeSort(a,l,i); mergeSort(a,i+1,r); merge(a,b,l,i,r); copy(b,a,l,r); } } template <class T> void Sort<T>::merge(T c[],T d[],int l,int m,int r) { int i=l,j=m+1,k=l; while((i<=m) && (j<=r)) if(c[i]<=c[j]) d[k++]=c[i++]; else d[k++]=c[j++]; if(i>m) for(int q=j;q<=r;q++)
DEPT OF CSE OOPS LAB Page 19

CR ENGINEERING COLLEGE

d[k++]=c[q]; else for(int q=i;q<=m;q++) d[k++]=c[q]; } template <class T> void Sort<T>::copy(T b[],T a[],int l,int r) { for(int i=l;i<=r;i++) a[i]=b[i]; } template <class T> void Sort<T>::show() { cout<<"\nThe Elements in the Sorted Array:\t"; for(int i=0;i<n;i++,cout<<"\t") cout<<Array[i]; } template <class T> Sort<T>::~Sort() { delete b; delete Array; } void main() { clrscr(); cout<<"\n\t\t\t************\n"; cout<<"\n\t\t\tInteger Sort\n"; cout<<"\n\t\t\t************\n"; Sort<int> obj; obj.get(); obj.callMerge(); obj.show(); cout<<"\n"; cout<<"\n\t\t\t**********\n"; cout<<"\n\t\t\tFloat Sort\n "; cout<<"\n\t\t\t**********\n"; Sort<float> obj2; obj2.get(); obj2.callMerge(); obj2.show(); cout<<"\n";
DEPT OF CSE OOPS LAB Page 20

CR ENGINEERING COLLEGE

cout<<"\n\t\t\t************\n"; cout<<"\n\t\t\tCharater Sort\n "; cout<<"\n\t\t\t************\n"; Sort<char> obj3; obj3.get(); obj3.callMerge(); obj3.show(); getch(); }

OUTPUT:
************ Integer Sort ************ Enter the size of the array:5 Enter the Elements: 23 11 1 67 2 The Elements in the Sorted Array: 1 2 11 23 67 ********** Float Sort ********** Enter the size of the array:5 Enter the Elements: 2.3 1.1 6.7 4.5 9.9 The Elements in the Sorted Array: 1.1 2.3 4.5 6.7 9.9 *********** Charater Sort *********** Enter the size of the array:5 Enter the Elements: w q a s b The Elements in the Sorted Array: a b q s w

DEPT OF CSE OOPS LAB

Page 21

CR ENGINEERING COLLEGE

QUICK SORT: #include<iostream.h> #include<conio.h> #include<process.h> template<class T> class Sort { private: int n,lb,ub; T *a; public: void get(); void callquick(); void quick(int lb,int ub); void show(); ~Sort(); }; template <class T> void Sort<T>::get() { cout<<"\nEnter the size of the array:"; cin>>n; a=new T[n]; cout<<"\nEnter the Elements:"; for(int i=0;i<n;i++) cin>>a[i]; lb=0; ub=n-1;

}
DEPT OF CSE OOPS LAB Page 22

CR ENGINEERING COLLEGE

template <class T> void Sort<T>::callquick() { (*this).quick(lb,ub); } template <class T> void Sort<T>::quick(int lb,int ub) { T temp=0; T flag=1,i=0,j=0,key=0; if(lb<ub) { i=lb; j=ub+1; key=a[lb]; while(flag==1) { i=i+1; while(a[i]<key) i=i+1; j=j-1; while(a[j]>key) j=j-1; if(i<j) { temp=a[i];

DEPT OF CSE OOPS LAB

Page 23

CR ENGINEERING COLLEGE

a[i]=a[j]; a[j]=temp; } else flag=0; } temp=a[lb]; a[lb]=a[j];a[j]=temp; show(); quick(lb,j-1); quick(j+1,ub); } } template <class T> void Sort<T>::show() { cout<<"\nThe Elements in the Array:\t"; for(int i=0;i<n;i++,cout<<"\t") cout<<a[i]; } template <class T> Sort<T>::~Sort() { delete a; }

DEPT OF CSE OOPS LAB

Page 24

CR ENGINEERING COLLEGE

void main() { clrscr(); Sort<int>obj; Sort<float>obj2; Sort<char>obj3; int w; do { cout<<"\n\t\t***************\n"; cout<<"\n\t\t QUICK SORT\n"; cout<<"\n\t\t***************\n"; cout<<"\n\t\t1.Integer Sort\n\t\t2.Float Sort\n\t\t3.Character Sort\n\t\t4.Exit\n"; cout<<"\n\t\tEnter Ur choice:"; cin>>w; switch(w) { case 1: obj.get(); obj.callquick(); break; case 2: obj2.get(); obj2.callquick(); break; case 3:
DEPT OF CSE OOPS LAB Page 25

CR ENGINEERING COLLEGE

obj3.get(); obj3.callquick(); break; case 4: exit(0); } }while(w!=4); getch(); } Output:


*************** QUICK SORT *************** 1.Integer Sort 2.Float Sort 3.Character Sort 4.Exit Enter Ur choice:1 Enter the size of the array:5 Enter the Elements:23 45 11 78 1 The Elements in the Array: 11 1 23 78 45 The Elements in the Array: 1 The Elements in the Array: 1 11 23 78 45 11 23 45 78

Enter Ur choice:3 Enter the size of the array:5 Enter the Elements:r s k a q

The Elements in the Array: a q

r r r s

s s

The Elements in the Array: a q k The Elements in the Array: a k q

DEPT OF CSE OOPS LAB

Page 26

CR ENGINEERING COLLEGE

Enter Ur choice:2 Enter the size of the array:5 Enter the Elements:2.2 4.5 1.1 7.8 0.1

The elements in the array:1.1 0.1 2.2 7.8 The elements in the array:0.1 The elements in the array:0.1
*************** QUICK SORT *************** 1.Integer Sort 2.Float Sort 3.Character Sort 4.Exit

4.5

1.1 2.2 7.8 4.5 1.1 2.2 4.5 7.8

Enter Ur choice:4

BUBBLE SORT: #include<iostream.h> #include<conio.h> template <class T> class Bubble { private: T a[10]; int size; public: Bubble();

DEPT OF CSE OOPS LAB

Page 27

CR ENGINEERING COLLEGE

void getdata(); void showdata(); void sortdata(); }; template <class T> Bubble<T>::Bubble() { } template <class T> void Bubble<T>::getdata() { cout<<"Enter the size of the array"; cin>>size; cout<<"Enter"<<size<<"elements"; for(int i=0;i<size;i++) { cin>>a[i]; } } template<class T> void Bubble<T>::showdata() { cout<<"The array elements are"<<endl; for(int i=0;i<size;i++) {

DEPT OF CSE OOPS LAB

Page 28

CR ENGINEERING COLLEGE

cout<<a[i]<<"\t"; } cout<<endl; } template <class T> void Bubble<T>::sortdata() { T temp; for(int i=0;i<size-1;i++) { for(int j=i+1;j<size;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } } int main() { Bubble<int>b1; b1.getdata();

b1.showdata();
DEPT OF CSE OOPS LAB Page 29

CR ENGINEERING COLLEGE

b1.sortdata(); cout<<"After sorting"; b1.showdata(); Bubble<float>b2; b2.getdata(); b2.showdata(); b2.sortdata(); cout<<"After sorting"; b2.showdata(); getch(); } Output: Enter the size of the array: 5 Insert 5 elements: 3 4 2 7 5 The array elements are: 3 After sorting The array elements are: 2 Enter the size of the array:4 3.3 8.1 1.2 5.4 3 4 5 7 4 2 7 5

DEPT OF CSE OOPS LAB

Page 30

CR ENGINEERING COLLEGE

The array elements are 3.3 8.1 1.2 1.54

After sorting the array elements are: 1.2 3.3 5.4 8.1 INSERTION SORT: #include<iostream.h> #include<conio.h> template<class T> void insertionsort(T arr[],T length) { T i,j,tmp; for(i=1;i<length;i++) { j=i; while(j>0&&arr[j-1]>arr[j]) { tmp=arr[j]; arr[j]=arr[j-1]; arr[j-1]=tmp; j--; } } }

void main()
DEPT OF CSE OOPS LAB Page 31

CR ENGINEERING COLLEGE

{ int num[25]; int i,size; clrscr(); cout<<"Program to sort elements using insertion sort"<<endl; cout<<"Enter size of elements<max-25>"<<endl; cin>>size; cout<<"Enter elements"<<endl; for(i=0;i<size;i++) cin>>num[i]; insertionsort(num,size); cout<<"Sorted elements are"<<endl; for(i=0;i<size;i++) { cout<<num[i]; cout<<"\t"; } getch(); }

Output: Enter the size of elements: 4 Enter elements: 2 6 Sorted elements are:2 3 8 3 6 8

DEPT OF CSE OOPS LAB

Page 32

CR ENGINEERING COLLEGE

Result Thus the program to implement the Templates of standard sorting algorithms such as bubble sort, insertion sort, merge sort, and quick sort is implemented.

DEPT OF CSE OOPS LAB

Page 33

CR ENGINEERING COLLEGE

Ex. No 7a

Design of Stack classes with necessary Exception handling

Aim To write a C++ program to implement the stack class with necessary exception handling. Algorithm Step 1: Start the Process Step 2: Create the class for stack. Step 3: Declare the member function for push and pop operation. Step 4: Push operation to insert the element. Step 5: Pop operation to delete the element. Step 6: Stop the Process. Program #include<iostream.h> #define MAX 5 class stack { protected: int arr[MAX]; public: int item; int top; stack() { top=0; } class overflowException{}; void push(int a) { cout<<top; if(top<MAX) { top++; arr[top]=a; } else {throw overflowException(); } } class underflowException{}; int pop()

{ if(top==0) {
DEPT OF CSE OOPS LAB Page 34

CR ENGINEERING COLLEGE

throw underflowException(); } else { int data=arr[top]; top--; return data; } } }; int main() { char ch; int choice; stack a; do { cout<<1.Push\n2.Pop<<end1; cout<<"Enter the choice"; cin>>choice; switch(choice) { case 1: try { cout<<"enter the item"; cin>>a.item; a.push(a.item); cout<<Item pushed; } catch(stack::overflowException ) { cout<<"Stack overflow"; } break; case 2: try { cout<<Item Popped:<<a.pop(); } catch(stack::underflowException) {

cout<<"Stack is empty"; } break; } cout<<"do u want to continue";


DEPT OF CSE OOPS LAB Page 35

CR ENGINEERING COLLEGE

cin>>ch; } while(ch=='y'); return 0; } Output 1.Push 2.Pop Enter the choice:1 enter the item:2 Item pushed do u want to continue Y 1.Push 2.Pop Enter the choice:1 enter the item:3 Item pushed do u want to continue Y 1.Push 2.Pop Enter the choice:2 Item Popped:3 do u want to continue n

Result Thus the program to implement the stack class with necessary exception handling is implemented

DEPT OF CSE OOPS LAB

Page 36

CR ENGINEERING COLLEGE

Ex. No 7b

Designing of Queue classes with necessary Exception handling

Aim To write a C++ program to implement the Queue class with necessary exception handling. Algorithm Step 1: Start the Process Step 2: Create a class for queue. Step 3: Declare the member function for front and rear operation. Step 4: Provide necessary exception handling for stack and queue. Step 5: Display the result. Step 6: Stop the Process Program #include<iostream.h> class que { int size; int arr[5]; int rear,front; int data; public: que() { size=5; rear=-1; front=-1; } class quefullException{}; void enque(int data) { if(rear+1>=size) { throw quefullException(); } else { if(rear==-1&&front==-1) { front++; } rear++; cout<<"rear"<<rear; arr[rear]=data; cout<<arr[rear];

DEPT OF CSE OOPS LAB

Page 37

CR ENGINEERING COLLEGE

} } class queEmptyException{}; int deque() { if(front>rear) { throw queEmptyException(); } return(arr[front++]); } }; int main() { que q; char choice; do { Cout<<1.Insert\n2.Delete<<endl; cout<<"enter the choice:"; int ch; cin>>ch; switch(ch) { case 1: try { cout<<"enter the element to insert"; int item; cin>>item; q.enque(item); cout<<Item inserted; } catch(que::quefullException) { cout<<"Queue is full"; } break; case 2: try { cout<<"the deleted item from queue"<<q.deque(); } catch(que::queEmptyException) { cout<<"queue is empty"; }

break;
DEPT OF CSE OOPS LAB Page 38

CR ENGINEERING COLLEGE

} cout<<"continue(y/n)"; cin>>choice; }while(choice=='y'); } Output 1.Insert 2.Delete Enter the choice:1 enter the element to insert :2 Item inserted continue (y/n)y 1.Insert 2.Delete Enter the choice:1 enter the element to insert :3 Item inserted continue (y/n)y 1.Insert 2.Delete Enter the choice:2 the deleted item from queue:2 continue (y/n)n

Result Thus the program to implement the Queue class with necessary exception handling is implemented.

DEPT OF CSE OOPS LAB

Page 39

CR ENGINEERING COLLEGE

Ex. No 8 Aim

Implementation of Graph for calculating Minimum Spanning Tree

To Write a C++ Program to implement the graph and to obtain a minimum cost spanning tree. Algorithm Step 1: Start the Process. Step 2:.Create a class to get the edges and cost of the graph. Step 3: Define a Graph class which represents the collection of Point and Arc objects. Step 4: Write a method to find a minimum cost spanning tree in a graph. Step 5: Stop the Process. Program #include<iostream.h> #include<conio.h> class kruskal { private: int n; //no of nodes int noe; //no edges in the graph int graph_edge[100][4]; int tree[10][10]; int sets[100][10]; int top[100]; public: void read_graph(); void initialize_span_t(); void sort_edges(); void algorithm(); int find_node(int ); void print_min_span_t(); }; void kruskal::read_graph() { cout<<*************************************************\n <<This program implements the kruskal algorithm\n <<*************************************************\n; cout<<Enter the no. of nodes in the undirected weighted graph ::; cin>>n; noe=0; cout<<Enter the weights for the following edges ::\n; for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) {
DEPT OF CSE OOPS LAB Page 40

CR ENGINEERING COLLEGE

cout<< < <<i<< , <<j<< > ::; int w; cin>>w; if(w!=0) { noe++; graph_edge[noe][1]=i; graph_edge[noe][2]=j; graph_edge[noe][3]=w; } } } // print the graph edges cout<<\n\nThe edges in the given graph are::\n; for(i=1;i<=noe;i++) cout<< < <<graph_edge[i][1] << , <<graph_edge[i][2] << > ::<<graph_edge[i][3]<<endl; } void kruskal::sort_edges() { /**** Sort the edges using bubble sort in increasing order**************/ for(int i=1;i<=noe-1;i++) { for(int j=1;j<=noe-i;j++) { if(graph_edge[j][3]>graph_edge[j+1][3]) { int t=graph_edge[j][1]; graph_edge[j][1]=graph_edge[j+1][1]; graph_edge[j+1][1]=t; t=graph_edge[j][2]; graph_edge[j][2]=graph_edge[j+1][2]; graph_edge[j+1][2]=t; t=graph_edge[j][3]; graph_edge[j][3]=graph_edge[j+1][3]; graph_edge[j+1][3]=t; } } } // print the graph edges cout<<\n\nAfter sorting the edges in the given graph are::\n; for(i=1;i<=noe;i++) cout<< < <<graph_edge[i][1] << , <<graph_edge[i][2] << > ::<<graph_edge[i][3]<<endl; } void kruskal::algorithm() {// ->make a set for each node for(int i=1;i<=n;i++)

DEPT OF CSE OOPS LAB

Page 41

CR ENGINEERING COLLEGE

{ sets[i][1]=i; top[i]=1; } cout<<\nThe algorithm starts ::\n\n; for(i=1;i<=noe;i++) { int p1=find_node(graph_edge[i][1]); int p2=find_node(graph_edge[i][2]); if(p1!=p2) { cout<<The edge included in the tree is :: << < <<graph_edge[i][1]<< , <<graph_edge[i][2]<< > <<endl<<endl; tree[graph_edge[i][1]][graph_edge[i][2]]=graph_edge[i][3]; tree[graph_edge[i][2]][graph_edge[i][1]]=graph_edge[i][3]; // Mix the two sets for(int j=1;j<=top[p2];j++) { top[p1]++; sets[p1][top[p1]]=sets[p2][j]; } top[p2]=0; } else { cout<<Inclusion of the edge << < <<graph_edge[i][1]<< , <<graph_edge[i][2]<< > <<forms a cycle so it is removed\n\n; } } } int kruskal::find_node(int n) { for(int i=1;i<=noe;i++) { for(int j=1;j<=top[i];j++) { if(n==sets[i][j]) return i; } } return -1; }

int main() {
DEPT OF CSE OOPS LAB Page 42

CR ENGINEERING COLLEGE

clrscr(); kruskal obj; obj.read_graph(); obj.sort_edges(); obj.algorithm(); return 0; getch(); } Output ****************************************** This program implements the kruskal algorithm ************************************************* Enter the no. of nodes in the undirected weighted graph ::3 Enter the weights for the following edges :: < 1 , 2> ::3 < 1 , 3> ::1 < 2 , 3> ::4 The edges in the given graph are:: < 1 , 2 > ::3 < 1 , 3 > ::1 < 2 , 3 > ::4 After sorting the edges in the given graph are:: < 1, 3 > ::1 < 1, 2 > ::3 < 2, 3 > ::4 The algorithm starts :: The edge included in the tree is :: < 1 , 3 >

The edge included in the tree is :: < 1 , 2 >

Inclusion of the edge < 2 , 3 > forms a cycle so it is removed Result Thus the Program to implement the graph and to obtain a minimum cost spanning tree is implemented.

DEPT OF CSE OOPS LAB

Page 43

CR ENGINEERING COLLEGE

Ex. No 9

Implementation of dynamic polymorphism & RTTI

Aim To write a C++ program to implement Hierarchy classes with Dynamic polymorphism and Use Virtual concept along with RTTI Algorithm Step 1: Start the Process. Step 2: Create a class SHAPE with data member and one member function as Virtual. Step 3: Create a derived class square,rectangle,polygon,circle and triangle with SHAPE as base class. Step 4: Create a Object in the main function. Step 5: Invoke the appropriate function using object. Step 6: Finally display the result. Step 7: Stop the Process. Program #include<iostream.h> #include<conio.h> class shape { public: int x,y; public: void getdata(int a,int b=0) { x=a; y=b; } virtual void area() { cout<<"shape "; } }; class square:public shape { public: void area() { cout<<"area of square:"<<x*x<<endl; } };

DEPT OF CSE OOPS LAB

Page 44

CR ENGINEERING COLLEGE
class rectangle:public shape { public: void area() { cout<<"area of rectangle:"<<x*y<<endl; } }; class circle:public shape { public: void area() { cout<<"area of circle:"<<(3.14)*x*x<<endl; } }; class triangle:public shape { public: int area1; void area() { cout<<"area of triangle:"; area1=(0.5)*x*y; cout<<area1<<endl; } }; class polygon:public triangle { public: void area() { cout<<"area of polygon:"<<6*area1<<endl; } }; void main() { clrscr(); shape *s; triangle t; square sq; rectangle rt; polygon p; circle c; s=&t; s->getdata(3,2); s>area(); s=&sq; s->getdata(4);

Dept., of CSE - CP LAB - I

Page 45

CR ENGINEERING COLLEGE
s->area(); s=&c; s->getdata(3);

s->area(); s=&rt; s->getdata(2,4); s->area(); s=&p; s->area(); getch(); }

Output area of triangle:3 area of square:16 area of circle:28.26 area of rectangle:8 area of polygon:23772

Result Thus the program to implement Hierarchy classes with Dynamic polymorphism and Use of Virtual concept along with RTTI is implemented.

Dept., of CSE - CP LAB - I

Page 46

CR ENGINEERING COLLEGE
Ex. No 10 File operations with randomly generated complex Number

Aim To a C++ program to implement the randomly generates complex numbers and write them two per line in a file along with an operator. 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. Algorithm Step 1: Start the Process. Step 2: Create two files. Step 3: One file is to read and another file to write. Step 4: Do the operations in one file and write the result in another file. Step 5: Display the result. Step 6: Stop the Process. Program #include<iostream.h> #include<fstream.h> #include<ctype.h> class Complex { private: int real; int imag; public:
void getComplex(); void showComplex(); friend istream& operator >>(istream&, Complex&); friend ostream& operator <<(ostream&, Complex&); friend Complex operator +(Complex, Complex); friend Complex operator -(Complex, Complex); }; void Complex::getComplex() { cout<<"Enter real and imaginary part:"; cin>>real>>imag; } void Complex::showComplex() { cout<<real; if(imag<0) { cout<<imag<<"i"<<endl; } else { cout<<"+"<<imag<<"i"<<endl; }

Dept., of CSE - CP LAB - I

Page 47

CR ENGINEERING COLLEGE
} istream& operator >>(istream &fin, Complex &c) { fin>>c.real; fin>>c.imag; return fin; } ostream& operator <<(ostream &fout, Complex &c) { fout<<c.real<<" "; fout<<c.imag<<" "; return fout; } Complex operator +(Complex c1, Complex c2) { Complex c; c.real=c1.real+c2.real; c.imag=c1.imag+c2.imag; return c; } Complex operator -(Complex c1, Complex c2) { Complex c; c.real=c1.real-c2.real; c.imag=c1.imag-c2.imag; return c; } int main() { Complex c1,c2,c3; char oper; char ch; fstream file; fstream rsul; rsul.open("resu.dat",ios::out); file.open("z:\complex.dat",ios::in|ios::out|ios::binary); if(rsul.fail()) { cout<<"unable to open"<<endl; } do { cout<<"Enter real and imaginary part of two complex numbers"<<endl; cin>>c1; file<<c1; cin>>c2; file<<c2; cout<<"Enter a operator"; cin>>oper; file<<oper<<endl; rsul<<c3;

Dept., of CSE - CP LAB - I

Page 48

CR ENGINEERING COLLEGE
cout<<"Another? "; cin>>ch; }while(toupper(ch)=='Y'); file.seekg(0); while(1) { file>>c1; file>>c2; file>>oper; if(file.fail()) break; cout<<c1; cout<<c2; cout<<oper; switch(oper) {

case '+': c3=c1+c2; cout<<endl; cout<<c3; c3.showComplex(); rsul<<c1; rsul<<c2; rsul<<" "<<c3<<endl; break; case '-': c3=c1-c2; cout<<endl; cout<<c3; c3.showComplex(); rsul<<c1<<c2<<" "<<c3<<endl; break; } } file.close(); rsul.close(); return 0; }

Output: Enter the real and imaginary part of two complex number: 2 3 4 5 Enter a operator + 11901 Result Thus the program to implement the randomly generates complex numbers and write them two per line in a file along with an operator. 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 is implemented.
Dept., of CSE - CP LAB - I Page 49