You must Sign In to post a response.
  • Category: Programming

    Explain object as a function argument.


    Studying for Diploma final exams? Have queries in learning computer programming concepts like object as a function argument? On this page you can read the answers posted by ISC experts to all your queries.

    Following is a question that was asked in the question paper of Diploma in Computer Technology(Summer 2019)conducted by Maharashtra State Board of Technical Examination, Mumbai for subject Object Oriented Programming(17432)(G Scheme), Semester-IV

    This question is asked for 8 marks and hence along with program some explanation is also expected.

    Q) Explain object as a function argument using following points with suitable example. [8 Marks]
    (i) Pass by value
    (ii) Pass by reference
  • Answers

    2 Answers found.
  • A function is a group of statements which are used to perform a particular task. In the functions, we can pass parameters as per the need to perform the task. A parameter can be any type of a variable or even we can use an object of a class. When an object of a class is passed to a member function of the same class, it's data members can be accessed inside the function using the object name and dot operator. It can be done either by value or by reference.

    Pass by value method :
    When an object is passed by value means a copy of the actual object is created inside the function. This exact copy will be destroyed when the function scope is completed. Which means any changes that are made to the copy of the object inside the function will not reflect in the actual object.
    Example for pass by value method :

    #include

    class weights
    {
    int kilograms;
    int grams;
    public:
    void getdata ();
    void putdata ();
    void sum_weights (weights,weights) ;
    } ;

    void weights :: getdata()
    {
    cout<<"/nKilograms:";
    cin>>kilograms;
    cout<<"Grams:";
    cin>>grams;
    }

    void weights :: putdata ()
    {
    cout<}

    void weights :: sum_weights(weights wl,weights w2)
    {
    grams = wl.grams + w2.grams;
    kilograms=grams/1000;
    grams=grams%1000;
    kilograms+=wl.kilograms+w2.kilograms;
    }

    int main ()
    {
    weights wl,w2 ,w3;
    cout<<"Enter weight in kilograms and grams\n";
    cout<<"\n Enter weight #1" ;
    wl.getdata();
    cout<<" \n Enter weight #2" ;
    w2.getdata();
    w3.sum_weights(wl,w2);
    cout<<"/n Weight #1 = ";
    wl.putdata();
    cout<<"Weight #2 = ";
    w2.putdata();
    cout<<"Total Weight = ";
    w3.putdata();
    return 0;
    }

    Output of above code:
    Enter weight in kilograms and grams
    Enter weight #1
    Kilograms: 12
    Grams: 560
    Enter weight #2
    Kilograms: 24
    Grams: 850
    Weight #1 = 12 Kgs. and 560 gms.
    Weight #2 = 24 Kgs. and 850 gms.
    Total Weight = 37 Kgs. and 410 gms.

    Pass by reference method:
    When an object is passed by reference means the address or reference of the actual object is passed to the function. Thus whatever changes are made inside the function will affect the actual object.
    Example for pass by reference:

    #include
    void swapnumbs(int &i, int &j) {
    int temp = i;
    i = j;
    j = temp;
    }

    int main(void) {
    int a = 10;
    int b = 20;

    swapnumbs(a, b);
    printf("A is %d and B is %d\n", a, b);
    return 0;
    }

    Output of the above code:
    A is 20 and B is 10

    --Time is money--
    Benjamin Franklin

  • function is a self contained block of program which performs a predefined task. There are two major categories of functions and they are
    1) call by value function
    2) call by reference function
    In case of call by value type function values of parameters are supplied to function as arguments.
    In case of call by reference type function instead of values of parameters addresses of parameters are supplied to function as arguments and hence in function definition pointers of appropriate data types are expected.
    Below is the program which shows use of call by value and call by reference in a single program. Appropriate comments are placed in the program.
    In call by reference when addresses of objects are passed as arguments using & sign before object then in definition of function these arguments are accepted as pointer of object type. And whenever pointer to object is used to access any data member we have to use arrow operator between pointer name and data member name.
    Please include iostream.h and conio.h header files before declaration of class.
    //PROGRAM TO IMPLEMENT OBJECT AS FUNCTION ARGUMENTS- CALL BY VALUE AND CALL BY REFERENCE.
    #include
    #include
    class time
    {
    private:
    int h,m,s;
    public:
    void get(void)
    {
    cout<<"\nEnter values for hours, minutes and seconds\n";
    cin>>h>>m>>s;
    }
    void display(void)
    {
    cout<<"\nTime is "< }

    friend time calculate(time x,time y) //call by value
    {
    time t;
    t.s=x.s + y.s;
    t.m=t.s/60;
    t.s=t.s%60;
    t.m=t.m+x.m+y.m;
    t.h=t.m/60;
    t.m=t.m%60;
    t.h=t.h+x.h+y.h;
    return(t);
    }
    friend time calculate(time *p,time * r) //call by referencel
    {
    time t;
    t.s=p->s + r->s;
    t.m=t.s/60;
    t.s=t.s%60;
    t.m=t.m + p->m + r->m;
    t.h=t.m/60;
    t.m=t.m%60;
    t.h=t.h+ p->h + r->h;
    return(t);
    }
    };
    void main(void)
    {
    class time t1,t2,t3;
    clrscr();
    cout<<"\nEnter time details for first object\n";
    t1.get();
    cout<<"\nEnter time details for second object\n";
    t2.get();
    clrscr();
    t3=calculate(t1,t2);
    cout<<"\nTime details for first object\n";
    t1.display();
    cout<<"*********************************\n";
    cout<<"\nTime details for second object\n";
    t2.display();
    cout<<"*********************************\n";
    cout<<"\nTime details for resultant object after addition of times of two objects\n";
    t3.display();
    cout<<"*********************************\n";
    class time T1,T2,T3;
    cout<<"\nEnter time details for first object\n";
    T1.get();
    cout<<"\nEnter time details for second object\n";
    T2.get();
    clrscr();
    T3=calculate(&T1,&T2);
    cout<<"\nTime details for first object\n";
    T1.display();
    cout<<"*********************************\n";
    cout<<"\nTime details for second object\n";
    T2.display();
    cout<<"*********************************\n";
    cout<<"\nTime details for resultant object after addition of times of two objects\n";
    T3.display();

    getch();
    }


  • Sign In to post your comments