943,641 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 424
  • C++ RSS
Nov 22nd, 2008
0

Help with C++

Expand Post »
I'm looking at this code and I'm not sure what it prints (I don't have a C++ compiler to run it). However, I also would like to understand why it prints what it does? hope someone can help, here's the code:
C++ Syntax (Toggle Plain Text)
  1. class A
  2. {
  3. public:
  4. virtual void p()
  5. { cout << "A.p" << endl; }
  6. void q()
  7. { cout << A.q" << endl; }
  8. virtual void r ()
  9. { p(); q(); }
  10. };
  11.  
  12. class B : public A
  13. {
  14. public:
  15. void p()
  16. { cout << "B.p" << endl; }
  17. };
  18. class C : public B
  19. {
  20. public:
  21. void q()
  22. { cout << "C.q" << endl; }
  23. void r ()
  24. { q(); p(); }
  25. };
  26.  
  27. ...
  28. A a;
  29. C c;
  30. a = c;
  31. a.r();
  32. A* ap=new B;
  33. ap -> r();
  34. ap = new C;
  35. ap -> r();
  36.  

I guess I'm most interested in knowing what funtion a.r() is going to call since right before we said had a=c. Will it call class A's function or class C's. I have no idea what A* ap = new B will do.
Reputation Points: 13
Solved Threads: 0
Newbie Poster
vladdy191 is offline Offline
16 posts
since Nov 2008
Nov 23rd, 2008
0

Re: Help with C++

>>I don't have a C++ compiler to run it
Download one of the free ones.

>>a = c;
That's illegal.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Nov 23rd, 2008
0

Re: Help with C++

>>a = c;
That's illegal.
Have you tried compiling it?
Last edited by dougy83; Nov 23rd, 2008 at 2:51 am.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Table
Next Thread in C++ Forum Timeline: How to generate an uniqe number always?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC