|
-
May 15th, 2008, 09:07 PM
#1
C++ vs Java
How does abstract classes and dynamic storage differ in C++ from Java?
thanks
-
May 15th, 2008, 09:32 PM
#2
Re: C++ vs Java
 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.
-
May 15th, 2008, 09:57 PM
#3
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.
-
May 16th, 2008, 04:00 AM
#4
Re: C++ vs Java
Abstract Class in C++
Code:
class MyClass
{
private:
MyClass(){....}
.....
public:
......
}
Constructor cant be accessed
-
May 16th, 2008, 04:10 AM
#5
Re: C++ vs Java
No, an abstract class in C++ is a class with at least one pure virtual function.
-
May 17th, 2008, 02:46 AM
#6
Re: C++ vs Java
 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.
-
May 18th, 2008, 02:15 AM
#7
Re: C++ vs Java
 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.
-
May 19th, 2008, 09:14 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|