CS202_LAP2

download CS202_LAP2

of 23

Transcript of CS202_LAP2

  • 8/13/2019 CS202_LAP2

    1/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 below2. /* Production of a Complex number and a Real number: m(a+jb)=ma + jmb3. Create a friend function of both classes (Real and Complex) to perform this operation4. */5. #include 6. #include 7. class Complex;8. class Real9. {10.private:11.float value;12.

    public:13.Real(float v=0)

    14.{15.value=v;16.}17.void display()18.{19.cout

  • 8/13/2019 CS202_LAP2

    2/23

    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

  • 8/13/2019 CS202_LAP2

    3/23

    friend class Complex;

    };

    class Complex

    {

    private:

    float real, image;

    public:

    Complex(float a=0, float b=0){

    real=a;

    image=b;

    }

    void display()

    {

    cout

  • 8/13/2019 CS202_LAP2

    4/23

    3. Create a Vector class including following members

    *code

    #include

    #include

    class Vector

    {

    private:

    int n; // number of elements

    float *data; // values of vector elements

    public:

    Vector() //Constructor allows user to enter values from keyboard

  • 8/13/2019 CS202_LAP2

    5/23

    {

    int i;

    cout> n;

    data = new float[n];

    for (i = 1; i

  • 8/13/2019 CS202_LAP2

    6/23

    }

    return cout

  • 8/13/2019 CS202_LAP2

    7/23

    void Vector::display()

    {

    cout

  • 8/13/2019 CS202_LAP2

    8/23

    *When there is no element.

  • 8/13/2019 CS202_LAP2

    9/23

    4. Create a Matrix class including following members:

  • 8/13/2019 CS202_LAP2

    10/23

    *source code

    #include

    #include

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

    cout > 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

  • 8/13/2019 CS202_LAP2

    11/23

    data = new float*[n];

    for(int i = 0;i

  • 8/13/2019 CS202_LAP2

    12/23

    if(A.n==B.n && A.m==B.m)

    {

    Matrix C(n,m);

    for(int i=0;i

  • 8/13/2019 CS202_LAP2

    13/23

    }

    bool Matrix::equal(Matrix B)

    {

    Matrix A = *this;

    if( A.n==B.n && A.m==B.m)

    {

    for(int i=0;i

  • 8/13/2019 CS202_LAP2

    14/23

    for (int j = 0; j

  • 8/13/2019 CS202_LAP2

    15/23

  • 8/13/2019 CS202_LAP2

    16/23

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

  • 8/13/2019 CS202_LAP2

    17/23

    5. Create functions:

  • 8/13/2019 CS202_LAP2

    18/23

    #include

    #include

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

    data = new float[n];for (i = 1; i

  • 8/13/2019 CS202_LAP2

    19/23

    friend class Matrix;

    // void display(); //Show values of vector

    void display_Vector()

    {

    cout

  • 8/13/2019 CS202_LAP2

    20/23

    ~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

  • 8/13/2019 CS202_LAP2

    21/23

    int tmp = i;

    i = j;

    j = tmp;

    };

    void swap(Vector &A, Vector &B)

    {

    if(A.n==B.n){

    for(int i=0;i

  • 8/13/2019 CS202_LAP2

    22/23

    {

    if(m == A.n)

    {

    Vector D;

    for(int i=0;i

  • 8/13/2019 CS202_LAP2

    23/23

    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.