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

Returning Objects

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

Returning Objects

 

A function can not only receive objects as arguments but also can return them. The example in program below illustrates how an object can be created ( withn a function) and returned to another function.

 

 

////////////////////////////[ RETURNING OBJECTS] /////////////////////

 

// -------------------------------------------- INCLUDE SECTOIN -------------------------------------

 

 

#include <iostream.h>

 

 

 

//-------------------------------------------- CLASS DECLARATION ----------------------------------

 

 

class complex                              //x + jy form

 

{

 

                float x;                 //real part

 

                float y;                   // imagenary part

 

           public:

 

               void input (float real, float imag)

 

                {

 

                             x = real;

 

                             y = imag;

 

                       friend complex sum( complex, complex);

 

                       void show (complex);

 

};

 

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

 

 

complex sum ( complex c1, complex c2)

 

{

 

              complex c3;

 

               c3.x=c1.x +c2.x;

 

               c3.y=c1.y+c2.y;

 

               return(c3);                                 // returns object c3
}

 

void complex : : show( complex c)

 

{

 

           cout << c.x << "+ j" << c.y<<"\n";

 

}

 

//-------------------------------------------- MAIN SECTION ------------------------------------------

 

int main(void)

 

{

 

            complex A,B,C;

 

            A.input(3.1, 5.65);

 

            B.input(2.75, 1.2);

 

 

            C = sum(A, B);    // C= A + B

 

   

 

             cout << "A= " << A.show(A);

 

             cout << "B= " << B.show(B);

 

             cout << "C= " << C.show(C);

 

}

 

/////////////////////////////< program 5.11>///////////////////////////

 

const Member Function

 

If a member function does not alter data in the class, then we may declare it as a const member function as follows:

 

                                    void mul(int, int) const;

 

                                    double get_balane(void) const;

 

The qualifier const is appended to the function prototype( in both declaration and definition). The compiler will generate error message if such functions try to alter the data values.

 

 


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