LAB 2: Friend Function, Function Overloading, and References
Instructor: Hunh Cng Php Affiliation: IT Faculty, Danang University of Technology
Name : L Trung Hiu Class : 10ES
1. Write and run the sample below
/* 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 Complex &prod(Real &A, Complex &B); }; class Complex { private: float real, image; public: Complex(float a=0, float b=0) { real=a; image=b; } void display() { cout<<real<<" + j*"<<image; } friend Complex &prod(Real &A, Complex &B); }; //Define the production function of a Real and a Complex Complex &prod(Real &A, Complex &B) { Complex C; C.real=A.value*B.real; C.image=A.value*B.image; return C; } main() { Real A(6); Complex B(5, 4); A.display(); cout<<"*("; B.display(); cout<<")= "; Complex C= prod(A,B); C.display(); getch(); }
Result:
2. Re-write the above program that the function prod is a member function of Complex class and a friend of Real class (Hint: prod should take only one argument)
Code:
/**************************** Name: Le Trung Hieu Class: 10ES CS202 - Lab2 - Problem 2 *****************************/ #include <iostream.h> #include <conio.h> class Real; 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); };
3. Create a Vector class including following members
Data members Description int n Number of elements float *data Values of vector elements
Functions Description Vector() Constructor allows user to enter values from keyboard Vector(float *a , int n) Construcror initializes values to data members from another array ~Vector() Destructor int capacity() Returns the current capacity of this vector void clear() R Removes all of the elements from this Vector. 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 boolean isEmpty() Tests if this vector has no components. float[] toArray() Returns an array containing all of the elements in this Vector in the correct order. void display() Show values of vector
Code:
/**************************** Name: Le Trung Hieu Class: 10ES CS202 - Lab2 - Problem 3 *****************************/ #include <iostream.h> #include <conio.h> class Vector { private: int n; //Number of element float *data; //Value of vector element public: Vector (); //Constructor allows user to enter values from keyboard Vector(float *a , int n); // Constructor initializes values to data members from another array ~Vector(){}; // Destructor int capacity(); //Returns the current capacity of this vector void clear(); // Removes all of the elements from this Vector. 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 Vector in the correct order void display(); // Show values of vector };
Vector::Vector() { cout<<"Enter the values of vector\n"; cout<<"The number of element: "; cin>>n; cout<<"The value of vector element:\n"; data=new float[n]; for (int i=0;i<n;i++) { cout << "data["<<i<<"] = "; cin >> data[i]; } }
Vector::Vector(float *a, int m) { n = m; data = new float[n]; for (int j=0; j<m; j++) data[j]=a[j]; }
int Vector::capacity() { return n; }
void Vector::clear() { delete [] data; n=0; }
bool Vector::contains(float elem) { for (int j=0; j<n; j++) { if (data[j]==elem) return true; } return false; }
int Vector::indexOf(float elem) { for (int j=0; j<n; j++) { if (data[j]==elem) return j; } return -1; }
int Vector::lastIndexOf(float elem) { for (int j=n-1; j>=0; j--) { if (data[j]==elem) return j; } return -1; }
Vector A; cout <<"Vector: "; A.display(); cout<<"\n"; cout<<"\nThe current capacity of this vector : "<< A.capacity();
if(A.contains(3)) cout <<"\nNumber 3 is a component of this Vector\n"; else cout <<"\nNumber 3 is not a component of this Vector\n";
cout<<"\nEnter the element you want to search for the first occurrence: "; cin>>elem; if (A.contains(elem)) cout<<"Index of the fist occurrence of element "<< elem<< " is: " << A.indexOf(elem)<<"\n"; else cout <<"The element you entered is not in the Vector!\n";
cout<<"\nEnter the element you want to search for the last occurrence: "; cin>>elem2; if (A.contains(elem2)) cout<<"\Index of the last occurrence of element "<< elem2<< " is: " << A.lastIndexOf(elem2)<<"\n"; else cout <<"This element is not in the vector!\n";
cout<<"\nEnter the index: "; cin>>index; if ((index<0)||(index>=A.capacity())) cout<<"Do not have this index !\n"; else cout <<"The component at the index "<<index<< " is: "<< A.elementAt(index)<<"\n";
if (A.isEmpty()) cout <<"\nThis vector has no components.\n"; else cout <<"\nThis vector has components.\n";
cout <<"\nThe array containing all of the elements in this vector in the correct order:\n "; A.toArray(); cout<<"\n"; A.clear(); cout<<"\nRemoves all of the elements from this vector."; cout <<"\nVector: "; A.display(); getch(); }
Result:
4. Create a Matrix class including following members
Data members Description int n Row of a matrix int m Column of a matrix float data[][] Data of matrix
Functions Description Matrix() Construcror allows user to enter values for a matrix from keyboard Matrix(int M, int N) create M-by-N matrix of 0's Matrix(float a[][]) Create matrix based on 2d array void swap(int i, int j) swap rows i and j Matrix transpose() Create and return the transpose of the invoking matrix A.data[j][i] = B.data[i][j] ; i=0..n-1, j=0..m-1 Matrix add(Matrix B) return C = A + B its valid if (B.m == A.m && B.n == A.n) C.data[i][j]= A.data[i][j] + B.data[i][j]; i=0..n-1, j=0..m-1 Matrix sub(Matrix B) return C = A B its valid if (B.m == A.m && B.n == A.n) C.data[i][j]= A.data[i][j] - B.data[i][j]; i=0..n-1, j=0..m-1 boolean equal(Matrix B) Check if A = B exactly? Matrix prod(Matrix B) // return C = A * B its valid if (A.m == B.n) C.data[i][j] += (A.data[i][k] * B.data[k][j]); i=0..B.m-1, j=0..A.n-1, k=0..B.n-1 display() Print matrix
Code:
/**************************** Name: Le Trung Hieu Class: 10ES CS202 - Lab2 - Problem 4 *****************************/
#include <iostream.h> #include <conio.h> class Matrix { private: int n, m; //Row and Column of a matrix float data[100][100]; //Data of matrix public: Matrix(); //Construcror allows user to enter values for a matrix from keyboard Matrix(int M, int N); //create M-by-N matrix of 0's Matrix(float a[][100], int M, int N);//Create matrix based on 2d array void swap(int i, int j); //swap rows i and j Matrix transpose(); //Create and return the transpose of the invoking matrix //A.data[j][i] = B.data[i][j] ; i=0..n-1, j=0..m-1 Matrix add(Matrix B); //return C = A + B //its valid if (B.m == A.m && B.n == A.n) //C.data[i][j]= A.data[i][j] + B.data[i][j]; i=0..n-1, j=0..m-1 Matrix sub(Matrix B); //return C = A B //its valid if (B.m == A.m && B.n == A.n) //C.data[i][j]= A.data[i][j] - B.data[i][j]; i=0..n-1, j=0..m-1 bool equal(Matrix B); //check if A = B exactly? Matrix prod(Matrix B); // return C = A * B //its valid if (A.m == B.n) //C.data[i][j] += (A.data[i][k] * B.data[k][j]); //i=0..B.m-1, j=0..A.n-1, k=0..B.n-1 void display(); //Print matrix };
Matrix::Matrix() { cout<<"Enter the values for a matrix.\n"; cout<<"Number of Rows: "; cin>>n; cout<<"Number of Columns: "; cin>>m; for (int i=0;i<n;i++) for(int j=0; j<m; j++) { cout <<"data["<<i<<"]["<<j<<"]= "; cin >> data[i][j]; } }
Matrix::Matrix(int M, int N) { n=M; m=N; for (int i=0;i<n;i++) { for(int j=0; j<m; j++) { data[i][j]=0; } } }
Matrix::Matrix(float a[][100], int M, int N) { n=M; m=N; for (int i=0;i<n;i++) { for(int j=0; j<m; j++) { this ->data[i][j]=data[i][j]; } } }
void Matrix::swap(int i, int j) { float tmp; for (int k=0; k<m;k++) { tmp = data[i][k]; data[i][k] = data[j][k]; data[j][k] = tmp; } }
Matrix Matrix::sub(Matrix B) {Matrix C(n,m); if ((B.n==n)&&(B.m==m)) { for (int i=0;i<n;i++) { for(int j=0; j<m; j++) C.data[i][j] = data[i][j]-B.data[i][j]; } } else cout<<"\nIt's invalid, cannot subtract !"; return C; }
bool Matrix::equal(Matrix B) { if ((B.n==n)&&(B.m==m)) { for (int i=0;i<n;i++) { for(int j=0; j<m; j++) { if (B.data[i][j] != data[i][j]) return false; else continue; } } return true; } else return false; }
Matrix Matrix::prod(Matrix B) { Matrix C(n,B.m); if (m==B.n) { for (int i=0; i<n; i++) { for (int j=0; j<B.m; j++) { C.data[i][j]=0; for (int k=0; k<m; k++) { C.data[i][j]+= (data[i][k]*B.data[k][j]); } } } } else cout<<"\nIt's invalid, cannot multiply !"; return C; }
void Matrix::display() { for (int i=0; i<n; i++) { for (int j=0; j<m; j++) cout <<data[i][j] << " ";
cout<<"\n"; } }
//main program int main() { Matrix A; cout <<"\nmatrix A:\n"; A.display(); cout << "\nmatrix B: (matrix B is the transpose of matrix A).\n"; Matrix B = A.transpose(); B.display();
if (A.equal(B)) cout << "\nmatrix A is equal to matrix B.\n"; else cout << "\nmatrix A is not equal to matrix B.\n";
A.swap(0,1); cout << "\nswap rows 0 and 1 of matrix A: \n"; A.display();
getch(); }
Result:
5. Create functions:
Functions Description Function type void swap(float &a, float &b) swap a and b non-member and inline function void swap(Vector &A, Vector &B) swap vector A and vector B its valid if (A.n == B.n) (using inline function above to swap elements) Friend function of class Vector void swap(Matrix &A, Matrix &B) swap matrix A and matrix B its valid if (B.m == A.m && B.n == A.n) (using inline function above to swap elements) Friend function of class Matrix Vector &prod(Matrix &A, Vector &B Production of Matrix and Vector (Its valid if (A.m == B.n)) Friend function of class Vector and class Matrix Vector &prod(Matrix &A) P Production of Matrix and Vector Member function of class Vector and friend function of class Matrix Vector &prod(Vector &A) Production of Matrix and Vector Member function of class Matrix and friend function of class Vector
Code:
/**************************** Name: Le Trung Hieu Class: 10ES CS202 - Lab2 - Problem 5 *****************************/ #include <iostream.h> #include <conio.h> class Matrix; //class vector class Vector { private: int n; //Number of element float *data; //Value of vector element public: Vector (); //Constructor allows user to enter values from keyboard Vector(float *a , int n); // Constructor initializes values to data members from another array ~Vector(){}; // Destructor int capacity(); //Returns the current capacity of this vector void clear(); // Removes all of the elements from this Vector. 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 Vector in the correct order void display(); // Show values of vector Vector &prod(Matrix &A); friend void swap(Vector &A, Vector &B); friend Vector &prod(Matrix &A, Vector &B);
}; Vector::Vector() { cout<<"Enter the values of vector\n"; cout<<"The number of element: "; cin>>n; cout<<"The value of vector element:\n"; data=new float[n]; for (int i=0;i<n;i++) { cout << "data["<<i<<"] = "; cin >> data[i]; } }
Vector::Vector(float *a, int m) { n = m; data = new float[n]; for (int j=0; j<m; j++) data[j]=a[j]; }
int Vector::indexOf(float elem) { for (int j=0; j<n; j++) { if (data[j]==elem) return j; } return -1; }
int Vector::lastIndexOf(float elem) { for (int j=n-1; j>=0; j--) { if (data[j]==elem) return j; } return -1; }
float Vector::elementAt(int index) //check element at specified index { return data[index]; }
bool Vector::isEmpty() // Tests if this vector has no components. { if (n==0) return true; else return false; }
float* Vector::toArray() //Returns an array containing all of the elements in this Vector in the correct order { return data; }
void Vector::display() // Show values of vector { for (int i=0; i<n; i++) cout << data[i]<<" "; }; //class Matrix class Matrix { private: int n, m; //Row and Column of a matrix float data[100][100]; //Data of matrix public: Matrix(); //Construcror allows user to enter values for a matrix from keyboard Matrix(int M, int N); //create M-by-N matrix of 0's Matrix(float a[][100], int M, int N);//Create matrix based on 2d array void swap(int i, int j); //swap rows i and j Matrix transpose(); //Create and return the transpose of the invoking matrix //A.data[j][i] = B.data[i][j] ; i=0..n-1, j=0..m-1 Matrix add(Matrix B); //return C = A + B //its valid if (B.m == A.m && B.n == A.n) //C.data[i][j]= A.data[i][j] + B.data[i][j]; i=0..n-1, j=0..m-1 Matrix sub(Matrix B); //return C = A B //its valid if (B.m == A.m && B.n == A.n) //C.data[i][j]= A.data[i][j] - B.data[i][j]; i=0..n-1, j=0..m-1 bool equal(Matrix B); //check if A = B exactly? Matrix prod(Matrix B); // return C = A * B //its valid if (A.m == B.n) //C.data[i][j] += (A.data[i][k] * B.data[k][j]); //i=0..B.m-1, j=0..A.n-1, k=0..B.n-1 void display(); //Print matrix friend void swap(Matrix &A, Matrix &B); friend Vector &prod(Matrix &A, Vector &B); friend Vector &Vector::prod(Matrix &A); friend Vector &prod(Matrix &A, Vector &B); };
Matrix::Matrix() { cout<<"Enter the values for a matrix.\n"; cout<<"Number of Rows: "; cin>>n; cout<<"Number of Columns: "; cin>>m; for (int i=0;i<n;i++) for(int j=0; j<m; j++) { cout <<"data["<<i<<"]["<<j<<"]= "; cin >> data[i][j]; } }
Matrix::Matrix(int M, int N) { n=M; m=N; for (int i=0;i<n;i++) { for(int j=0; j<m; j++) { data[i][j]=0; } } }
Matrix::Matrix(float a[][100], int M, int N) { n=M; m=N; for (int i=0;i<n;i++) { for(int j=0; j<m; j++) { this ->data[i][j]=data[i][j]; } } }
void Matrix::swap(int i, int j) { float tmp; for (int k=0; k<m;k++) { tmp = data[i][k]; data[i][k] = data[j][k]; data[j][k] = tmp; } }
cout<<"Product of Matrix B and vector A :\n"; Vector C = prod(B1,A); C.display(); cout <<"\nProduct of Matrix B and vector A using another function:\n"; Vector D = A.prod(B1); cout <<"\n"; D.display(); getch(); }