Solved

Can you explain this very simple code to me.

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WordPress: Need a simple quiz plug-in 2 139
Compile GLUT with Visual Studio 2015 1 149
how Adwords bidding happens 1 59
Low-cost full color image t-shirt printing 5 79
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.
Marketers need statistics and metrics like everybody else needs oxygen. In this article we explain how to enable marketing campaign statistics for Microsoft Exchange mail.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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…

803 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