How to copy a class object....please take a look

Hello,
I am a beginner in Visual C++.Net. I need some help with my class.

I would like to copy one object to another one.

Here is the code:
//------------------- Test.h -------------------------------------
Class Test
{
    public:
    double AN, double BN, double CN;

    // Constructor
    Test(double a, double b, double c);

   // Destructor
    ~Test();

   //Function
   void Just_a_test();

};
//------------------- Test.cpp -------------------------------------
# include "Test.h";

Test::Test(double a, double b, double c)
{
    AN=a;
    BN=b;
    CN=c;
}

Test::~Test()
{
}

// Function
 void Just_a_test()
{
    ...just do something with AN, BN and CN
}

//----------------------------- my Dlg.cpp --------------------------------
.... on click button i.e.

double a,b,c;

a=100;
b=200;
c=300;
// create a object and call constructor
Test First_Object (a,b,c);

// call the function
First_Object.Just_a_test();

****************************************
I would like to create now a new object i.e. Second_Object and copy all the data from
First_Object

Back in c++ I just did this:
Second_Object=First_Object;

This will not work with Visual c++
Please let me know.
Please give me some credit .... I am a beginner.

Thank you
With best regards,
Thomas





Thomas StockbrueggerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
>> Back in c++ I just did this:
Second_Object=First_Object;

This will not work with Visual c++

This works in Visual C++ exactly like in any other C++ implementation. Since your class doesn't contain operator=, C++ executes default operator which assignes all members of First_Object to second object.

Second_Object=First_Object;

This line is executed by the following way:

Second_Object.AN = First_Object.AN;
Second_Object.BN = First_Object.BN;
Second_Object.CN = First_Object.CN;

This way works unless your class contains pointers, lists or other non-trivial data which gives incorrect result by applying default operator=. In this case you need to write your own operator=, and still use the same assignment Second_Object=First_Object.
Thomas StockbrueggerAuthor Commented:
Hi AlexFM,
thanks for your fast answer.

Yes... this works great.
//-------------------------------------------
Second_Object.AN = First_Object.AN;
Second_Object.BN = First_Object.BN;
Second_Object.CN = First_Object.CN;
//--------------------------------------------

 >> you need to write your own operator=
How can I write my own operator ?

Please take a look....like this????
 
//####################################################
//------------------- Test.h -------------------------------------
Class Test
{
    public:
    double AN, double BN, double CN;

    // Constructor
    Test(double a, double b, double c);

//- - - - - - - - - - - - - - - - - - - - - - -
    //Copy Consturctor
    Test(const Test &copy_test)
//- - - - - - - - - - - - - - - - - - - - - - -


   // Destructor
    ~Test();

   //Function
   void Just_a_test();

};
//------------------- Test.cpp -------------------------------------
# include "Test.h";

Test::Test(double a, double b, double c)
{
    AN=a;
    BN=b;
    CN=c;
}
//- - - - - - - - - - - - - - - - - - - - - - -
Test:Test(const Test &copy_test)
{
     AN=copy_test.AN
     BN=copy_test.BN
     CN=copy_test.CN
}
//- - - - - - - - - - - - - - - - - - - - - - -



Test::~Test()
{
}

// Function
 void Just_a_test()
{
    ...just do something with AN, BN and CN
}




//####################################################
Please let me know. How can I select/start  the Copy Consturctor from my Dlg.cpp ?
I very appreciate your help.
Best regards,
Thomas
AlexFMCommented:
Test& operator=(const Test& copy_test);   // h-file

// implementation:
Test& Test::operator=(const Test &copy_test)
{
     AN=copy_test.AN;
     BN=copy_test.BN;
     CN=copy_test.CN;

     return *this;
}

For Test class, operator= works like default operator=, you don't need it. But if class contains some non-trivial data (like pointers), it is necessary to write operator=.
Operator= returns Test& to compile multiple assignments, for example:
Test t1, t2, t3;

t1 = t2 = t3;    // this line is not compiled if operator= returns void

Usually operator= and copy constructor have the same code, which can be moved to another function called from both of them. But they are called in different situations.

Test t1;
Test t2(t1);   // copy constructor is called for t2

t2 = t1;       // operator= is called for t1
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

AlexFMCommented:
This is the same class with char* member. See how copy constructor and operator= allocate space for this member:

class Test
{
    public:
    double AN;
    double BN;
    double CN;
    char* name;

    // Constructor
    Test(double a, double b, double c, char* name);

//- - - - - - - - - - - - - - - - - - - - - - -
    //Copy Consturctor
    Test(const Test &copy_test);
//- - - - - - - - - - - - - - - - - - - - - - -


   // Destructor
    ~Test();

    Test& operator=(const Test& copy_test);

    //Function
    void Just_a_test();

};

Test::Test(double a, double b, double c, char* name)
{
    AN=a;
    BN=b;
    CN=c;

    this->name = new char[strlen(name) + 1];
    strcpy(this->name, name);
}
//- - - - - - - - - - - - - - - - - - - - - - -
Test::Test(const Test &copy_test)
{
     AN=copy_test.AN;
     BN=copy_test.BN;
     CN=copy_test.CN;

    this->name = new char[strlen(copy_test.name) + 1];
    strcpy(this->name, copy_test.name);
}

Test& Test::operator=(const Test &copy_test)
{
     AN=copy_test.AN;
     BN=copy_test.BN;
     CN=copy_test.CN;

     delete[] name;
     this->name = new char[strlen(copy_test.name) + 1];
     strcpy(this->name, copy_test.name);


     return *this;
}


//- - - - - - - - - - - - - - - - - - - - - - -



Test::~Test()
{
    delete[] name;
}

// Function
void Test::Just_a_test()
{
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Thomas StockbrueggerAuthor Commented:
Hi Alex,
wow...thanks for your super fast answer.
I will try this....and let you know...tomorrow.

Thank you very much.
I very appreciate your help.

Have a great day.
Best regards,
Thomas

Thomas StockbrueggerAuthor Commented:
Hi AlexFM,
I have tried your code...yes it works.
Thank you very much for your detailed information.
I now understand a copy constructor and operator=
I very appreciated you help.

Keep up the good work at experts exchange !!!
I have just added 500 points with grade A.
Thank you Alex.

Best regards,
Thomas
tsp2002
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.