You are on page 1of 23

CS202: Programming Systems LAB 2: Friend Function, Function Overloading, and References Instructor: Hunh Cng Php Affiliation:

IT Faculty, Danang University of Technology 1. Write and run the sample below 2. /* Production of a Complex number and a Real number: m(a+jb)=ma + jmb 3. Create a friend function of both classes (Real and Complex) to perform this operation 4. */ 5. #include <iostream.h> 6. #include <conio.h> 7. class Complex; 8. class Real 9. { 10. private: 11. float value; 12. public: 13. Real(float v=0) 14. { 15. value=v; 16. } 17. void display() 18. { 19. cout<<value; 20. } 21. friend Complex &prod(Real &A, Complex &B); 22. }; 23. class Complex 24. { 25. private: 26. float real, image; 27. public: 28. Complex(float a=0, float b=0) 29. { 30. real=a; 31. image=b; 32. } 33. void display() 34. { 35. cout<<real<<" + j*"<<image; 36. } 37. friend Complex &prod(Real &A, Complex &B); 38. }; 39. //Define the production function of a Real and a Complex 40. Complex &prod(Real &A, Complex &B) 41. { 42. Complex C;

43. C.real=A.value*B.real; 44. C.image=A.value*B.image; 45. return C; 46. } 47. main() 48. { 49. Real A(6); 50. Complex B(5, 4); 51. A.display(); 52. cout<<"*("; 53. B.display(); 54. cout<<")= "; 55. Complex C= prod(A,B); 56. C.display(); 57. getch(); 58. } *Run this code

2. Re-write the above program that the function prodis a member function of Complex class and a friend of Real class (Hint: prod should take only one argument) /* Production of a Complex number and a Real number: m(a+jb)=ma + jmb Create a friend function of both classes (Real and Complex) to perform this operation */ #include <iostream.h> #include <conio.h> class Complex; class Real { private: float value; public: Real(float v=0) { value=v; } void display() { cout<<value; }

friend class Complex; }; class Complex { private: float real, image; public: Complex(float a=0, float b=0) { real=a; image=b; } void display() { cout<<real<<" + j*"<<image; }

Complex prod(Real &A) { Complex C; C.real=A.value*real; C.image=A.value*image; return C; } };

main() { Real A(6); Complex B(5, 4); A.display(); cout<<"*("; B.display(); cout<<")= "; Complex C= B.prod(A); C.display(); getch(); }

3. Create a Vector class including following members

*code #include <iostream.h> #include <conio.h>

class Vector { private: int n; // number of elements float *data; // values of vector elements public: Vector() //Constructor allows user to enter values from keyboard

{ int i; cout<< "the numbers of elements of the vector: n =" ; cin >> n; data = new float[n]; for (i = 1; i <=n; i++) { cout << "data["<<i<<"] = " ; cin >> data[i]; cout<<endl; } } Vector( float *a, int m) // Constructor initializes values to data members from another array { for(int i = m +1; i >0; i++) {a[i]= data[i];} } ~Vector(){} //destructor int capacity() //return the current capacity of this vector { return n; } void clear() //Removes all of the elements from thisVector. { n = 0; delete [] data; } bool contains(float elem); //Tests if the specified object is a component in this vector. int indexOf(float elem); //Searches for the first occurrence of the given argument. int lastIndexOf(float elem); //Returns the index of the last occurrence of the specified object in this vector. float elementAt(int index); //Returns the component at the specified index bool isEmpty(); //Tests if this vector has no components. float* toArray(); //Returns an array containing all of the elements in this the correct order. void display(); //Show values of vector }; bool Vector::contains(float elem) { int k; for ( k = 1 ; k <= n; k++) { if(elem == data[k]) {return cout<<"vector contains element 11\n";}

} return cout<<"vector does not contain element 11\n"; }; bool Vector::isEmpty() { if(n==0) {return cout<<"\nThis vector is empty";} else {return cout<<"\nThis vector is not empty";} }; int Vector::indexOf(float elem) { for (int j = 1; j <= n; j++) { if( elem == data[j]) { return j; } } }; int Vector::lastIndexOf(float elem) { int a; for(a=n+1;a>0;a--) { if(elem == data[a]) { return a; } } }; float Vector::elementAt(int index) { if (index>n) { cout<<"\ninvalid index\n"; return 0; } else{ return data[index]; } }; float* Vector::toArray() { return data; };

void Vector::display() { cout<<"\nshow all value of the vector:\n"; for(int i=1;i<=n;i++) { cout<<"data["<<i<<"]="<<data[i]<<endl; } }; main(){ Vector A; A.contains(11);

// data[1] = 6 // data[2] = 2 // data[3] = 7 // data[4] = 4 // data[5] = 11

cout<<"\nelement 11 has index: "<<A.indexOf(11)<<endl; cout<<"The capacity of this vector= "<<A.capacity()<<endl; A.isEmpty(); // data[6] = 3

cout<<"\nindex 8 contains the element: "<<A.elementAt(8)<<endl;

// data[7] = 7 // data[8] = 12

cout<<"The last ocurency of element 7 is at index "<<A.lastIndexOf(7)<<endl; A.display(); getch(); } // data[9] = 23

Run and compile:

*When there is no element.

4. Create a Matrix class including following members:

*source code #include <iostream> #include <conio.h> using namespace std; class Matrix { private: int n , m; float **data; public: Matrix() //Construcror allows user to enter values for a matrix from keyboard { int i, j; cout << "enter the capacity of row n = "; cin >> n; cout << "enter the capacity of column m = "; cin >> m; data = new float*[n]; // create rows for (i = 0; i < n; i++) { data[i] = new float[m]; // create columns } for (i = 0; i < n; i++) { for(j = 0; j <m; j++) { cout<< "\ndata["<<i<<"]["<<j<<"] = "; //input matrix cin >> data[i][j]; } } } ~Matrix(){} Matrix(int N, int M) //create M-by-N matrix of 0's { n = N; m = M; data = new float*[n]; for (int i =0; i < n;i++) { data[i] = new float[m]; } } Matrix(float **a) {

data = new float*[n]; for(int i = 0;i <n;i++) { data[i] = new float[m]; } for(int i=0;i<n;i++) { for (int j=0;j<m;j++) { a[i][j] = data[i][j]; } } } void swap(int i, int j); //swap rows i and j Matrix transpose(); //Create and return the transpose of the invoking matrix A[i][j] = B[j][i] Matrix add(Matrix B); // calculate C = A + B Matrix sub(Matrix B); // calculate C = A - B bool equal(Matrix B); // check wheather A = B Matrix prod(Matrix B); // calculate C = A*B void display(); }; void Matrix::swap(int i, int j) { float *temp = data[i]; data[i] = data[j]; data[j] = temp; } Matrix Matrix:: transpose() { Matrix A(n,m); cout << "Transposition of the matrix A: "<<endl; for(int i=0;i<A.n;i++) { for(int j=0;j<A.m;j++) { A.data[j][i] = data[i][j];

} } return A; } Matrix Matrix::add(Matrix B) // add transpose as a friend function { Matrix A = *this;

if(A.n==B.n && A.m==B.m) { Matrix C(n,m);

for(int i=0;i<C.n;i++) { for(int j=0;j<C.m;j++) { C.data[i][j]=A.data[i][j]+B.data[i][j];

} } return C; } else { cout <<"Error!!!"; } } Matrix Matrix:: sub(Matrix B) { Matrix A = *this; Matrix D(n,m); if(A.n==B.n && A.m==B.m) {

for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { D.data[i][j]= A.data[i][j] - B.data[i][j];

} } return D; } else { cout <<"Erorr!!!"; }

} bool Matrix::equal(Matrix B) { Matrix A = *this; if( A.n==B.n && A.m==B.m) {

for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(data[i][j] == B.data[i][j]) return cout<<"Matrix A = Matrix B"; else return cout<<"Matrix A != Matrix B"; } } } else { cout <<"Error!!!"; } } Matrix Matrix:: prod(Matrix B) { // calculate C = A*B Matrix A = *this; if (A.m == B.n){ Matrix E(n,B.m); E.n=A.n;E.m=B.m; for (int i = 0; i <E.n; i++) for (int j = 0; j <E.m; j++) for (int k = 0; k <A.m; k++) E.data[i][j] += (data[i][k] * B.data[k][j]); return E; } else{ cout<<"Error!!!"<<endl; } } void Matrix::display() { cout<<"\nMatrix :\n"; for (int i = 0; i <n; i++) {

for (int j = 0; j <m; j++) cout<<"\ndata["<<i<<"]["<<j<<"]="<<data[i][j]<<endl; } } int main() { cout<<"Setup Matrix:"<<endl; cout<<"Matrix A\n"; Matrix A; cout<<"\n***************************************************\n"; cout<<"\nMatrix B\n"; Matrix B; cout<<"\n***************************************************\n"; A.transpose().display(); cout<<"\n***************************************************\n"; cout<<"\nSwap rows 0 and 1 of matrix A\n"; A.swap(0,1); A.display(); cout<<"\n***************************************************\n"; cout<<"\nC = A + B: \n"; A.add(B).display(); cout<<"\n***************************************************\n"; cout<<"\nD = A - B: \n"; A.sub(B).display(); cout<<"\n***************************************************\n"; cout<<"\nE = A * B: \n"; A.prod(B).display(); cout<<"\n***************************************************\n";

A.equal(B);

getch(); } *The result There are several cases happened. In actually, I will show some specific cases. 1./ Matrix A and matrix B have the different either column or rows or both. However, we definitely can use function transposition and swap for the matrix A.

2. With the perfect conditions, the best result is showed:

5. Create functions:

#include <iostream.h> #include <conio.h> class Matrix; /////class Vector class Vector { private: int n; // number of elements float *data; // values of vector elements public: Vector() //Constructor allows user to enter values from keyboard { int i; cout<< "the numbers of elements of the vector: n =" ; cin >> n; data = new float[n]; for (i = 1; i <=n; i++) { cout << "data["<<i<<"] = " ; cin >> data[i]; cout<<endl; } } Vector( float *a, int m) // Constructor initializes values to data members from another array { for(int i = m +1; i >0; i++) {a[i]= data[i];} } ~Vector(){} //destructor int capacity() //return the current capacity of this vector { return n; } void clear() //Removes all of the elements from thisVector. { n = 0; delete [] data; } // float* toArray(); float* toArray() { return data; } //Returns an array containing all of the elements in this the correct order.

friend class Matrix; // void display(); void display_Vector() {

//Show values of vector

cout<<"\nshow all value of the vector:\n"; for(int i=1;i<=n;i++) { cout<<"\ndata["<<i<<"]="<<data[i]<<endl; } friend void swap(Vector &A, Vector &B); //friend Vector &prod(Matrix &A, Vector &B); }; // end of class Vector\ //*****************************************************************************//

//Class Matrix class Matrix { private: int n , m; float **data; public: Matrix() { int i, j;

//Construcror allows user to enter values for a matrix from keyboard

cout << "enter the capacity of row n = "; cin >> n; cout << "enter the capacity of column m = "; cin >> m; data = new float*[n]; // create rows for (i = 0; i < n; i++) { data[i] = new float[m]; // create columns } for (i = 0; i < n; i++) { for(j = 0; j <m; j++) { cout<< "\ndata["<<i<<"]["<<j<<"] = "; //input matrix cin >> data[i][j]; } } }

~Matrix(){} Matrix(int N, int M) //create M-by-N matrix of 0's { n = N; m = M; data = new float*[n]; for (int i =0; i < n;i++) { data[i] = new float[m]; } } Matrix(float **a) { data = new float*[n]; for(int i = 0;i <n;i++) { data[i] = new float[m]; } for(int i=0;i<n;i++) { for (int j=0;j<m;j++) { a[i][j] = data[i][j]; } } }

} void display_Matrix() { cout<<"\nMatrix :\n"; for (int i = 0; i <n; i++) { for (int j = 0; j <m; j++) cout<<"\ndata["<<i<<"]["<<j<<"]="<<data[i][j]<<endl; } }

friend void swap(Matrix &A, Matrix&B); //friend Vector &prod(Matrix &A, Vector &B); friend class Vector; }; inline void swap(int& i, int& j) {

int tmp = i; i = j; j = tmp; }; void swap(Vector &A, Vector &B) { if(A.n==B.n) { for(int i=0;i<A.n;i++) { swap(A.data[i],B.data[i]); } } else cout<<"\nError!!!\n"; }; void swap(Matrix &A, Matrix&B) { if (B.m == A.m && B.n == A.n) { for(int i=0;i<A.n;i++) { for(int j=0;j<A.m;i++) { swap(A[i][j],B[i][j]); } } } else cout<<"\Error!!!\n"; } Vector &prod(Matrix &A, Vector &B) { if(A.m == B.n) { Vector C; for(int i=0;i<A.n;i++) { for(int j=0;i<A.m;i++) { C.data[i]+= (A.data[i][j]*B.data[i]); } } } return C; } Vector Vector::&prod(Matrix &A)

{ if(m == A.n) { Vector D; for(int i=0;i<n;i++) { for(int j=0;i<m;i++) { D.data[i]+= (data[i][j]*A.data[i]); } } } return D; } Vector Matrix::&prod(Vector &A) { if(A.m == n) { Vector E; for(int i=0;i<n;i++) { for(int j=0;i<m;i++) { E.data[i]+= (A.data[i][j]*data[i]); } } } return E; } main() { Vector A; Vector B; A.display_Vector(); B.display_Vector(); Matrix G; Matrix F; G.display_Matrix(); F.display_Matrix(); swap(A,B); swap(G,F); Vector C = prod(G,A); Vector D = G.prod(A); Vector E = A.prod(G);

int x = 7, y = 6; swap(x,y); getch(); }

I have some error which I havent fix yet, so I could not run this code.

You might also like