can any one solve this question its C++ plz

Jun 21, 2012 at 10:52am
Implement a Stack class to store the integers (maximum 50) with its usual push and pop functionality. Provide the following constructors: default and parameterized (to insert first element only). In addition to push and pop functions, define a friend function of class Stack named “find_index” with two parameters: a Stack object and an integer value that the user wants to search in the stack. This function searches and returns the stack array index of the 2nd parameter, if match is found.

Jun 21, 2012 at 10:54am
Homeworks? No, thanks.
Jun 21, 2012 at 10:58am
In answer to your title; yes.

Come on, though, have a go. It won't hurt.
Jun 21, 2012 at 11:12am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <cstddef>

class Stack {
private:
    std::size_t size;
    int ia[50];

public:
    Stack(): size(0)
    {}

    Stack(int n): size(1)
    {
        ia[0] = n;
    }

    int push()
    {
        // your turn!
    }

    int pop()
    {
        // your turn, again!
    }

    friend std::size_t find_index(Stack, int);
};

std::size_t find_index(Stack s, int i)
{
    std::size_t r=0;

    while (s.size != 0)
        if (s.pop() == i)
            return r;
        else
            ++r;

    throw "Couldn't find anything!";
}
Topic archived. No new replies allowed.