samedi 3 novembre 2018

Please explain output from the `function1(p1,p2,p3);`

    `class coordinate
    {
    private:
        int x;
        int y;

    public:

        coordinate()//DEFAULT CONSTRUCTOR
        {
            x = 0;
            y = 0;
            cout << "def C coordinate\t" << x << " " << y << endl;
        }

        coordinate(int a, int b)//PARAMETRIZED CONSTRUCTOR
        {
            x = a;
            y = b;
            cout << "prmtzd C coordinate\t" << x << " " << y << endl;
        }

        coordinate(const coordinate &cpy)//COPY CONSTRUCTOR
        {
            x = cpy.x;
            y = cpy.y;
            cout << "CPY C coordinate\t" << x << " " << y << endl;
        }

        void setall(int a, int b){x = a; y = b;}

        int getx(){return x;}

        int gety(){return y;}

        ~coordinate()//DESTRUCTOR
        {
            cout << "D coordinate\t" << x << " " << y << endl;
        }
    };
    ///////////////////////////////////////////////////////////////////////////////////////////////////
    class point 
    {
    private:
        coordinate xy;
        int z;

    public:

        coordinate &ref = xy;

        point():xy(),z(0)//DEFAULT CONSTRUCTOR
        {
            cout << "def C point\t" << xy.getx() << " " << xy.gety() << " " << z << endl;
        }

        point(int a, int b, int c) :xy(a, b), z(c)
        {
            cout << "prmtzd C point\t" << xy.getx() << " " << xy.gety() << " " << z << endl;
        }

        point(coordinate a, int b)
        {
            xy = a;//this calls default constructor
            z = b;
            cout << "prmtzd C point\t" << xy.getx() << " " << xy.gety() << " " << z << endl;
        }

        point(const coordinate &cpy)//COPY CONSTRUCTOR
        {
            xy = cpy;
            z = 100;
            cout << "cpy C point\t" << xy.getx() << " " << xy.gety() << " " << z << endl;
        }

        void setall(int a, int b, int c){xy.setall(a, b); z = c;}

        void print() { cout << xy.getx() << " " << xy.gety() << " " << z << endl; }

        ~point()//DESTRUCTOR
        {
            cout << "D point\t" << xy.getx() << " " << xy.gety() << " " << z << endl;
        }
    };

    void function1(point &p1, point p2, point p3)
    {

        p2.setall(10, 10, 10);
        p3.ref.setall(200, 200);

    }

    coordinate co1;

    int main()
    {
        point p1; 
        co1.setall(100, 100);
        point p2(5, 5, 5);
        point p3(co1); 
        p1.print();
        p2.print();
        p3.print();
        **function1(p1,p2,p3);**
        co1.setall(8, 8);
        point p4(co1, 8);
        co1.~coordinate();
        p1.print();
        p2.print();
        p3.print();
        return 0;

    }
    `

i need to know how to work function 1 function1(p1,p2,p3);
the OUTPUT print

CPY C coordinate 100 100??
CPY C coordinate 5 5 ??

why not print
cpy C point

OUTPUT

def C coordinate        0 0
def C coordinate        0 0
def C point     0 0 0
prmtzd C coordinate     5 5
prmtzd C point  5 5 5
def C coordinate        0 0
cpy C point     100 100 100
0 0 0
5 5 5
100 100 100
CPY C coordinate        100 100
CPY C coordinate        5 5
D point 10 10 10
D coordinate    10 10
D point 100 100 100
D coordinate    100 100
CPY C coordinate        8 8
def C coordinate        0 0
prmtzd C point  8 8 8
D coordinate    8 8
D coordinate    8 8
0 0 0
5 5 5
200 200 100
D point 8 8 8
D coordinate    8 8
D point 200 200 100
D coordinate    200 200
D point 5 5 5
D coordinate    5 5
D point 0 0 0
D coordinate    0 0
D coordinate    8 8

Aucun commentaire:

Enregistrer un commentaire