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

Array of Objects

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

Array of Objects

 

We can have arrays of variables that are of the type class. such variables are called arrays of objects. Consider the following class definition:

                                    class employee

 

                                    {

 

                                                char name[30];

 

                                                float age;

 

                                        public:

 

                                                void getdata(void);

 

                                                void putdata(void);

 

                                    };

 

The identifier employee is a user-defined type and can be used to create objects that relate to different categories of the employees, example:

                                    employee manager[3];           // array of manager

 

                                    employee foreman[15];         // array of foreman

 

                                    employee worker[75];           //array of worker

 

Since an array of objects behaves like any other array, we can use the usual array-accessing functions. For example:

                                    Manager[ i ].putdata( );

 

will display the data of the ith element of the array manager.

/////////////////////////[ ARRAYS OF OBJECTS]//////////////////////////

 

//-------------------------------------- include section -----------------------------------------------------

 

#include <iostream.h>

 

 

//--------------------------------------- class declaration ---------------------------------------------------

 

class employee

 

{

 

         char name[30];         // string as class member

 

         float age;

 

    public:

 

         void getdata(void);

 

         void putdata(void);

 

};

 

//--------------------------------------- member function definition -------------------------------------

 

void employee : : putdata(void)

 

{

 

         cout << "Enter name: ";

 

         cin >> name;

 

         cout << "Enter age: ";

 

         cin >> age;

 

}

 

void employee : : putdata(void)

 

{

 

        cout <<" Name : " << name << "\n";

 

        cout <<" Age :"<<age <<"\n";

 

}

 

const int size = 3;

 

//---------------------------------------------- main section ---------------------------------------------------

 

int main(void)

 

{

 

            employee manager[size];

 

            for(int i =0; i<size; i++)

 

         {

 

                   cout <<"\nDetials of manager "<<i+1<<"\n";

 

                   manager[i].getdata( );

 

          }

 

          cout <<"\n";

 

           for(i=0; i<size; i++)

 

           {

 

                     cout <<"\n Manager " << i+1<<"\n";

 

                     manager[i].putdata( );

 

            }

 

}

 

////////////////////////////<program 5.6>///////////////////////////////

 

 

Objects as Function Arguments

 

Like any other data type, an object may be used as a function argument. This can be done in two ways:

  • A copy of the entire objects is passed to function.
  • Only the address of the object is transferred to the function.

The first method is called as pass-by-value. Since a copy of the object is passed to the function, any changes make to the object inside the function do not affect the object used to call the function.

The second method is called pass-by-reference. When an address of the objects is passed, the called function works directly on the actual object used in the call. This means that any changes made to the object inside the function will reflect in the actual object. The pass-by-reference method is more efficient since it requires passing only the address of the object and not the entire object.

Program 5.7 illustrates the use of objects as function arguments.

///////////////////////[ OBJECTS AS ARGUMENTS]///////////////////////

 

//---------------------------------------- INCLUDE SECTION---------------------------------------------

 

#include <iostream.h>

 

 

//----------------------------------------- class declaration ---------------------------------------------------

 

class time

 

{

 

          int hours;

 

          int minutes;

 

     public:

 

          void getdata(int h, int m)

 

          {

 

                     hours = h;

 

                     minutes = m;

 

           }

 

        void puttime (void)

 

          {

 

                     cout << hours << "hours and ";

 

                     cout <<minutes<<"minutes "<<"\n";

 

           }

 

           void sum(time, time );    // objects are arguments

 

};

 

//-------------------------------------- member function definition ---------------------------------------

 

 

void time : : sum(time t1, time t2)         // t1, t2 are objects

 

{

 

              minutes = t1.minutes+t2.minutes;

 

              hours = minutes/60;

 

              minutes = minutes%60;

 

              hours = hours + t1.hours + t2.hours;

 

}

 

//----------------------------------------------------- main section --------------------------------------------

 

int main(void)

 

{

 

            time T1, T2, T3;

 

            T1.gettime(2, 45);

 

            T2.gettime(3, 30);

 

 

             T3.sum(T1, T2);

 

 

              cout << "T1 = ";               T1.puttime( );

 

              cout << "T2 = ";               T2.puttime( );

 

              cout << "T3 = ";               T3.puttime( );

 

}

 

/////////////////////////////<program 5.7>/////////////////////////////

 

The output of program would be:

                        T1= 2 hours and 45 minutes

                        T2= 3 hours and 30 minutes

                        T3= 6 hours and 15 minutes

 

An object can also be passed as an argument to a non-member function. however, such functions can have access to the public member functions only through the objects passed as arguments to it. These functions cannot have access to the private data members.

 

 


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