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

    Program using C++ to show object as a function argument.


    Need help for writing a program in C language for Diploma exam? Looking out for a solution for exam preparation? Find answers from experts on this page.

    Following is the question asked in MSBTE(Maharashtra State Board of Technical Education, Mumbai) Diploma in Computer Engineering question paper for Summer 2018 examination.
    This question is asked for 4 marks and hence requires some explanation along with program code. Please let me know the solution.
    Q) Write a program to show object as function argument. [ 4 Mark]
  • Answers

    1 Answers found.
  • Though the class is a user-defined data type, it still behaves like a built-in data type. It means we can do almost everything with class and object of a class that we can do with built-in data types. This is the real beauty of Object Oriented Programming.
    The function is a self-contained block of a program which performs a predefined task. In normal practice, we can easily pass integer, float, character, array, structure variable, pointer variable as an argument to a function. In the same way, it is possible to pass one or more objects of a class as an argument to a function. In my program when the statement "t1.sum(t3);" appears, it means the t1 object is going call a function named as the sum and object t2 get passed as an argument to the function. Inside the sum function members of object t1 (which is responsible for the call) are accessed directly and members of object t2(which is passed as an argument) are accessed using . (dot) operator.

    Below is the program which may satisfy you. Please include iostream.h and conio.h header files before the definition of class.

    //PROGRAM TO SHOW OBJECT AS A FUNCTION ARGUMENT
    #include
    #include
    class time
    {
    private:
    int h,m,s;
    public:
    time() {};
    time(int hr, int mn, int sc)
    {
    h=hr;
    m=mn;
    s=sc;
    }
    time sum(time t1)
    {
    time temp;
    temp.h=h+t1.h;
    temp.m=m+t1.m;
    temp.s=s+t1.s;
    int hr,min,sec;
    sec=s+t1.s;
    min=sec/60;
    sec=sec%60;
    temp.s=sec;
    temp.m=temp.m+min;
    min=temp.m;
    hr=min/60;
    min=min%60;
    temp.m=min;
    temp.h=temp.h+hr;
    return(temp);
    }
    void display(void)
    {
    cout<<"\nHours = "< cout<<"\nMinutes = "< cout<<"\nSeconds = "< }
    };

    void main(void)
    {
    int hr,mn,sc;
    clrscr();
    cout<<"Enter hours, minutes and seconds for first object\n";
    cin>>hr>>mn>>sc;
    class time t1(hr,mn,sc);
    cout<<"Enter hours, minutes and seconds for second object\n";
    cin>>hr>>mn>>sc;
    class time t2(hr,mn,sc);
    class time t3;
    t3=t1.sum(t2);
    clrscr();
    cout<<"\nTime details of first object are as follows\n";
    t1.display();
    cout<<"\nTime details of second object are as follows\n";
    t2.display();
    cout<<"\nAfter sumation of two objects time details of time summation are as follows\n";
    t3.display();
    getch();
    }


  • Sign In to post your comments