-2
\$\begingroup\$

I worked on my code a bit from the earlier post and was able to make it work. I am new to C++ so please be kind.

I basically have two questions apart from the peer review.

  1. When I use templates definitions in a header and just call it from source, it doesn't work. Why?

  2. To make it work using strings I want to use a typeof() alternative of C++. Can anyone suggest one?

The code might be a mess but I would still like some criticism.

#include<iostream>
#include<string>


using namespace std;

template<class T,int size=2>
class stack{

private:
    T stacked[size];
    int count;

public:

    void Insert(T data);
    void Display();
    stack();

};

template<class T,int size>
stack<T,size>::stack()
{

    for(int i=0;i<size;i++)
            stacked[i]=0;

    count = 0;

}

template<class T,int size>
void stack<T,size>::Insert(T data)
{        if(count>=size)
    {
            cout<<"Stack is full";
            return;
    }
    count++;
    stacked[count] = data;
    return;
}
template<class T,int size>
void stack<T,size>::Display()
{       cout<<"Printing out values:"<<endl;
    for(int i=0;i<size;i++)
            cout<< stacked[i]<<endl;
}


int main()
{
stack<int,5> S1;

S1.Insert(10);
S1.Insert(22);
S1.Insert(5522);
S1.Display();

stack<double,6> S2;

S2.Insert(333);
S2.Insert(6666);
S2.Insert(667777);
S2.Display();


stack<char,6> s3;

    s3.Insert('s');
    s3.Insert('a');
    s3.Insert('v');
    s3.Insert('v');
    s3.Insert('y');
    s3.Display();

 /*   stack<string,6> s4;

    s4.Insert("sav");
    s4.Insert("vvy");
    s4.Insert("Is a");
    s4.Insert("great");

    s4.Display();
*/
return 0;
 }
\$\endgroup\$
4
  • \$\begingroup\$ I am sorry but why are you editing this in such a wrong way? Please let my grammar be correct. \$\endgroup\$ Commented Sep 24, 2015 at 21:56
  • \$\begingroup\$ Thank you for the given comment. Was there a code review anywhere? Last post you said it should work. Also, do you know any alternate to typeof? \$\endgroup\$ Commented Sep 24, 2015 at 22:04
  • \$\begingroup\$ Also, it works when I don't initialise with 0 in the constructor even for strings. \$\endgroup\$ Commented Sep 24, 2015 at 22:06
  • 1
    \$\begingroup\$ Code compiles and runs for me. \$\endgroup\$ Commented Sep 24, 2015 at 22:26

1 Answer 1

5
\$\begingroup\$

Design

Its not really a stack is it!
A term stack implies certain things to computer scientists. It is basically LIFO queue. Which implies that you need to be able to add and remove elements.

Code Review.

using namespace

Don't do this:

using namespace std;

Read this: Why is “using namespace std;” considered bad practice? it will explain why in detail. But basically you are going to cause more problems for yourself than it is worth. Also you just need to prefix stuff with the 5 characters std:: its not that hard.

So don't start a bad habbit. Stop using the above line.

Constructor:

The elements type T better be constructable from int.

            stacked[i]=0;

This works if your type T is an integer fine. But if T is class type. Then this is saying construct an object with the constructor that accepts an integer (or a single value whose type can be converted to from int (double/float)). Then call the assignment operator to put the value in the array.

In the constructor you should prefer to use the the initializer list than code.

template<class T,int size>
void stack<T,size>::stack()
    : stacked()             // this forces default initialization.
                            // for types the default constructor.
                            // for POD types zero will be used.
    , count(0)
{}

Arrays are zero based.

Here you are inserting elements from [1-size] where what you want is [0-size)

    count++;
    stacked[count] = data;

So you have an off by one error (where you are writing past the end of the array).

Prefer '\n' over std::endl

       cout<<"Printing out values:"<<endl;
            cout<< stacked[i]<<endl;

The std::endl puts the character '\n' onto the stream then flushes the stream. You very rarely need to flush the stream. The buffers know when they need to be flushed and will almost always do an optimal job.

Zero sized arrays.

T stacked[size];

If size is zero this is illegal. C++ does not allow zero sized arrays.

\$\endgroup\$
3
  • \$\begingroup\$ Thank You @Loki this was actually helpful. I agree this is not entirely a stack(c'mon just started building it). I was thinking to add an if condition for string to check if "typeof" is string. Also, I initialised size to 2 so it won't be zero? Am I wrong? \$\endgroup\$ Commented Sep 24, 2015 at 23:47
  • \$\begingroup\$ But somebody could do: stack<int, 0> s; You can use static_assert to make it obvious that zero is an invalid value. \$\endgroup\$ Commented Sep 25, 2015 at 4:30
  • \$\begingroup\$ Adding a check for a specific type is a bad idea. You could have one constructor that just uses the default constructor for T (like I demonstrated above). A second constructor (if you are feeling brave) could be passed a default value that is used to initialize all members of the array with. \$\endgroup\$ Commented Sep 25, 2015 at 4:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.