testing overloaded assignment operator within a class

Posted on 2005-05-11
Last Modified: 2010-04-17

I am trying to create a test class to test my overloaded functions and can't seem to call the operator functions correctly.

Here is my function for the assignment operator test for my complex number program:

void ComplexTest::test_AssignmentOp(const Complex& ct)
      cout << "Testing complex assignemnt operator..." << endl;
      if (_complex.operator =(ct.operator =)
            cout << "Passes assignment operator test!" << endl;
      else cout << "Failed assignment operator test!" << endl;
How do I test that opertaor-what needs to go on the right of it.  The program works fine I am just trying to add this test class to it.  Any ideas?  Thanks!
Question by:dauler
    LVL 2

    Accepted Solution


    There are many thing you can test with regard to assignment operator.
    You need to first decide what exactly do you want to test.
    A reasonable thing to test is that after you assign an A to a B (i.e. B=A)
    then they are equal (i.e. A==B).

    So this uggest that the test function will assign one complex to the other,
    and then compare them, If they differ, the test failed.

    Here is the suggested code :

    void ComplexTest::test_AssignmentOp(const Complex& ct)
    /* Purpose : verify that the assignment operator works as expected.
     * Method : Assign a given complex object to a local object and verify
     * that after the assignment, both have the exact same value

      cout << "Testing complex assignemnt operator..." << endl;
      Complex lhs; // create a complex on stack, using default Ctor.
      lhs.operator=(ct); // assign the Complex given as parameter
      // verify that both complex are the same :
      if (lhs == ct ) // using operator == to compare
        cout << "Passes assignment operator test!" << endl;
        cout << "Failed assignment operator test!" << endl;

    Of ourse, a shorter notation for the actuall assignment is :
      lhs = ct;

    Hope this help,

    Author Comment

    Thanks beer7child(great moniker!)

    That works-thank you.  However, testing the other operators such as +, - , * gives me an error message with the conditional statement.  The compiler says that conditional satements are not allowed with Complex type-any ideas?

    LVL 2

    Expert Comment


    I'm glad you find my suggestion helpfull, and thanks for the points.

    If you can post the actuall source code that is causing the compilation error
    and the exact compiler message, I might be able to assist.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    This is an explanation of a simple data model to help parse a JSON feed
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now