c++

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2008
Posts: 24
Reputation: balakrishnan.kb is an unknown quantity at this point 
Solved Threads: 0
balakrishnan.kb balakrishnan.kb is offline Offline
Newbie Poster

c++

 
0
  #1
Sep 18th, 2008
what is the difference between virutual keyword in c++ and final in java
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,868
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: c++

 
1
  #2
Sep 18th, 2008
Assuming you mean final as applied to methods, the two are basically opposites due to default virtualness. In C++, member functions are not virtual by default, so the virtual keyword is used to enable virtual behavior. In Java, methods are virtual by default, so the final keyword is used to disable virtual behavior.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: c++

 
0
  #3
Sep 18th, 2008
To make an example of the difference via implementation--

  1. #include <iostream>
  2. #include <memory>
  3.  
  4. using std::cout;
  5. using std::cin;
  6. using std::ostream;
  7. using std::auto_ptr;
  8.  
  9. template<bool>class B;
  10. template<bool>class C;
  11. template<bool>class D;
  12.  
  13. class A{
  14. public:
  15. ostream& run(ostream& out = cout){
  16. return out << "Class A\n";
  17. }
  18. };
  19.  
  20. template<> // when true, run is virtual, when false run is not virtual
  21. class B<true> : public A{
  22. public:
  23. virtual ostream& run(ostream& out = cout){
  24. return out << "Class B\n";
  25. }
  26. };
  27.  
  28. template<>
  29. class B<false> : public A{
  30. public:
  31. ostream& run(ostream& out = cout){
  32. return out << "Class B\n";
  33. }
  34. };
  35.  
  36. template<> // when true, run is virtual. When false, run is non-virtual
  37. class C<true> : public B<true>{
  38. public:
  39. virtual ostream& run(ostream& out = cout){
  40. return out << "Class C\n";
  41. }
  42. };
  43.  
  44. template<>
  45. class C<false> : public B<false>{
  46. public:
  47. ostream& run(ostream& out = cout){
  48. return out << "Class C\n";
  49. }
  50. };
  51.  
  52. template<> // no difference between true and false in this case
  53. class D<true> : public C<true>{
  54. public:
  55. ostream& run(ostream& out = cout){
  56. return out << "Class D\n";
  57. }
  58. };
  59.  
  60. template<>
  61. class D<false> : public C<false>{
  62. public:
  63. ostream& run(ostream& out = cout){
  64. return out << "Class D\n";
  65. }
  66. };
  67.  
  68. int main(){
  69.  
  70. auto_ptr< B<true> > test1 (new D<true>); // B virtual run is true
  71. auto_ptr< B<false> > test2 (new D<false>); // B virtual run is false (it isn't virtual)
  72. auto_ptr< B<true> > test3 (new C<true>); // B virtual run is true
  73. auto_ptr< B<false> > test4 (new C<false>); // B virtual run is false
  74.  
  75. test1->run();
  76. test2->run();
  77. test3->run();
  78. test4->run();
  79.  
  80. cout << "\n\n";
  81.  
  82. D<true>* p1 = dynamic_cast< D<true>* >( test1.get() );
  83. // D<false>* p2 = dynamic_cast< D<false>* >( test2.get() ); // gives 'B<false> is not polymorphic type' error
  84. C<true>* p3 = dynamic_cast< C<true>* >( test3.get() );
  85. // C<false>* p4 = dynamic_cast< C<false>* >( test4.get() ); // gives 'B<false> is not polymorphic type' error
  86.  
  87. p1->run();
  88. // p2->run();
  89. p3->run();
  90. // p4->run();
  91.  
  92. cin.get();
  93. return 0;
  94. }

...

  1. import java.io.*;
  2.  
  3. public class NamespaceClass_1{
  4.  
  5. public static void main(String... args){
  6. NamespaceClass_1 nsc1 = new NamespaceClass_1();
  7. NamespaceClass_1.A a1 = nsc1.new A(), a2 = nsc1.new B(),
  8. a3 = nsc1.new C(), a4 = nsc1.new D();
  9. final OutputStream out = System.out;
  10.  
  11. a4.run(a3.run(a2.run(a1.run(out)))); // Run til we run out! =P
  12. }
  13.  
  14. class A{
  15. /*virtual*/ OutputStream run(OutputStream out){
  16. PrintStream ps = new PrintStream(out);
  17. ps.println("Class A");
  18. return ps;
  19. }
  20. }
  21.  
  22. class B extends A{
  23. @Override final OutputStream run(OutputStream out){
  24. PrintStream ps = new PrintStream(out);
  25. ps.println("Class B");
  26. return ps;
  27. }
  28. }
  29.  
  30. class C extends B{
  31. /*
  32. void run(OutputStream out){
  33. PrintStream ps = new PrintStream(out);
  34. ps.println("Class C");
  35. }*/ // cannot override final run method
  36. }
  37.  
  38. class D extends C{
  39. /*
  40. void run(OutputStream out){
  41. PrintStream ps = new PrintStream(out);
  42. ps.println("Class D");
  43. }*/ // cannot override final run method
  44. }
  45. }
Last edited by Alex Edwards; Sep 18th, 2008 at 9:18 pm.
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


Views: 403 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC