CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: C++ vs Java

  1. #1
    Join Date
    Oct 2007
    Posts
    41

    C++ vs Java

    How does abstract classes and dynamic storage differ in C++ from Java?

    thanks

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: C++ vs Java

    Quote Originally Posted by JohnSmith70
    How does abstract classes and dynamic storage differ in C++ from Java?

    thanks
    Dynamic storage in java = garbage collection, dynamic storage in C++ = no garbage collection. In C++ you allocate and free dynamic memory like so:
    Code:
    int* i = new int(1);
      delete i;
    
      int* a = new int[5];
      delete[] a;
      a = NULL; // Make sure the pointer isn't accessed again.
    Is that what you meant? I haven't had much experience with abstract classes in Java but in C++ to make an abstract class you'll need a pure virtual function:
    Code:
    class B
    {
    public:
      virtual void Pure_Virtual_Func() = 0;
    };
    Edit: There are libraries that provide alternatives to manually handing dynamic memory in C++ such as STL and boost. Read this, this, this and this for more information.
    Last edited by Mybowlcut; May 15th, 2008 at 09:41 PM.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  3. #3
    Join Date
    Oct 2006
    Location
    Singapore
    Posts
    346

    Re: C++ vs Java

    Abstract classes in Java use the abstract keyword. In C++, there is no such keyword. In C++, for a class to be called abstract, it must contain at least one pure virtual function. In Java, an abstract class may or may not contain abstract methods (The Java terminology for pure virtual functions). Irrespective of the language, abstract classes cannot be instantiated.
    Believe in your Dreams, Work for what you Believe in.
    My thoughts? Angelo's Stuff
    Some fun things I've done: RayWatch, QuickFeed, ACSVParser

    @ngelo

  4. #4
    Join Date
    Jan 2008
    Posts
    32

    Re: C++ vs Java

    Abstract Class in C++
    Code:
    class MyClass
    {
    private:
       MyClass(){....}
       .....
    public:
      ......
    }
    Constructor cant be accessed

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: C++ vs Java

    No, an abstract class in C++ is a class with at least one pure virtual function.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: C++ vs Java

    Quote Originally Posted by Revne
    Constructor cant be accessed
    Putting the CTOR in private area would also prevent it from being inherited. It should be protected, for this tricky 'abstract' to work.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  7. #7
    Join Date
    Nov 2003
    Posts
    1,405

    Re: C++ vs Java

    Quote Originally Posted by JohnSmith70
    How does abstract classes and dynamic storage differ in C++ from Java?
    In Java when a class has an abstract method (no body) the class must be declared abstract using the abstract keyword. Java also has a special kind of class called an interface which is a totally abstract class where all methods are abstract.

    In Java all methods (apart from static, final and private) are virtual by default. To make a method non-virtual it is declared final (cannot be overridden). So Java and C++ have opposite defaults. In Java the default is virtual, in C++ it's non-virtual.

    In Java garbage collection is built into the language. Objects must not be explicitly deleted.

    C++ relies on manual deletion of dynamically allocated objects or, as an alternative, so called reference counting smart pointers (which are, and will be more so, defined by the standard library). Garbage collection must be provided by a third party library.

    In C++ pointers to objects represent actual memory locations. Not so in Java where objects may be moved around by the garbage collector.
    Last edited by _uj; May 18th, 2008 at 03:57 AM.

  8. #8
    Join Date
    Jul 2003
    Posts
    7

    Re: C++ vs Java

    In JAVA - abstract is a keyword that, when added to a class definition, disallows the instantiation of a class. Interface is similiar only it forces the class to have only signatures of methods and no attributes.

    In C++ - its the same idea but no keywords. Once you define a pure virtual function, the class cannot be instantiated. The idea of interface exists as well, if you don't supply a body to non of the functions, the class is actually an interface. (abstract class is, in a way, a generalization of interfaces and "normal" classes).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured