3. STACK
Push: Increment top Index and
add the element at top
Pop : Delete the element at top
and decrement top Index
Empty: If top Index is -1; Stack is
empty
int & top( )
QUEUE
Push: Increment rear Index and
add the element at rear
Pop : Delete the element at front
and increment front Index
Empty : If front Index is greater
than rear Index; Queue is empty
int &front( )
Sikander 3
4. int main( )
{
int dschoice ,ch , ele;
cout << "1. Stack 2. Queue n";
cin >> dschoice ;
if(dschoice == 1)
{
Stack s1;
for( ; ; ) {
cout <<"StackOperations n";
cout <<"1. Push n 2. Pop 3. Display n";
cin >> ch;
switch(ch) {
case 1 : cout <<"Enter the element to push : ";
cin >> ele;
s1.push(ele);
break;
case 2 : if(s1.empty() == true)
cout <<"Stack is empty n";
else
s1.pop( );
break;
case 3 : s1.display( );
}
}
}
Sikander 4
else
{
Queue q1;
for( ; ; )
{
cout <<"QueueOperations n";
cout <<"1. Push n 2. Pop 3. Display n";
cin >> ch;
switch(ch)
{
case 1 : cout <<"Enter the element to push : ";
cin >> ele;
q1.push(ele);
break;
case 2 : if(q1.empty() == true)
cout <<"Queue is empty n";
else
q1.pop( );
break;
case 3 : q1.display( );
} //End of Switch
} //End of For
} //End of else
} //End of Main
6. class LDS
{
};
Sikander 6
class Stack : public LDS
{
public : void push(int ele);
};
classQueue : public LDS
{
public : void push(int ele);
};
int main( )
{
LDS *ptr = new Stack; //Valid
ptr->push( 5 ); // Invalid; as we don’t have push in LDS
}
7. class LDS
{
public : virtual void push(int ele);
};
Sikander 7
class Stack : public LDS
{
public : void push(int ele);
};
classQueue : public LDS
{
public : void push(int ele);
};
int main( )
{
LDS *ptr = new Stack; //Valid
ptr->push( 5 ); // No Compilation Error
// Linker Error
}
8. class LDS
{
public : virtual void push(int ele) = 0;
};
Sikander 8
class Stack : public LDS
{
public : void push(int ele);
};
classQueue : public LDS
{
public : void push(int ele);
};
int main( )
{
LDS *ptr = new Stack; //Valid
ptr->push( 5 ); // No Compilation Error, No Linker Error
//Invokes push of Stack.
ptr = new Queue;
ptr->push(6); //Invokes push of Queue
}
10. void dsoperations(LDS &ref);
int main( )
{
int dschoice ,ch , ele;
LDS *ptr;
Stack s1;
Queue q1;
cout << "1. Stack 2.Queue n";
cin >> dschoice ;
if(dschoice == 1)
dsoperations(s1);
else
dsoperations(q1);
}
Sikander 10
void dsoperations(LDS &ref)
{
for( ; ; ) {
cout <<"1. Push n 2. Pop 3. Display n";
cin >> ch;
switch(ch) {
case 1 : cout <<"Enter the element to push : ";
cin >> ele;
ref.push(ele);
break;
case 2 : if(ref.empty() == true)
cout <<“Empty n";
else
ref.pop( );
break;
case 3 : ref.display( );
}
}
}
11. class Shape
{ public : virtual int area( ) = 0;
};
class Rectangle : public Shape
{ int m_l , m_b;
public :
Rectangle(int l , int b ) : m_l(l) , m_b(b)
{ }
int area( ){
return m_l * m_b;
}
};
class Circle : public Shape
{ int m_radius;
Public : Circle(int r) : m_radius( r )
{}
int area( )
{
return 3.14 * m_radius * m_radius;
}
}
Sikander 11
int main( )
{
Shape *ps = new Rectangle(4 , 5);
cout << ps->area( );
ps = new Circle(6);
cout << ps->area( );
}
12. class Shape
{
public : virtual int area( ) = 0;
};
class Rectangle : public Shape
{
int m_l , m_b;
public : Rectangle(int l , int b )
{
m_l = l , m_b = b;
}
int area( ){
return m_l * m_b;
}
};
Sikander 12
int main( )
{
Rectangle r(4 , 5);
Shape &s = r;
s.area( );
}