0% found this document useful (0 votes)
11 views9 pages

Data Structure and Analysis Overview

This document contains code snippets demonstrating various C++ concepts: 1. A class matrix and vector are defined with default arguments in a printvector method. A multiply method takes a matrix and vector as arguments and performs matrix-vector multiplication. 2. A base class and derived dev class are defined with virtual functions. getdata and display functions are called through a base class pointer to demonstrate polymorphism. 3. A stack ADT is implemented using an array with push, pop and display methods. 4. A queue ADT is also implemented using an array with insert, delete and display methods. 5. A function template swap is defined to swap two variables of any type using templates.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

Data Structure and Analysis Overview

This document contains code snippets demonstrating various C++ concepts: 1. A class matrix and vector are defined with default arguments in a printvector method. A multiply method takes a matrix and vector as arguments and performs matrix-vector multiplication. 2. A base class and derived dev class are defined with virtual functions. getdata and display functions are called through a base class pointer to demonstrate polymorphism. 3. A stack ADT is implemented using an array with push, pop and display methods. 4. A queue ADT is also implemented using an array with insert, delete and display methods. 5. A function template swap is defined to swap two variables of any type using templates.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

STATIC MEMBERS AND METHODS WITH DEFAULT ARGUMENTS ************************************************ PROGRAM: #include<iostream.h> #include<conio.

h> class vector; void printvector(char ch='w'); class matrix { int a[20][20]; int i,j,m,n; Public: matrix() { cout<<"enter the row and column of a matrix"<<endl; cin>>m>>n; getmat(); } void getmat() { cout<<"enter the matrix value one by one"; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cin>>a[i][j]; } } } void printmat() { cout<<"the matrix:"<<endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) cout<<a[i][j]<<"\t"; cout<<"\n"; } } friend void multiply(matrix m1,vector v1); }; class vector { int a[10]; int i,n; public: vector()

{ cout<<"enter the number of values in vector"<<endl; cin>>n; getvector(); } void getvector() { cout<<"enter the values of vector"<<endl; for(i=0;i<n;i++) cin>>a[i]; } public: void printvector(char ch) { cout<<"the vector:"<<endl; cout<<"("; for(i=0;i<n;i++) cout<<a[i]<<ch; cout<<"\b"<<")"<<endl; } friend void multiply(matrix m1,vector v1); }; void multiply(matrix m2,vector v2) { static int i,k,c[20]; cout<<"matrix:"<<m2.m<<"X"<<m2.m<<endl; cout<<"vector:"<<v2.n<<"X1"<<"\t here we use it as column vector"<<endl; if(m2.n!=v2.n) { cout<<"matrix vector multiplication is not possible"<<endl; } else { for(i=0;i<m2.m;i++) { for(k=0;k<m2.n;k++) c[i]=c[i]+m2.a[i][k]*v2.a[k]; } { cout<<"the resultant vector:"<<endl; cout<<"("; for(i=0;i<m2.m;i++) cout<<c[i]<<","; cout<<"\b"<<")"<<endl; } } } void main()

{ clrscr(); matrix m1; vector v1; [Link](); [Link](','); multiply(m1,v1); getch(); }

VIRTUAL FUNCTIONS
#include<iostream.h> #include<conio.h> class base { private: int x; float y; public : virtual void getdata( ); virtual void display( ); }; class dev : public base { private: int roll; char name[20]; public : void getdata( ); void display( ); }; void base :: getdata( ) { } void base :: display( ) { } void dev :: getdata( ) { cout<< Enter Roll of the Student ; cin>> roll; cout<< Enter name of the student; cin>>name; } void dev :: display( ) { cout<<Name is :<<name<<endl; cout<< Roll no is :<<roll <<endl; } void main( ) { base * ptr; dev obj; clrscr( ); ptr = &obj; ptr -> getdata( ); ptr -> display( ); getch( ); }

OUTPUT Enter the roll no of the student: 111 Enter the name of the student : Kapil Dev Name is : Kapil Dev Roll no is : 111

STACK ADT USING ARRAY


#include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; class stack { int stk[5]; int top; public: stack() { top=-1; } void push(int x) { if(top > 4) { cout <<"stack over flow"; return; } stk[++top]=x; cout <<"inserted" <<x; } void pop() { if(top <0) { cout <<"stack under flow"; return; } cout <<"deleted" <<stk[top--]; } void display() { if(top<0) { cout <<" stack empty"; return; } for(int i=top;i>=0;i--) cout <<stk[i] <<" "; } }; main()

{ int ch; stack st; while(1) { cout <<"\[Link] [Link] [Link] [Link]\nEnter ur choice"; cin >> ch; switch(ch) { case 1: cout <<"enter the element"; cin >> ch; [Link](ch); break; case 2: [Link](); break; case 3: [Link]();break; case 4: exit(0); } } return (0); }

OUTPUTS [Link] [Link] [Link] [Link] Enter ur choice2 stack under flow [Link] [Link] [Link] [Link] Enter ur choice1 enter the element2 inserted2 [Link] [Link] [Link] [Link] Enter ur choice1 enter the element3 inserted3 [Link] [Link] [Link] [Link] Enter ur choice2 deleted3 [Link] [Link] [Link] [Link] Enter ur choice1 enter the element5 inserted5 [Link] [Link] [Link] [Link] Enter ur choice3 52 [Link] [Link] [Link] [Link] Enter ur choice4

QUEUE ADT USING AN ARRAY


#include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; class queue { int queue1[5]; int rear,front; public: queue() { rear=-1; front=-1; } void insert(int x) { if(rear > 4) { cout <<"queue over flow"; front=rear=-1; return; } queue1[++rear]=x; cout <<"inserted" <<x; } void delet() { if(front==rear) { cout <<"queue under flow"; return; } cout <<"deleted" <<queue1[++front]; } void display() { if(rear==front) { cout <<" queue empty"; return; } for(int i=front+1;i<=rear;i++) cout <<queue1[i]<<" "; } }; main() { int ch; queue qu; while(1) { cout <<"\[Link] choice"; cin >> ch;

[Link]

[Link]

[Link]\nEnter ur

switch(ch) { case 1:

cout <<"enter the element"; cin >> ch; [Link](ch); break; case 2: [Link](); break; case 3: [Link]();break; case 4: exit(0); }

} return (0); }

OUTPUT [Link] [Link] [Link] [Link] Enter ur choice1 enter the element21 inserted21 [Link] [Link] [Link] [Link] Enter ur choice1 enter the element22 inserted22 [Link] [Link] [Link] [Link] Enter ur choice1 enter the element16 inserted16 [Link] [Link] [Link] [Link] Enter ur choice3 21 22 16 [Link] [Link] [Link] [Link] Enter ur choice2 deleted21 [Link] [Link] [Link] [Link] Enter ur choice3 22 16 [Link] [Link] [Link] [Link] Enter ur choice FUNCTION TEMPLATE
#include<iostream.h> #include<conio.h> template<class t>

void swap(t &x,t &y) { t temp=x; x=y; y=temp; } void fun(int a,int b,float c,float d) { cout<<"\na and b before swaping :"<<a<<"\t"<<b; swap(a,b); cout<<"\na and b after swaping :"<<a<<"\t"<<b; cout<<"\n\nc and d before swaping :"<<c<<"\t"<<d; swap(c,d); cout<<"\nc and d after swaping :"<<c<<"\t"<<d; } void main() { int a,b; float c,d; clrscr(); cout<<"Enter A,B values(integer):"; cin>>a>>b; cout<<"Enter C,D values(float):"; cin>>c>>d; fun(a,b,c,d); getch(); }

Output:
Enter A, B values (integer): 10 20 Enter C, D values (float): 2.50 10.80 A and B before swapping: 10 20 A and B after swapping: 20 10 C and D before swapping: 2.50 10.80 C and D after swapping: 10.80 2.50 EXCEPTION HANDLING
#include<iostream.h> #include<conio.h> void main() { int a,b,c; float d; clrscr(); cout<<"Enter the value of a:"; cin>>a; cout<<"Enter the value of b:";

cin>>b; cout<<"Enter the value of c:"; cin>>c; try { if((a-b)!=0) { d=c/(a-b); cout<<"Result is:"<<d; } else { throw(a-b); } } catch(int i) { cout<<"Answer is infinite because a-b is:"<<i; } getch(); }

Output:
Enter the value for a: 20 Enter the value for b: 20 Enter the value for c: 40 Answer is infinite because a-b is: 0

You might also like