Wednesday, 11 March 2015

Write a program that will demonstrate object as function argument pass by value

Write a program that will demonstrate object as function argument pass by value. The program that uses the following properties - class name- objarg, two integer type private member variable- a, b. Three public member function - input_add_val(int c, int d), alter_sign(objorg x), show(). input_add_val(int c, int d) get the value from main() and do the addition operation for a and b. show() function will displays the addition result as output. alter_sign(objorg x) change the sign of addition result.

#include<iostream>
using namespace std;
class objarg
{
    int a,b;
public:
    int input_add_val(int c,int d);
    int alter_sign(objarg x);
    int show();
};
int objarg::input_add_val(int c,int d)
{
    a=c;
    b=d;
}
int objarg::show()
{
    cout<<"Addition is: "<<a+b<<endl;
}
int objarg::alter_sign(objarg x)
    {
        x.a=-x.a;
        x.b=-x.b;
        cout<<"Additio of changes is : "<<x.a+x.b<<endl;
    }

int main()
{
    objarg r;
    r.input_add_val(25,25);
    cout<<"Before changes"<<endl;
    r.show();
    cout<<"After changes "<<endl;
    r.alter_sign(r);
    return 0;

}

No comments:

Post a Comment