Thread: assignment constructor overloading

  1. #16
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I don't know if a has a corresponding variable or not...I am guessing it has, so in my assignment operator I have this.value = b.value

  2. #17
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by laserlight View Post
    If this is with respect to my definitions of A and B in post #7, then it is as simple as:
    Code:
    printit(b2);
    it doesn't work, it print's A print method
    Last edited by -EquinoX-; 10-28-2009 at 10:44 AM.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by -EquinoX-
    it doesn't work, it print's A print method
    You probably made the print member function non-virtual when adapting my example. Try this:
    Code:
    #include <ostream>
    
    class A
    {
    public:
        virtual ~A() {}
    
        virtual void print(std::ostream& out) const
        {
            out << "A";
        }
    };
    
    class B : public A
    {
    public:
        explicit B(int x) : x(x) {}
    
        virtual void print(std::ostream& out) const
        {
            out << "B: " << x;
        }
    private:
        int x;
    };
    
    #include <iostream>
    
    void printit(const A& a)
    {
        a.print(std::cout);
        std::cout << std::endl;
    }
    
    int main()
    {
        A a;
        printit(a);
    
        B b(2);
        printit(b);
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by laserlight View Post
    You probably made the print member function non-virtual when adapting my example. Try this:
    Code:
    #include <ostream>
    
    class A
    {
    public:
        virtual ~A() {}
    
        virtual void print(std::ostream& out) const
        {
            out << "A";
        }
    };
    
    class B : public A
    {
    public:
        explicit B(int x) : x(x) {}
    
        virtual void print(std::ostream& out) const
        {
            out << "B: " << x;
        }
    private:
        int x;
    };
    
    #include <iostream>
    
    void printit(const A& a)
    {
        a.print(std::cout);
        std::cout << std::endl;
    }
    
    int main()
    {
        A a;
        printit(a);
    
        B b(2);
        printit(b);
    }
    maybe I am not a bit clear... I want it to print out using A's print but printing out B's value...

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by -EquinoX-
    maybe I am not a bit clear... I want it to print out using A's print but printing out B's value...
    In that case, your design is simply wrong. B should not be derived from A. Rather, A and B should be separate, but you could have a B as a member of A:
    Code:
    #include <ostream>
    
    class B
    {
    public:
        explicit B(int x) : x(x) {}
    
        int get() const
        {
            return x;
        }
    private:
        int x;
    };
    
    class A
    {
    public:
        explicit A(const B& b) : b(b) {}
    
        virtual void print(std::ostream& out) const
        {
            out << "A: " << b.get();
        }
    private:
        B b;
    };
    
    #include <iostream>
    
    int main()
    {
        A a(B(2));
        a.print(std::cout);
        std::cout << std::endl;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by laserlight View Post
    In that case, your design is simply wrong. B should not be derived from A. Rather, A and B should be separate, but you could have a B as a member of A:
    Code:
    #include <ostream>
    
    class B
    {
    public:
        explicit B(int x) : x(x) {}
    
        int get() const
        {
            return x;
        }
    private:
        int x;
    };
    
    class A
    {
    public:
        explicit A(const B& b) : b(b) {}
    
        virtual void print(std::ostream& out) const
        {
            out << "A: " << b.get();
        }
    private:
        B b;
    };
    
    #include <iostream>
    
    int main()
    {
        A a(B(2));
        a.print(std::cout);
        std::cout << std::endl;
    }
    no I am pretty sure that B has to be derived from A... I am just trying to figure out how to make this happen....

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by -EquinoX-
    no I am pretty sure that B has to be derived from A
    If you are sure that B has to be derived from A, then you are sure that A should have at least one virtual function. As such, why can you not use a virtual print function?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I did use print as a virtual function.. it did not work though.. here's my code:

    2shared - download code.txt

    in the code when calling printit(b2) it did call A's print, which is "funds blah..blah (which is right)" but then the value is wrong.. (it's 0 instead of 2, while b2's value is 2) that's my only problem
    Last edited by -EquinoX-; 10-28-2009 at 11:12 AM.

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by -EquinoX-
    I did use print as a virtual function.. it did not work though..
    What did you try?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by -EquinoX-
    in the code when calling printit(b2) it did call A's print, which is "funds blah..blah (which is right)" but then the value is wrong.. (it's 0 instead of 2, while b2's value is 2) that's my only problem
    Post the smallest and simplest compilable code that demonstrates the problem. If you really cannot post that, then post your code here, using the file attachment feature if necessary. Frankly, it took me too long to figure out how to download from 2shared, so I cannot be bothered.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to post hw06-question2.h as well.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You did not declare A's print member function as virtual.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    If I do a's print as virtual it would print funds withdrawn instead of funds deposited.... I want it to print funds deposited... and when I do b2.print I want it to print funds withdrawn...
    It's so complicated
    Last edited by -EquinoX-; 10-28-2009 at 11:59 AM.

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by -EquinoX-
    I did try all possibilites of virtual and it still doesn't print b2's value as 2
    I think that it is likely that you still have a type slicing problem. Another potential problem is that you have a public member variable named value in B. This hides the value member variable inherited from A.

    I do not understand why you need to use inheritance, but this is my remedy for your type slicing problem: turn A into an abstract base class, e.g., declare its print member function as pure virtual, or declare its destructor as pure virtual (but keep the implementation of A's destructor).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by laserlight View Post
    I think that it is likely that you still have a type slicing problem. Another potential problem is that you have a public member variable named value in B. This hides the value member variable inherited from A.

    I do not understand why you need to use inheritance, but this is my remedy for your type slicing problem: turn A into an abstract base class, e.g., declare its print member function as pure virtual, or declare its destructor as pure virtual (but keep the implementation of A's destructor).
    so I don't need an implementation of A? and I can't do A a; ?
    Last edited by -EquinoX-; 10-28-2009 at 12:08 PM.

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, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM