In a library a librarian is stacking the books using their numeric ids. He may push or pop a book from top and want to know which book ids are currently in the stack. Make a class based menu driven program to take size of stack in 1st line and then either of options – pop, push or stop. If pop or stop option is given no numeric value follows but if push is given a numeric value to be pushed is given.
Sample Input:
5 push 3 push 1 push 9 push 11 pop push 4 push 7 pop stop
Output:
Welcome to stacks!
Give one of options: pop, push, stop
Give one of options: pop, push, stop
Give one of options: pop, push, stop
Give one of options: pop, push, stop
Give one of options: pop, push, stop
Element popped: 11
Give one of options: pop, push, stop
Give one of options: pop, push, stop
Give one of options: pop, push, stop
Element popped: 7
Give one of options: pop, push, stop
Stack elements are:
|4|
---
|9|
---
|1|
---
|3|
---
Consider the code given below.
[Assume all the header files and namespaces included.]
How many times the following are invoked:
I) Default constructor
II) Parameterized constructor
III) Copy constructor
IV) Destructor
class test
{ int a;
public:
test(){a=0;}
test(int x) {a=x;}
test(const test &T){a=T.a;}
~test(){ }
test add(test X)
{ test T;
T.a=a+X.a;
return 0;
}
int main()
{ test t1(5),t2(10),t3;
t3=t1.add(t2);
return 0;
}
Consider a 20 *5 two-dimensional array marks which has its base address = 1000 and the size of an element = 2. Now compute the address of the element, marks[18][ 4] assuming that the elements are stored in row major order.
Complete the given code to get the output shown below:-
Base1
Base2
#include<iostream>
using namespace std;
class Base1 {
public:
void display()
{ cout<<"Base1"<<endl; } };
class Base2 {
public:
void display()
{ cout<<"Base2"<<endl"; } };
class Derived: public Base1, public
Base2 {
public:
};
int main()
{
Derived d;
return 0;
}
Write the parameterized constructor for all the classes in the hierarchy.
class A
{ int a;}
Class B:public virtual A
{int b;}
Class C:public virtual A
{int c; }
Class D:public b,public C
{int d;}
Consider the class hierarchy shown below.
class A
{ int a;
protected:
int b;
public:
int c; };
class B: protected A
{ };
Class C: public B
{ };
Which data members of class A are accessible
- in class B
-in class C
-in main function
Justify your answer.
Include the function calls at appropriate places in the given program, to get the
output as shown below
In base
In derived
#include<iostream>
using namespace std;
class base
{ int b;
protected:
void display(){cout<<"In base\n";}
};
class derived:public base
{ int d;
public:
void display(){ cout<<"In derived\n";}
};
int main()
{ derived D;
return 0;
}