|
From: Jason H. <jas...@bt...> - 2001-11-30 04:08:23
|
Hiya. You'll find enclosed a revamped version of your program that works ok.
I hope you don't mind? However before you look at the files I'll tell you
what made it not work and see if you can work it out for yourself before
peeking.
The main problem of the functions you wrote was that you were trying to
access private members of an object from outside of that object (non-member
functions). You could give the fuctions friend access to the class, but you
don't need to. One thing that helps is that you don't need to worry about
changing the object that is passed. It is a copy of the original and any
changes to it won't affect the original. Unless an argument is passed as a
referenced then a copy is made of the original for the function. The only
other way the function could change the original would for a pointer to be
passed, which is the traditional approach. Be aware that making a copy of
objects adds overhead to a function call, slowing everything down in the
case of very big objects. The size of 100 you use at the moment is OK for
now though.
Now you know that you aren't affecting the original then you can
destructivly operate on the passed copy and get the info you want. That is
you can count the values by popping them off the stack, for example.
If you wanted to do it non-destructivly then you could add the ability to
the class, meaning add other member functions that provide other
information, but don't expose the private members unecessarily. The
alternative is to make a manual copy and either work with the copy or use
original and restore it after, but be aware that this isn't much different
than the current approach!
I have made some other optimisations in the attached version. Maybe you'll
like them, maybe you won't!
Regards, Jason.
----- Original Message -----
From: "chromatonus 712" <chr...@ho...>
To: <dev...@li...>
Sent: Friday, November 30, 2001 2:28 AM
Subject: [Dev-C++] stack with class
> hi, may I know how to display elements and count the number of elements in
a
> stack defined below, without destruction? thank you very much!
>
>
>
>
>
> // Program: STACK 2
> // Objective: To understand how a stack works as implemented by a class
> // and an array for the data
> // Last modified 11-29-01
>
>
> #include <iostream.h>
> #include <conio.h>
>
> const max_stack = 100;
>
> class stacktype
> {
> public:
> stacktype();
> // default constructor
> // postcondition: initializes stack by setting TOP = 0
> void initialize();
> // postcondition: erase stack by setting TOP = 0
> void push( int );
> // postcondition: If the stack is not full,
> // increase TOP by 1 , then
> // put info into DATA[TOP]
> int pop();
> // postcondition: If the stack is not empty,
> // put DATA[TOP] into x,
> // decrease TOP by 1,
> // return x
> bool is_empty();
> // postcondition: Return true if stack is empty, false otherwise.
> bool is_full();
> // postcondition: Return true if stack is full, false otherwise.
> private:
> int mydata[max_stack];
> int mytop; // subscript of the top of the stack
> };
>
>
>
//==========================================================================
====
> //============================== Free functions
> ================================
>
//==========================================================================
====
>
> void display_top( stacktype );
> // precondition: send in stack
> // postcondition: display, without destruction, the value in data[top]
> void display_stack( stacktype );
> // precondition: send in stack
> // postcondition: display, without destruction, the values of all the
> // elements in the stack, in the order entered.
> int count_values( stacktype );
> // precondition: send in stack
> // postcondition: return the number of data pieces
> int menu();
> // postcondition: initial screen. Display options, allow user to enter
> // choice and return this value
>
>
//==========================================================================
====
> //============================== Implementation
> ================================
>
//==========================================================================
====
>
> stacktype::stacktype()
> {
> mytop = 0;
> }
> void stacktype::initialize()
> {
> mytop = 0;
> }
> bool stacktype::is_empty()
> {
> if( mytop == 0 )
> return true;
> else
> return false;
> }
> bool stacktype::is_full()
> {
> if( mytop == 99 )
> return true;
> else
> return false;
> }
> void stacktype::push( int x )
> {
> if( is_full() == false )
> {
> mytop++;
> mydata[mytop] = x;
> }
> }
> int stacktype::pop()
> {
> if( is_empty() == false )
> {
> int x = mydata[mytop];
> mytop--;
> return x;
> }
> }
>
>
>
//==========================================================================
====
> //============================== Client Program
> ================================
>
//==========================================================================
====
>
> int menu()
> {
> int choice;
> cout<< "STACK EDITOR MENU OPTIONS" << "\n";
> cout<< "1. Create new stack" << "\n";
> cout<< "2. Add elements to stack" << "\n";
> cout<< "3. Delete elements from stack" << "\n";
> cout<< "4. Display top element in stack " << "\n";
> cout<< "5. Display whole stack in the order entered" << "\n";
> cout<< "6. Count numbers of elements in stack" << "\n";
> cout<< "7. Exit" << "\n" << "\n";
> gotoxy(0,8);
> cout<< "choice? ";
> cin>> choice;
> return(choice);
> }
>
> int main()
> {
> stacktype S, T;
> int choice, x, i;
> choice = menu();
> while( choice != 7 )
> {
> switch( choice )
> {
> case 1: S.initialize();
> gotoxy(0,8);
> cout<< "choice? ";
> cin>> choice;
> break;
>
> case 2: gotoxy(0,10);
> while(S.is_full() == false)
> {
> cout<< "number to insert? ";
> cin>> x;
> S.push(x);
> }
> gotoxy(0,8);
> cout<< "choice? ";
> cin>> choice;
> break;
>
> case 3: gotoxy(0,10);
> S.pop();
> gotoxy(0,8);
> cout<< "choice? ";
> cin>> choice;
> break;
>
> case 4: gotoxy(0,12);
> display_top(S);
> gotoxy(0,8);
> cout<< "\n" << "choice? ";
> cin>> choice;
> break;
>
> case 5: display_stack(S);
> gotoxy(0,8);
> cout<< "choice? ";
> cin>> choice;
> break;
>
> case 6: count_values(S);
> cout<< i;
> gotoxy(0,8);
> cout<< "choice? ";
> cin>> choice;
> break;
>
> case 7: exit();
> break;
>
> choice = menu();
> }
> }
> }
>
> void display_top( stacktype S )
> {
> S.pop();
> cout<< S.mytop;
> }
>
> void display_stack( stacktype S )
> {
> stacktype T;
> while( S.mytop != 0 )
> {
> S.pop();
> T.push(S.mytop);
> }
> while( T.mytop != 0 )
> {
> cout<< T.pop();
> T.mytop--;
> }
> }
>
> int count_values( stacktype S )
> {
> int i = 0;
> if(S.mytop != 0)
> {
> while(S.mytop != 0)
> {
> S.pop();
> i++;
> }
> return i;
> }
> /////// ???????
> }
>
> int exit()
> {
> return x;
> }
>
> _________________________________________________________________
> Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|