Solved

Can you explain this very simple code to me.

Posted on 2011-02-20
2
209 Views
Last Modified: 2012-05-11
I have started learning C++ so can someone explain the following two versions of code in the simplest way.


n a m e s p a c e S t a c k {
v o i d p u s h (c h a r );
c h a r p o p ();
}

v o i d f ()
{
S t a c k :: p u s h (´c ´);
i f (S t a c k :: p o p () != ´c ´) e r r o r ("i m p o s s i b l e ");
}

n a m e s p a c e S t a c k {
c o n s t  i n t  m a x _ s i z e = 2 0 0 ;
c h a r  v [m a x _ s i z e ];
i n t  t o p = 0 ;
v o i d  p u s h (c h a r c )

c h a r p o p () }



Another version of the same code but whats the difference??

n a m e s p a c e S t a c k
 {
v o i d p u s h (c h a r );
c h a r p o p ();
}

#i n c l u d e "s t a c k .h "
v o i d f ()
{
S t a c k :: p u s h (´c ´);
i f (S t a c k :: p o p () != ´c ´) e r r o r ("i m p o s s i b l e ");
}

#i n c l u d e "s t a c k .h "
n a m e s p a c e S t a c k {
c o n s t  i n t  m a x _ s i z e = 2 0 0 ;
c h a r v [m a x _ s i z e ];
i n t  t o p = 0 ;
}
v o i d S t a c k :: p u s h (c h a r c )
c h a r S t a c k :: p o p ()
0
Comment
Question by:Osloiite
2 Comments
 
LVL 13

Accepted Solution

by:
F Igor earned 50 total points
ID: 34936677
The first version is a "one file" program for a "stack" example,
while the second version is the same program in a modular "multi file" program.
(to demonstrate the use of #include)

The second version more explicit:

//File named "stack.h" (defines the Stack namespace)

namespace Stack
 {
void push (char );
char pop();
}


//Another file using the stack.h file (uses the Stack namespace to call their functions)

#include "stack.h"

void f()
{
Stack::push(´c´);
if (Stack::pop() != ´c´) error ("impossible");
}


//Another file using the stack.h file
(adds some properties to namespace Stack)

#include "stack.h"
namespace Stack {
const  int max_size = 200;
char v[max_size];
int  top = 0 ;
}
void Stack::push (char c )
char Stack::pop()





Note the use of Stack::push() or Stack::pop() when the call it's out of the namespace
(second example) and calling directly to push() and pop() when it's within the namespace.

namespace Stack {
const  int max_size = 200;
char v[max_size];
int  top = 0 ;
}
void Stack::push (char c )
char Stack::pop()


namespace Stack {
const  int max_size = 200;
char v[max_size];
int  top = 0 ;
void push (char c )
char pop()
}
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 34958191
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

With a software solution, you always get to control email signatures and have the final say on the design - not an unrelated department or the uninterested end user.
Read about how to choose the best possible content marketing agency to suit your needs. Content marketing has become an integral part of running a successful tech business, so it is wise to be informed.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now