Solved

Can you explain this very simple code to me.

Posted on 2011-02-20
2
227 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Marketing can be an uncomfortable undertaking, especially if your material is technology based. Luckily, we’ve compiled some simple and (relatively) painless tips to put an end to your trepidation and start your path to success.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

821 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