0% found this document useful (0 votes)
45 views

Oops Lab Pgms 1-5

MULTIPLICATION USING static MEMBERS, DEFAULT ARGUMENTS AND FRIEND FUNCTION. COMPLEX NUMBER CONVERSIONS USING OPERATOR OVERLOADING AND TYPE CONVERSIONS.

Uploaded by

suresh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Oops Lab Pgms 1-5

MULTIPLICATION USING static MEMBERS, DEFAULT ARGUMENTS AND FRIEND FUNCTION. COMPLEX NUMBER CONVERSIONS USING OPERATOR OVERLOADING AND TYPE CONVERSIONS.

Uploaded by

suresh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

1. MATRIX MULTIPLICATION USING STATIC MEMBERS, DEFAULT ARGUMENTS AND FRIEND FUNCTION #include <iostream> #include <stdlib.

h> class Matrix { private: float matrix_a[3][3]; float matrix_b[3][3]; static float matrix_c[3][3]; public: Matrix(); void get_matrix_a(); void get_matrix_b(); void show_result_Matrix(); friend void multiply_matrices(Matrix); }; void Matrix::get_matrix_a() { cout<<" Enter the values of the Matrix A row by row : "<<endl; cout<<" A = "<<endl; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { cin>>matrix_a[i][j]; } } } void Matrix::get_matrix_b() { cout<<" Enter the values of the Matrix B row by row : "<<endl; cout<<" B = "<<endl; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { cin>>matrix_b[i][j];

} } } void multiply_matrices(Matrix X) { float value=0; float sum=0; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { for(int k=0;k<3;k++) { value=X.matrix_a[i][k]*X.matrix_b[k][j]; sum= sum+value; } X.matrix_c[i][j]=sum; cout<<"\n"<<X.matrix_c[i][j]; value=0.0; sum=0.0; } } } void Matrix::show_result_Matrix() { cout<<"\n"<<"Matrix A"<<"\n"; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { cout<<matrix_a[i][j]<<" "; } cout<<"\n"; } cout<<"\n"<<"Matrix B"<<"\n"; for(int i=0;i<3;i++) { for(int j=0;j<3;j++)

{ cout<<matrix_b[i][j]<<" } cout<<"\n"; } cout<<"\n\n"<<" Product Matrix Output"<<"\n\n"; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { cout<<matrix_c[i][j]<<" "; } cout<<"\n"; } } float Matrix::matrix_c[3][3]; int main() { Matrix Obj; Obj.get_matrix_a(); Obj.get_matrix_b(); multiply_matrices(Obj); Obj.show_result_Matrix(); return 0; } ";

2. COMPLEX NUMBER CONVERSIONS USING OPERATOR OVERLOADING AND TYPE CONVERSIONS

#include<iostream> #include<cmath> class complex { double real,img; public: complex() { real=0; img=0; } complex(int integer) { real=integer; img=0; } complex(double value) { real=value; img=0; } complex(double r,double i) { real=r; img =i; } operator int() { double r= real; double i= img; int s = int (sqrt((r*r) - (i*i))); return s; } void display(); complex operator +(complex obj3);

complex operator *(complex obj3); }; complex complex::operator+(complex obj3) { complex temp; temp.real=real+obj3.real; temp.img=img+obj3.img; return temp; } complex complex::operator*(complex obj3) { complex temp; temp.real=((real*obj3.real)-(img*obj3.img)); temp.img= ((real*obj3.img)+(img*obj3.real)); return temp; } void complex::display() { cout<<"\n\nRESULT : "<<real; if((img>0) || (img==0 )) cout<<" + "<<img<<"j"<<"\n\n"; else cout<<" - "<<img<<"j"<<"\n\n"; } int main() { int integer; cout<<"\n*******conversion of Integer to complex******"; complex obj1(50); obj1.display(); cout<<"\n******conversion of Double to complex********"; complex obj2(25.55); obj2.display(); complex obj3(4,2),obj4(1,2),obj5; cout<<"\n*****conversion of complex to integer*******"; integer = int(obj3); cout<<"\n\nconversion Integer value is : "<<integer; cout<<"\n\n*******calling Operator(+) Overloading*******"; obj5=obj3 + obj4; obj5.display();

cout<<"\n********calling Operator(*) Overloading********"; obj5 = obj3 * obj4; obj5.display(); return 0;

} OUTPUT: *******conversion of Integer to complex****** RESULT : 50 + 0j ******conversion of Double to complex******** RESULT : 25.55 + 0j *****conversion of complex to integer******* Conversion Integer value is : 3 *******calling Operator(+) Overloading******* RESULT : 5 + 4j ********calling Operator(*) Overloading******** RESULT : 0 + 10j

3. DYNAMIC MEMORY ALLOCATION USING CONSTRUCTOR AND ASSIGNMENT OPERATOR #include<iostream> int i,j; class matrix { int **p,row,col; public: matrix(int r,int c) { col=c; row=r; p=new int *[row]; for(int i=0;i<row;i++) p[i]=new int[col]; cout<<"\nConstructor call\n"; } ~matrix() { cout<<"\n\nDestructor at work\n\n"; } void get(); void display(); matrix operator=(matrix ob2); }; matrix matrix::operator=(matrix ob2) { for(i=0;i<row;i++) { for(j=0;j<col;j++) { p[i][j]=ob2.p[i][j]; } } return *this; } void matrix::get() { cout<<" Enter the matrix in "<<row<<" x "<<col<<" size "<<endl; for(i=0;i<row;i++) { for(j=0;j<col;j++) { cin>>p[i][j]; }

} } void matrix::display() { for(i=0;i<row;i++) { for(j=0;j<col;j++) { cout<<p[i][j]<<"\t"; } cout<<endl; } } int main() { int r,c; cout<<"Enter the row : ";cin>>r; cout<<"\nEnter the column : ";cin>>c; matrix ob(r,c);//Calls Parameterized Constructors ob.get(); cout<<"\n\nDyanamic allocation in Normal objects\n\n"; ob.display(); matrix ob1=ob;//By default copy constructor is called cout<<"\n\nDynamic allocation using copy constructor\n\n"; ob1.display(); matrix ob2(r,c); ob2=ob; //ob2 object is implicitly passed, using assignment operator cout<<"\n\nDynamic allocation using Assingment (=) operator overloading\n\n"; ob2.display(); cout<<endl; return 0; }

OUTPUT: Enter the row : 2 Enter the column : 2 Constructor call Enter the matrix in 2 x 2 size 1 2 4 5 Dyanamic allocation in Normal objects 1 4 2 5

Dynamic allocation using copy constructor 1 4 2 5

Constructor call

Destructor at work

Destructor at work

Dynamic allocation using Assingment (=) operator overloading 1 4 2 5

Destructor at work

Destructor at work

Destructor at work

4. OVERLOADING NEW AND DELETE OPERATOR USING TYPE CONVERSION #include<iostream.h> #include<conio.h> #include<stdlib.h> class vector { private: int *array; public: void *operator new(size_t size) { vector *v; cout<<\nOperator new invoked; v=::new vector; v->array = new int[size]; if(!v) { cout<<Unable to allocate memory; exit(0); } return v; } void operator delete(void* v) { cout<<\nOperator delete invoked; vector *vp; vp = (vector*) v; delete (int *) vp->array; ::delete v; } void read(int); int max(int); int sum(int); }; void vector::read(int s) { for(int i=0; i<s; i++) { cout<<\nEnter element <<i+1<<:; cin>>array[i];

} } int vector::sum(int s) { int tot=0; for(int i=0; i<s; i++) tot+=array[i]; return tot; } int vector::max(int s) { int max=0; for(int i=0;i<s;i++) if(array[i]>max) max = array[i]; return max; } void main() { int s; clrscr(); cout<<\nEnter how many elements; cin >> s; vector *vec = new vector; cout<<Enter vector data\n; vec->read(s); cout<<\nThe maximum value is <<vec->max(s); cout<<\nThe sum is<< vec->sum(s); delete vec; getch(); }

OUTPUT: Enter how many elements: 4 Operator new invoked Enter vector data Enter element 1:12 Enter element 2:32 Enter element 3:44 Enter element 4:56 The maximum value is56 The sum is144 Operator delete invoked

5. LINKED LIST USING TEMPLATES #include <iostream> template<class TYPE> struct link { TYPE data; link* next; }; template<class TYPE> class linklist { private: link<TYPE>* first; public: linklist() { first = NULL; } void additem(TYPE d); void display(); }; template<class TYPE> void linklist<TYPE>::additem(TYPE d) { link<TYPE>* newlink = new link<TYPE>; newlink->data = d; newlink->next = first; first = newlink; } template<class TYPE> void linklist<TYPE>::display() { link<TYPE>* current = first; while( current != NULL ) { cout << endl << current->data; current = current->next; } }

int main() { linklist<double> ld; ld.additem(151.5); ld.additem(262.6); ld.additem(373.7); ld.display(); linklist<char> lch; lch.additem(a); lch.additem(b); lch.additem(c); lch.display(); cout << endl; return 0; }

OUTPUT: 373.7 262.6 151.5 c b a

You might also like