انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة

Arrays within a Class

الكلية كلية العلوم للبنات     القسم قسم الحاسبات     المرحلة 2
أستاذ المادة مهدي عبد سلمان المسلماوي       3/29/2011 10:12:21 AM

arrays within a class

 

the arrays can be used as member variables in class. the following class definition is valid.

 

 

                      const int size = 10 // provides value for array size

 

                      class vector

 

                      {

 

                                              int a[size] // "a" is int type array

 

                            public:

 

                                              void setval(void)

 

                                              void display(void)

 

                      }

 

the array variable a[ ] declared as a private member of the class vector can be used in the member functions like any other array variable. we can perform any operations on them. for instance, in the above class definition, the member function setval( ) sets the values of the array a[ ] and display( ) function displays the values. similarly, we may use other member functions to perform any other operations on the array values.

 

let us consider a shopping list of items for which we place an order with a dealer every month. the list includes details such as the code number and price of each item. we would like to perform operations such as adding an item to the list, deleting an item from the list and printing the total values of the order. program below shows how these operations are implemented using a class with arrays as data members.

 

/////////////////// [ processing shopping list ]//////////////////////

 

// - include section -

 

#include < iostream.h>

 

 

const m = 50

 

// - class declaration -

 

class items

 

{

 

                int itemcode[m]

 

                float itemprice[m]

 

                int count

 

  public:

 

                void cnt(void) { count = 0 }      // initializes count to 0

 

                void getitem(void)

 

                void displaysum(void)

 

                void remove(void)

 

                void displayitems(void)

 

}

 

//===================== member functions definitions ==============

 

void items : : getitem( void)  // assign values to members

 

{

 

                  cout < < "enter item code:"

 

                  cin > > itemcode [ count ]

 

                  cout  < < "enter item cost  :"

 

                  cin > > itemprice[ count ]

 

                  count++

 

}

 

void items : : displaysum( void )  // display total value

 

{

 

                float sum = 0

 

                for( int i = 0 i < count i++)

 

            {

 

                            sum += itemprice[i]

 

            }

 

            cout < < "\ntotal value  :" < < sum < < "\n"

 

}

 

void items : : remove( void )    // deleting a specified item

 

{

 

                item a

 

                cout < < "enter item code :"

 

                cin > > a

 

                for( int i = 0 i < count i++)

 

                {

 

                                if (itemcode[i] == a )

 

                                        {

 

                                                          itemprice[i]=0

 

                                        }

 

                }

 

}

 

void items : : displayitems(void)    // displaying items

 

{

 

                    cout  < < "\n code price \n"

 

                    for(int i = 0 i < count i++)

 

                    {

 

                                      cout < < "\n" < < itemcode[i]

 

                                      cout < < "        " < < itemprice [i]

 

                    }

 

                    cout < < "\n"

 

}

 

// ====================== main program ======================

 

int main(void)

 

{

 

                  items order

 

                  order.cnt( )

 

                  int x

 

                  do // do …. while loop

 

                    {

 

                                        cout < < "\n you can do the following:"

 

                                                          < < " enter appropriate number \n"

 

                                        cout < < "\n1 : add an item "

 

                                        cout < < "\n2 : display total value "

 

                                        cout < < "\n3 : deleting an item"

 

                                        cout < < "\n4 : display all items"

 

                                        cout < < "\n5 : quit"

 

                                        cout< < "\n\nwhat is your option?"

 

                                        cin > > x

 

                                        switch(x)

 

                                          {

 

                                                                        case 1 : order.getitem() break

 

                                                                      case 2 : order.displaysum() break

 

                                                                      case 3 : order.remove() break

 

                                                                      case 4 : order.displayitems() break

 

                                                                      case 5 : break

 

                                                                  default : cout < < "error in input try again\"

 

                                            }

 

                    } while ( x != 5)           // do ..  while ends

 

return(0)

 

}

 

/////////////////////////< program 5.3 > ////////////////////////////////

 

 

the program uses two arrays, namely itemcode [] to hold the code number of items and itemprice[] to hold the prices. a third data member cout is used to keep a record of items in the list. the program uses a total of four functions to implement the operations to be performed on the list. the statement

 

                                                                      const int m = 50

 

defines the size of the array members.

 

                      the first function cnt( ) simply sets the variable count to zero. the second function getitem( ) gets the item code and the item price interactivity and assigns them to array members itemcode[count] and itemprice [count]. note that inside this function count is incremented after the assignment operation is over. the function displaysum( ) first evaluates the total value of the order and then prints the value. the four function remove( ) deletings a given item from the list. it uses the item code to locate it in the list and sets the price to zero indicating that the item is not "active" in the list. lastly, the function displayitem( ) displays all the items in the list.

 

the program implements all the tasks using a menu-based user interface.

 

 

memory allocation for objects

 

 

the memory space for objects is allocated when they are declared and not when the class is specified. this statement is only partly true.

 

actually, the member functions are created and placed in the memory space only once when they are defined as a part of class specification. since all the objects belonging to that class use the same member functions, no separate space is allocated for member functions when the object are created. only space for member variables is allocated separately for each object. separate memory locations for the objects are essential because the member variables will hold different data values for different objects.

 

   

member functions 2

member function 1

common for all objects

memory created when functions defined

member variable 2

member variable 1

object 1

member variable 2

member variable 1

object 2

member variable 2

member variable 1

object 3

memory created when objects  defined

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .