J
jbzdak
Recently, I have been learning Java (it is one of laboratories on my
department), well I know Java is awfully slow, but it is not the point
that I'm interested.
Java has a keyword "interface. (and if you know what it is skip this
paragraph), that is an abstract class, whose every function is public
and purely virtual, and every field is public and static. Java has its
own reasons for creating that keyword - you can only inherit from one
non-interface class, but from infinite interfaces.
The main question is whether using similar classes in C++ is considered
a good practice. I know that every abstract function has a small
overhead on every time you use it. But are there other cons?
I had an usage for it (in Java program). I had to manage a small
database, small enough to store it in a single file, and load it whole
into memory. I created an interface Product, that and implemented it in
SmipleProduct which is a straightforward, or even blunt,
implementation. And every function in SimpleProduct that would use an
instance of products gets a java-pointer to Product. Also I had a
couple of classes that used the Product interface. And I had created an
interface class for Product data base (that also gets only
java-pointers of Product)
Looks like that:
interface Product{
//(...)
}
class SimpleProduct{
SimpleProduct(Product foo){ /*(...)*/};
//(...)
}
interface ProductDataBase{
void add (Product);
Product findById(int id);
//(...)
}
And if I the database had grown a lot I would had just to re-implement
classes representing DataBase using for example mySQL. And whole GUI,
and classes used to compare
products would be left untouched.
The second question is whether it is possible to get similar result -
easiness of re-implementation of parts of program - using other,
better, techniques.
department), well I know Java is awfully slow, but it is not the point
that I'm interested.
Java has a keyword "interface. (and if you know what it is skip this
paragraph), that is an abstract class, whose every function is public
and purely virtual, and every field is public and static. Java has its
own reasons for creating that keyword - you can only inherit from one
non-interface class, but from infinite interfaces.
The main question is whether using similar classes in C++ is considered
a good practice. I know that every abstract function has a small
overhead on every time you use it. But are there other cons?
I had an usage for it (in Java program). I had to manage a small
database, small enough to store it in a single file, and load it whole
into memory. I created an interface Product, that and implemented it in
SmipleProduct which is a straightforward, or even blunt,
implementation. And every function in SimpleProduct that would use an
instance of products gets a java-pointer to Product. Also I had a
couple of classes that used the Product interface. And I had created an
interface class for Product data base (that also gets only
java-pointers of Product)
Looks like that:
interface Product{
//(...)
}
class SimpleProduct{
SimpleProduct(Product foo){ /*(...)*/};
//(...)
}
interface ProductDataBase{
void add (Product);
Product findById(int id);
//(...)
}
And if I the database had grown a lot I would had just to re-implement
classes representing DataBase using for example mySQL. And whole GUI,
and classes used to compare
products would be left untouched.
The second question is whether it is possible to get similar result -
easiness of re-implementation of parts of program - using other,
better, techniques.