Sie sind auf Seite 1von 27

CS202: Programming Systems

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);
};

class Real
{
private:
float value;
public:
Real(float v=0)
{
value=v;
}
void display()
{
cout<<value;
}
friend Complex &Complex::prod(Real &A);
};

// prod is a member function of Complex class
Complex &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;
C= B.prod(A);
C.display();
getch();
}

Result:















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;
}

float Vector::elementAt(int index)
{


return data[index];
}

bool Vector::isEmpty()
{
if (n==0)
return true;
else
return false;
}

float* Vector::toArray()
{
float* array= new float[n];
for (int j=0; j<n; j++)
{
array[j]=data[j];
}
for (int i=0; i<n; i++)
cout << array[i]<<" ";
}

void Vector::display()
{
for (int i=0; i<n; i++)
cout << data[i]<<" ";
}

//main program
main()
{
int elem, elem2, index;

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::transpose()
{
Matrix trans(m,n);
for (int i=0;i<n;i++)
{
for(int j=0; j<m; j++)
{
trans.data[j][i] = data[i][j];
}
}
return trans;
}

Matrix Matrix::add(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] = B.data[i][j] + data[i][j];
}
}
else cout<<"\nIt's invalid, cannot add !";
return C;
}

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();

cout << "\nmatrix A add matrix B:";
Matrix Add = A.add(B);
cout<<"\n";
Add.display();


cout << "\nmatrix A subtract matrix B:";
Matrix Sub = A.sub(B);
cout<<"\n";
Sub.display();

cout << "\nmatrix A multiply matrix B:";
Matrix Prod = A.prod(B);
cout<<"\n";
Prod.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::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;
}

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;
}
}

Matrix Matrix::transpose()
{
Matrix trans(m,n);
for (int i=0;i<n;i++)
{
for(int j=0; j<m; j++)
{
trans.data[j][i] = data[i][j];
}
}
return trans;
}

Matrix Matrix::add(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] = B.data[i][j] + data[i][j];
}
}
else cout<<"\nIt's invalid, cannot add !";
return C;
}

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";
}
};
// function

inline void swap(float &a, float &b)
{
int temp;
temp = a;
a = b;
b= temp;
}

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 << "\nThe two vectors are not equal in size! cannot swap "<<endl;
}


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; j++)
swap (A.data[i][j], B.data[i][j]);
}
else cout <<"The two matrix is not equal in size, cannot swap!"<<endl;
}

Vector &prod(Matrix &A, Vector &B)
{ float * product = new float [B.n];
for (int i=0; i<B.n; i++) //initialize value for product
product[i]=0;

for (int i=0; i<A.m; i++)
for (int j=0; j<B.n; j++)
product[i] = product[i] + B.data[j]*A.data[j][i];
Vector b(product, B.n);

return b;
}

Vector &Vector::prod(Matrix &A)
{ float* product = new float[n];

for (int i=0; i<A.m; i++)
for (int j=0; j<n; j++)
product[i] = product[i] + data[j]*A.data[j][i];
Vector a(product, n);
return a;
}
// main program
main ()
{
float a, b;
cout << "Enter two numbers.\n";
cout << "Number a : "; cin>>a;
cout << "Number b : "; cin >>b;
cout << "\n";
swap (a, b);
cout << "Swap a and b.\n";
cout << "Number a : "<<a<<"\n";
cout << "Number b : "<<b<<"\n";
cout << "\n";
cout <<"Enter two vectors:\n";
Vector A;
cout << "Vector A is: "; A.display();
cout<<"\nVector B:\n";
Vector B;
cout <<"Vector B is: ";
B.display();
cout << "\n";
swap(A, B);
cout <<"\nSwap vector A and vector B:\n";
cout<<"Vector A: ";
A.display();
cout <<"\nVector B: ";
B.display();

cout <<"\nEnter two matrix.\n";
Matrix A1;
cout << "Matrix A: \n";
A1.display();
cout << "\n";
Matrix B1;
cout << "Matrix B: \n";
B1.display();
cout << "\n";
swap (A1, B1);
cout <<"Swap matrix A and matrix B. \n";
cout << "Matrix A:\n";
A1.display();
cout << "Matrix B:\n";
B1.display();

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();
}

Result:

Das könnte Ihnen auch gefallen