?
Solved

Can you explain this very simple code to me.

Posted on 2011-02-20
2
Medium Priority
?
244 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
[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
  • Learn & ask questions
2 Comments
 
LVL 13

Accepted Solution

by:
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

by:DeepuAbrahamK
ID: 34958191
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

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.
Starting your own business is always a daunting process, and for most people it is brand new experience. Avoid the common pitfalls by following these tips to start on the road to success.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Suggested Courses

752 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