answersLogoWhite

0


Best Answer

An abstract data type (ADT) exists purely to provide a common interface to all the objects that may be derived from it, without providing the full implementation of that interface. It is a concept rather than an actual object.

For instance, an Animal object is a fairly general class of object, while Cat and Dog are specific types of Animal. It does not make sense to allow users to create an instance of Animal, since it would impossible to provide an implementation for every type of animal that that class could possibly represent.

For instance, all animals make a noise, so it makes sense for the Animal class to have a MakeNoise() method. But we cannot implement this method since the Animal class would have to determine what type of animal it actually was before it could produce the correct sound. Animals simply do not do this in real life: a dog does not ask itself whether it is a cat or a dog before deciding that it should bark rather than meow!

So we declare Animal to be an ADT by making the MakeNoise() method pure-virtual. We can then derive a Cat object from our Animal ADT and implement the MakeNoise() method within the Cat object. We can do the same for the Dog class, a Mouse class, an Elephant class, and so on. Each specific object knows what sound it must make.

Since all these specific classes are derived from a common ADT, the Animal class, we can create collections of animals, without regard to their specific type. When we call an animal's MakeNoise() method, a Cat object will meow while a Dog object will bark, the Mouse will squeak and the Elephant will Trumpet. We've effectively called a general method but got a specific method instead, without having to work out which specific method to call.

That is the essence of all ADTs; by creating a new data type from an ADT, we can provide a specific implementation for an already existing common interface. Clients of the ADT (such as a queue or stack) needn't know what data types it contains, nor what data types it could contain in the future because they already know the common interface of the ADT. If you decide you want a lion in your queue, derive one from the ADT and the client won't care: the MakeNoise() method will make the lion roar!

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

7y ago

An abstract data type (ADT) is a base class that has one or more pure-virtual methods. A derived class must provide an overridden implementation for all the pure-virtual methods inherited from its abstract base classes otherwise it, too, becomes an ADT. Once overridden by any class in the hierarchy, a pure-virtual method reverts to a virtual method and that implementation can then be inherited by further derivatives of the class. However, only classes that provide or inherit a complete implementation can be instantiated in their own right. ADTs can only be instantiated through derivation.

ADTs are useful when creating classes that are intended to provide interfaces to more specialised classes, but which do not have enough information to implement those interfaces. For instance, consider a shape base class:

class shape {

public:

virtual void draw (void);

};

shape s; // a shapeless shape?

s.draw (); // how do we draw a shapeless shape?

The draw method is common to all types of shape, however a shape is not a type of shape. At best it is a shapeless shape, but it is not reasonable to expect a shapeless shape to be drawn (or rotated, mirrored, skewed or any other operation common to shapes), thus a shape cannot implement these methods. But it must still provide the interface because it is common to all types of shape (circles, triangles, rectangles, pentagons, hexagons, etc). We achieve this with pure-virtual methods:

class shape { public:

virtual void draw (void) =0;

};

The pseudo-initialiser, =0, renders a virtual function pure and turns the base class into an ADT. This means we cannot instantiate objects of type shape in their own right:

shape s; // error -- shape is an ADT!

To implement the pure-virtual interface we must derive concrete objects from the ADT:

class circle : public shape {

int x, y; // centre point

int r; // radius

public:

circle (int xx, int yy, int rr): x {xx}, y{yy}, r{rr} {}

void draw (void) override;

};

Here, circle provides a complete implementation for the pure virtual interface of its base class, thus we can instantiate circles (and thus shapes):

circle c {0, 0, 42} ;

c.draw (); // ok

Similarly with rectangles:

class rectangle : public shape {

int x, y; // centre point

int w, h; // width and height

int r; // rotation

public:

rectangle (int xx, int yy, int ww, int hh, int rr): x {xx}, y{yy}, w{ww}, h{hh}, r{rr} {}

void draw (void) override;

};

rectangle r {0, 0, 10, 15, 0};

r.draw (); // ok

At this point we can start to add other interfaces to the ADT and implement them in each of the derivatives. However, we then notice that some methods don't actually do anything for certain objects. For instance the rotate method applies to all shapes, but no matter how much we rotate a circle, it looks exactly the same. Thus rotate does not apply to circles. We can eliminate this specific interface from circles simply by creating a new ADT purely for the rotatable shapes:

class shape {

public:

virtual void draw (draw) =0;

};

class rotatable: public shape {

public:

virtual void rotate (int rad) =0;

};

class circle: public shape {

int x, y; // centre point

int r; // radius

public:

circle (int xx, int yy, int rr): x {xx}, y{yy}, r{rr} {}

void draw (void) override;

};

class rectangle: public rotatable {

int x, y; // centre point

int w, h; // width and height

int r; // rotation

public:

rectangle (int xx, int yy, int ww, int hh, int rr): x {xx}, y{yy}, w{ww}, h{hh}, r{rr} {}

void draw (void) override;

void rotate (int rad) override;

};

shape s; // error - shape is an ADT!

rotatable rot; // error -- rotatable is an ADT!

circle c {0, 0, 42} ;

c.draw (); // ok

c.rotate (12); // error: circle does not have a rotate() method!

rectangle r {0, 0, 10, 15, 0};

r.draw (); // ok

r.rotate (12); // ok

Note that although we cannot instantiate shape or rotatable objects in their own right, we can still refer to them. After all, every circle is a type of shape and has a public base class of type shape that we can refer to. Thus the following are all perfectly valid "shape containers":

std::vector<shape*> s; // a vector of shapes (of all kinds)

std::Vector<circle*> c; // a vector of circles

std::vector<rectangle*> rc; // a vector of rectangles

std::vector<rotatable*> r; // a vector of all rotatable shapes (including rectangles)

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is abstract data type for C Plus Plus examples?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is different between primitive date type and non primitive data type in c plus plus programming language?

A primitive data type is built into the language - int, char, long, etc. A non-primitive data type is am abstract data type that is built out of primitive data types - linked list, queue, stack, etc.


Is built-in data-type are abstract data-types in java?

All built-in data types are not abstract data types.


What is called representation of data structure in memory?

"Abstract Data Type"


What is the type of class for which objects can not be created except the static member?

Abstract data types or abstract base classes.


Representation of stack data structure in c plus plus?

Stack is an abstract data type that allows you to input and output data in a way that the first data which was placed in the stack will be the last one to get out. We use physical examples of stack in our daily lives such as the stack of dishes or stack of coins where you only add or remove objects from the top of the stack. You can see the implementation in c++ in related links, below.


What are abstract nouns starts with letter K?

Examples of abstract nouns that start with K are:karmakeennesskind (type or sort)kindnesskinshipknowledge


Do Abstract data type increases the complexity of program or reduces the complexity?

Decreases.


What are the examples of data type?

quantitative and qualitative


What is true about true about abstract data type Object of an abstract class type cant be created. We can derive classes from these abstract classes Object of an abstract class t?

True - an instance of an abstract class cannot be created. False - derive (subclass) from a class, not the object (the instance).


What are abstract data types?

Abstract Data Types An Abstract Data Type (ADT) is a data type that has been created by a programmer &ndash; i.e., it is not built-in in the programming language. As any other data types, an ADT is composed of a domain (the set of values belonging to the data type) and a collection of operations to manipulate such values. The only difference is that such data type will be constructed by the programmer. When we build an ADT we really want to apply the principles of encapsulation and information hiding mentioned earlier. This means that, once we have finished building the data type, we wish others to use the data type exclusively through the operations we provide, and in no other way. In particular, to protect our implementation and guarantee the ability to evolve software, we want to ensure that the implementation of the ADT is hidden from other users.


What is an abstract data structure?

Abstract Data Type in computing is a set of data along with a set of predefined operations.The actual data inside the ADT is protected from direct manipulation. The exposed operations is the only way to manipulate the data.In easier terms, it is very much like (though not limited) to the objects in object oriented programming.


What is the difference between data structure and abstract data type?

An Abstract Data Type is an interface that interacts with a data structure. A Data Structure is an implementation of the ADT. for example. If you were going to create a linked list you would create an Interface listing all the methods required by the list. Then in the linked list class you would code how the list uses these methods. Hope this helps :)