Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Can you explain this very simple code to me.

Posted on 2011-02-20
Medium Priority
256 Views
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
Question by:Osloiite
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 13

Accepted Solution

F Igor earned 200 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

ID: 34958191
0

## Featured Post

Question has a verified solution.

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

Read on to get a few ideas on how to promote your next corporate event.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
An overview of how to create reports in Adobe Analytics (formerly Omniture Site Catalyst) using pageNames, events, eVars and props. This video will show you how to install the Omniture Debugger tool so can see (and test) what is being passed int…
###### Suggested Courses
Course of the Month11 days, 12 hours left to enroll