Vous êtes sur la page 1sur 9

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; m1.printmat(); v1.printvector(','); 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 <<"\n1.push 2.pop 3.display 4.exit\nEnter ur choice"; cin >> ch; switch(ch) { case 1: cout <<"enter the element"; cin >> ch; st.push(ch); break; case 2: st.pop(); break; case 3: st.display();break; case 4: exit(0); } } return (0); }

OUTPUTS 1.push 2.pop 3.display 4.exit Enter ur choice2 stack under flow 1.push 2.pop 3.display 4.exit Enter ur choice1 enter the element2 inserted2 1.push 2.pop 3.display 4.exit Enter ur choice1 enter the element3 inserted3 1.push 2.pop 3.display 4.exit Enter ur choice2 deleted3 1.push 2.pop 3.display 4.exit Enter ur choice1 enter the element5 inserted5 1.push 2.pop 3.display 4.exit Enter ur choice3 52 1.push 2.pop 3.display 4.exit 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 <<"\n1.insert choice"; cin >> ch;

2.delet

3.display

4.exit\nEnter ur

switch(ch) { case 1:

cout <<"enter the element"; cin >> ch; qu.insert(ch); break; case 2: qu.delet(); break; case 3: qu.display();break; case 4: exit(0); }

} return (0); }

OUTPUT 1.insert 2.delet 3.display 4.exit Enter ur choice1 enter the element21 inserted21 1.insert 2.delet 3.display 4.exit Enter ur choice1 enter the element22 inserted22 1.insert 2.delet 3.display 4.exit Enter ur choice1 enter the element16 inserted16 1.insert 2.delet 3.display 4.exit Enter ur choice3 21 22 16 1.insert 2.delet 3.display 4.exit Enter ur choice2 deleted21 1.insert 2.delet 3.display 4.exit Enter ur choice3 22 16 1.insert 2.delet 3.display 4.exit 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

Vous aimerez peut-être aussi