Shiladitya Bose
Shiladitya Bose

Reputation: 1203

Is Abstract class an example of Abstract data type?

I'm getting confused by these two. What I learned is that Abstract data type is a mathematical model for data type, where it specifies the objects and the methods to manipulate these objects without specifying the details about the implementation of the objects and methods. Ex: an abstract stack model defines a stack with push and pop operations to insert and delete items to and from the stack. We can implement this in many ways, by using linked lists, arrays or classes.

Now, coming to the definition of abstract class, its a parent class which has one or more methods that doesn't have definition(implementation?) and cannot be instantiated (much like we can't implement an abstract stack as it is, without defining the stack's underlying mechanism through one of the concrete data structures). For ex: if we have an abstract class called Mammal which includes a function called eat(), we don't know how a mammal eats because a mammal is abstract. Although we can define eat() for a cow which is a derived class of mammal. Does this mean that mammal serves as an adt and cow class is an implementation of the mammal adt?

Correct me if I'm wrong in any way. Any kind of help would be really appreciated.

Upvotes: 0

Views: 4058

Answers (3)

WindyFields
WindyFields

Reputation: 2875

Abstract data type is a mathematical model for data type...

Now, coming to the definition of abstract class...

You need to distinguish between theoretical mathematical models and a practical implementation techniques.

Models are created by people in order to reason about problems easily, in some comprehensible, generalized way.

Meanwhile, the actual code is written in order to work and get the job done.

"Abstract data type" is a model. "Abstract class" is a programming technique which some programming languages (C++, C#, Java) support on the language level.

"Abstract data type" lets you think and talk about the solution of a problem, without overloading your brain with unnecessary (at this moment) implementation details. When you need a FIFO data structure, you say just "stack", but not "a doubly-linked list with the pointer to the head node and the ability to...".

"Abstract class" lets you write the code once and then reuse it later (because that is the point of OOP - code reuse). When you see that several types have a common interface and functionality - you may create "an abstract class" and put the intersection of their functionality in inside, while still being able to rely on yet unimplemented functions, which will be implemented by some concrete type later. This way, you write the code once and when you need to change it later - it's only one place to make the change in.

Note:
Although, in C++ ISO Standard (at least in the draft) there is a note:

Note: The abstract class mechanism supports the notion of a general concept, such as a shape, of which only more concrete variants, such as circle and square, can actually be used.

but it is just a note. The real definition is:

A class is abstract if it has at least one pure (aka unimplemented) virtual function.

which leads to the obvious constraint:

no objects of an abstract class can be created except as subobjects of a class derived from it

Personally, I like that C++ (unlike C# and Java) doesn't have the keyword "abstract". It only has type inheritance and virtual functions (which may remain unimplemented). This helps you focus on a practical matter: inherit where needed, override where necessary.

In a nutshell, using OOP - be pragmatic.

Upvotes: 3

Christian Hackl
Christian Hackl

Reputation: 27538

Is Abstract class an example of Abstract data type?

Yes, but in C++, abstract classes have become an increasingly rare example of abstract data types, because generic programming is often a superior alternative.

Ex: an abstract stack model defines a stack with push and pop operations to insert and delete items to and from the stack. We can implement this in many ways, by using linked lists, arrays or classes.

The C++ std::stack class template more or less works like this. It has member functions push and pop, and it's implemented in terms of the Container type parameter, which defaults to std::deque.

For an implementation with a linked list, you'd type std::stack<int, std::list<int>>. However, arrays cannot be used to implement a stack, because a stack can grow and shrink, and arrays have a fixed size.

It's very important to understand that the std::stack has absolutely nothing to do with abstract classes or runtime polymorphism. There's not a single virtual function involved.

Now, coming to the definition of abstract class, its a parent class which has one or more methods that doesn't have definition(implementation?) and cannot be instantiated

Yes, that's precisely the definition of an abstract class in C++.

In theory, such a stack class could look like this:

template <class T>
class Stack
{
public:
    virtual ~Stack() = 0;
    virtual void push(T const& value) = 0;
    virtual T pop() = 0;
};

In this example, the element type is still generic, but the implementation of the container is meant to be provided by a concrete derived class. Such container designs are idiomatic in other languages, but not in C++.

much like we can't implement an abstract stack as it is, without defining the stack's underlying mechanism through one of the concrete data structures

Yes, you couldn't use std::stack without providing a container type parameter (but that's impossible anyway, because there's the default std::deque parameter), and you cannot instantiate a Stack<int> my_stack; either.

Upvotes: 0

Klaus
Klaus

Reputation: 25623

The term "abstract data type" is not directly related to anything in C++. So abstract class is one of the potential implementation strategies to implement abstract data types in the given language. But there are a lot more techniques to do that.

So abstract base classes allow you to define a set of derived classes and give you the guarantee that all interfaces ( declarations ) have also an implementation, if not, the compiler throws an error, because you can't get an instance of your class because of the missing method definition.

But you also can use compile time polymorphism and related techniques like CRTP to have abstract data types.

So you have to decide which features you need and what price you want to pay for it. Runtime polymorphism comes with the extra cost of vtable and vtable dispatching but with the benefit of late binding. Compile time polymorphism comes with the benefit of much better optimizable code with faster execution and less code size. Both give you errors if an interface is not implemented, at minimum at the linker stage.

But abstract data types with polymorphism, independend of runtime or compile time, is not a 1:1 relation. Making things abstract can also be given by simply defining an interface which must be somewhere fulfilled.

In a short: Abstract data types is not a directly represented in c++ while abstract base class is a c++ technique.

Upvotes: 1

Related Questions