How does abstract classes and dynamic storage differ in C++ from Java?
thanks
Printable View
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:Quote:
Originally Posted by JohnSmith70
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:int* i = new int(1);
delete i;
int* a = new int[5];
delete[] a;
a = NULL; // Make sure the pointer isn't accessed again.
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.Code:class B
{
public:
virtual void Pure_Virtual_Func() = 0;
};
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.
Abstract Class in C++
Constructor cant be accessed ;)Code:class MyClass
{
private:
MyClass(){....}
.....
public:
......
}
No, an abstract class in C++ is a class with at least one pure virtual function.
Putting the CTOR in private area would also prevent it from being inherited. It should be protected, for this tricky 'abstract' to work.Quote:
Originally Posted by Revne
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.Quote:
Originally Posted by JohnSmith70
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.
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).