943,915 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 453
  • C++ RSS
Sep 18th, 2008
0

c++

Expand Post »
what is the difference between virutual keyword in c++ and final in java
Reputation Points: 10
Solved Threads: 0
Newbie Poster
balakrishnan.kb is offline Offline
24 posts
since Aug 2008
Sep 18th, 2008
1

Re: c++

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 18th, 2008
0

Re: c++

To make an example of the difference via implementation--

c++ Syntax (Toggle Plain Text)
  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. }

...

java Syntax (Toggle Plain Text)
  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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008

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: output window for c++!!! help!!
Next Thread in C++ Forum Timeline: Please help with link list reading back the list





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


Follow us on Twitter


© 2011 DaniWeb® LLC