assignment constructor overloading

This is a discussion on assignment constructor overloading within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by -EquinoX- so I don't need an implementation of A? I am not sure. The problem is, I ...

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Quote Originally Posted by -EquinoX-
    so I don't need an implementation of A?
    I am not sure. The problem is, I do not have enough information to decide if inheritance is needed here. You say it is, so okay, we can go with that. But then you apparently have a bad habit of creating A objects when you do not intend to, so perhaps you should be using A as just an interface.

    Maybe we should work on your class design first instead of trying to fix this minor problem of why you end up with 0 printed instead of 2. So, what are the real names of A and B? What exactly are you trying to model?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #32
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Okay, so what is the main function and any function that it calls?

    EDIT:
    Quote Originally Posted by -EquinoX-
    and the main code that I can't change... I can only change the implementation, that's the whole purpose
    Right. Now I understand the point of this exercise: there is no point. Your teacher is just wasting your time. In fact, your teacher is doing worse: he/she is teaching you bad design and bad practices.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #33
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    see my edited post above

  4. #34
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by laserlight View Post
    Okay, so what is the main function and any function that it calls?

    EDIT:

    Right. Now I understand the point of this exercise: there is no point. Your teacher is just wasting your time. In fact, your teacher is doing worse: he/she is teaching you bad design and bad practices.
    I don't know about that, I am just following instructions...

  5. #35
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Okay, this is my idea: the print member function should not be virtual since the output of a.print() in printit() appears to be always from A::print(). You can have a protected value member variable of type int in A, and no member variables declared for B. The copy assignment operator for A should not copy over value.

    I found it mind boggling to think of what exactly should be done to make it such that b1.print() gives $2 yet printit(b1) does not, so I came up with a simple hack: add a protected flag member variable of type bool in A. Overload the copy assignment operator for B such that it copies over the value and sets flag to true. Now, in A::print(), print 0 if flag is true, else print value. B::print() unconditionally prints value.

    Oh, and I have ignored the simplest hack of all:
    Code:
    class A
    {
    public:
        void print() const {}
    };
    
    class B : public A
    {
    public:
        B(int x = 0) {}
    
        void print() const
        {
            std::cout << "Just got my paycheck!\n"
                        "Direct deposit not working :(\n"
                        "Just got my paycheck!\n"
                        "Direct deposit not working :(\n"
                        "Withdrawing funds.\n"
                        "Just got my paycheck!\n"
                        "Depositing funds.\n"
                        "Funds withdrawn: $2\n"
                        "Funds deposited: $0\n"
                        "Funds deposited: $0\n"
                        "Funds deposited: $2\n"
                        "Funds deposited: $0\n"
                        "No need to find another ATM.\n"
                        "Now I have to drive to the bank across town.\n"
                        "No need to find another ATM.\n"
                        "Now I have to drive to the bank across town.\n"
                        "No need to find another ATM.\n";
        }
    };
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #36
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Quote Originally Posted by -EquinoX-
    so A has an int value and B doesn't have an int value and A's copy assignment will do nothing?
    B inherits A's int member variable, presumably named value. A's copy assignment will not copy anything: it will just print a message.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #37
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so B can access A's value and flag if it's protected?

  8. #38
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Quote Originally Posted by -EquinoX-
    so B can access A's value and flag if it's protected?
    Yes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #39
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Okay, I just tried what you suggested and it works... I thought of your implementation before of using a flag...which is kind of a hack... I was wondering if there was a more elegant way to do this

  10. #40
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Quote Originally Posted by -EquinoX-
    I thought of your implementation before of using a flag...which is kind of a hack...
    Yes, it is a hack.

    Quote Originally Posted by -EquinoX-
    I was wondering if there was a more elegant way to do this
    Don't bother. The whole assignment is a farce.

    I guess that it might teach you something about reasoning about what you might need to do to maintain a poorly written program that you cannot change certain parts of because you are afraid of breaking more stuff, but then the solution would be to implement the hack along with tests, and then refactor the stuff that you were afraid to change, using the tests as a guide to whether you broke anything.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #41
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by laserlight View Post
    Yes, it is a hack.


    Don't bother. The whole assignment is a farce.

    I guess that it might teach you something about reasoning about what you might need to do to maintain a poorly written program that you cannot change certain parts of because you are afraid of breaking more stuff, but then the solution would be to implement the hack along with tests, and then refactor the stuff that you were afraid to change, using the tests as a guide to whether you broke anything.
    thank you for all of your help, I appreciate it

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 08:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21