Help with C++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 16
Reputation: vladdy191 is an unknown quantity at this point 
Solved Threads: 0
vladdy191 vladdy191 is offline Offline
Newbie Poster

Help with C++

 
0
  #1
Nov 22nd, 2008
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help with C++

 
0
  #2
Nov 23rd, 2008
>>I don't have a C++ compiler to run it
Download one of the free ones.

>>a = c;
That's illegal.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Help with C++

 
0
  #3
Nov 23rd, 2008
Originally Posted by Ancient Dragon View Post
>>a = c;
That's illegal.
Have you tried compiling it?
Last edited by dougy83; Nov 23rd, 2008 at 2:51 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC