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
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
You probably made the print member function non-virtual when adapting my example. Try this:Originally Posted by -EquinoX-
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); }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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:Originally Posted by -EquinoX-
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; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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?Originally Posted by -EquinoX-
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
What did you try?Originally Posted by -EquinoX-
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.Originally Posted by -EquinoX-
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
You need to post hw06-question2.h as well.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
You did not declare A's print member function as virtual.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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.Originally Posted by -EquinoX-
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).
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)