answersLogoWhite

0


Best Answer

There is no abstract keyword in C++, therefore what you ask cannot be done.

An abstract base class (or abstract data type if you prefer) is achieved by declaring at least one pure-virtual function in a class. Abstract base classes cannot be instantiated other than through derivation, where the pure-virtual method must be overridden by the derivative lest it become abstract itself.

The following example demonstrates an abstract data type:

struct abstract_object {

// pure-virtual function (no implementation provided)

virtual void virtual_method()=0;

};

struct real_object: public abstract_object {

// Implementation of pure-virtual function

void virtual_method(){ std::cout<<"Hello world!"<<std::endl; }

};

In the above example, abstract_object provides no implementation of the pure-virtual method. You can provide a generic implementation if you wish but it will not be inherited by any derivative (you can only call the generic method explicitly from the more specific implementation in the derived class). The fact it is declared pure-virtual guarantees that all non-abstract derivatives will provide their own specific implementations. Once a derivative implements the method, that implementation can subsequently be inherited by other derivates or overridden as required, just as if it were a non-pure-virtual method.

An abstract base class is a conceptual class. For example, circles and squares are actual objects that might be derived from a conceptual shape class. You wouldn't normally want users to be able to instantiate a shape by itself, so it must contain at least one pure-virtual method, such as a draw() method. This not only prevents users from instantiating shapes, it ensures that all derivatives, such as circle and square, provide their own specific draw methods. That is, the shape doesn't have enouygh information to draw itself, but a circle or square do. Thus the role of the conceptual class is simply in order to provide a generic interface that must be overridden by the more specific types.

To use an abstract base class in a stack implementation, you might choose to use a generic data container that the stack can easily push and pop, regardless of its actual type. The actual type of data on the stack is of no importance to the stack itself, that aspect is only of importance to the stack's consumers. By using a conceptual object with a generic interface, consumers can be assured that all derivatives of that conceptual object can safely be pushed and popped from the stack, while the generic interface methods of the conceptual object allow consumers to access the more specific behaviours of those objects, without the need to know the actual type (which requires expensive runtime type information). In other words, you can push squares and circles onto a stack of shapes, but when you pop them from the stack, you don't need runtime type information to determine their actual type, because all derivatives of shape are still shapes, they simply behave according to their actual type, polymorphically. That is, when you pop a shape off the stack and call its draw method, you rightly expect a circle to draw a circle and a square to draw a square. You get that for free because the draw method is a virtual method. And the only reason for having an abstract base class (shape) in the first place is that you don't want consumers instantiating shape objects directly, because a shape is a conceptual, generic object, not an actual object.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Stack program using key word abstract in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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 is pop in C plus plus?

"Pop" allows you to remove items off of the stack. The stack is an area in memory that contains the information of a program, such as function names and instructions, the values of variables, etc. The opposite of pop is "push". This allows you to add items to the stack.


'write a simple program for stack operation in c plus plus?

void push(int y) { if(top&gt;stackSize) { cout&lt;&lt;"stack full"&lt;&lt;endl; return; } else { top++; stack[top]=y; } } int pop() { int a; if(top&lt;=0) { cout&lt;&lt;"stack is empty"&lt;&lt;endl; return 0; } else { a=stack[top]; top--; } return(a); }


Does PlayStation plus stack?

Yes, it is possible to purchase, or 'stack', additional time to your Plus membership


C plus plus program using a stacks converting a postfix-infix?

Yes


Program to get a system time using c plus plus?

time in hours second minute


How you use stack in c plus plus classes?

It would be easier to manipulate the stack in assembly language rather than C++.


How do you write a program in C plus plus plus plus How do you write a program in C to swap two variables without using the third oneo swap two variables without using the third one?

To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;


Program to generate Fibonacci series using storage class in c plus plus?

i dn't know. haha


How do you write a C plus plus program that displays a pyramid of Xes on the screen using a for loop?

printf ("x")


Any program in c plus plus without using any header file?

For example: int main (void) { return 0; }


How do you display stars in c plus plus using the if else structure and a for loop and you use void main?

It depends on what program you design really