| | |
c++
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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
To make an example of the difference via implementation--
...
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <memory> using std::cout; using std::cin; using std::ostream; using std::auto_ptr; template<bool>class B; template<bool>class C; template<bool>class D; class A{ public: ostream& run(ostream& out = cout){ return out << "Class A\n"; } }; template<> // when true, run is virtual, when false run is not virtual class B<true> : public A{ public: virtual ostream& run(ostream& out = cout){ return out << "Class B\n"; } }; template<> class B<false> : public A{ public: ostream& run(ostream& out = cout){ return out << "Class B\n"; } }; template<> // when true, run is virtual. When false, run is non-virtual class C<true> : public B<true>{ public: virtual ostream& run(ostream& out = cout){ return out << "Class C\n"; } }; template<> class C<false> : public B<false>{ public: ostream& run(ostream& out = cout){ return out << "Class C\n"; } }; template<> // no difference between true and false in this case class D<true> : public C<true>{ public: ostream& run(ostream& out = cout){ return out << "Class D\n"; } }; template<> class D<false> : public C<false>{ public: ostream& run(ostream& out = cout){ return out << "Class D\n"; } }; int main(){ auto_ptr< B<true> > test1 (new D<true>); // B virtual run is true auto_ptr< B<false> > test2 (new D<false>); // B virtual run is false (it isn't virtual) auto_ptr< B<true> > test3 (new C<true>); // B virtual run is true auto_ptr< B<false> > test4 (new C<false>); // B virtual run is false test1->run(); test2->run(); test3->run(); test4->run(); cout << "\n\n"; D<true>* p1 = dynamic_cast< D<true>* >( test1.get() ); // D<false>* p2 = dynamic_cast< D<false>* >( test2.get() ); // gives 'B<false> is not polymorphic type' error C<true>* p3 = dynamic_cast< C<true>* >( test3.get() ); // C<false>* p4 = dynamic_cast< C<false>* >( test4.get() ); // gives 'B<false> is not polymorphic type' error p1->run(); // p2->run(); p3->run(); // p4->run(); cin.get(); return 0; }
...
java Syntax (Toggle Plain Text)
import java.io.*; public class NamespaceClass_1{ public static void main(String... args){ NamespaceClass_1 nsc1 = new NamespaceClass_1(); NamespaceClass_1.A a1 = nsc1.new A(), a2 = nsc1.new B(), a3 = nsc1.new C(), a4 = nsc1.new D(); final OutputStream out = System.out; a4.run(a3.run(a2.run(a1.run(out)))); // Run til we run out! =P } class A{ /*virtual*/ OutputStream run(OutputStream out){ PrintStream ps = new PrintStream(out); ps.println("Class A"); return ps; } } class B extends A{ @Override final OutputStream run(OutputStream out){ PrintStream ps = new PrintStream(out); ps.println("Class B"); return ps; } } class C extends B{ /* void run(OutputStream out){ PrintStream ps = new PrintStream(out); ps.println("Class C"); }*/ // cannot override final run method } class D extends C{ /* void run(OutputStream out){ PrintStream ps = new PrintStream(out); ps.println("Class D"); }*/ // cannot override final run method } }
Last edited by Alex Edwards; Sep 18th, 2008 at 9:18 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: output window for c++!!! help!!
- Next Thread: C++ Dice Roll Game
Views: 402 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






