user14786650
user14786650

Reputation:

Output provides size of integer instead of the actual integer

// Create classes called Class1 and Class2 with each having one private member. Add member function to set a value (say setValue) on each class. Add one more function max() that is friendly to both classes, max() function should compare two private member of two classes and show maximum among them. Create one-one object of each class and set a value on them. Display the maximum number among them

#include <iostream>
using namespace std;

class Class1
{
    int a;

    public:
        int setValue();
    
    friend int max();
};

class Class2
{
    int b;

    public:
        int setValue();

    friend int max();
};

int Class1::setValue()
{
    cout<<"Enter the first number:";
    cin>>a;
}


int Class2::setValue()
{
    cout<<"Enter the second number:";
    cin>>b;
}

int max()
{
    Class1 c1;
    Class2 c2;

    if(c1.a>c2.b)
    {
        cout<<"Greater number: "<<c1.a<<endl;
    }

    else
    {
        cout<<"Greater number: "<<c2.b<<endl;
    }
}

int main()
{
    Class1 obj1;
    Class2 obj2;

    obj1.setValue();
    obj2.setValue();
    cout<<endl;
    max();

    return 0;
}

Upvotes: 1

Views: 120

Answers (3)

Alex Riveron
Alex Riveron

Reputation: 411

You should try passing the Class1 and Class2 objects you want to compare instead of creating new local Class1 and Class2 objects inside of the max function.

Try something like this instead:

#include <iostream>
using namespace std;

class Class1;
class Class2;

class Class1
{
    int a;

    public:
        void setValue();
    
    friend int max(Class1 c1, Class2 c2);
};

class Class2
{
    int b;

    public:
        void setValue();

    friend int max(Class1 c1, Class2 c2);
};

void Class1::setValue()
{
    cout<<"Enter the first number:";
    cin>>a;
}


void Class2::setValue()
{
    cout<<"Enter the second number:";
    cin>>b;
}

int max(Class1 c1, Class2 c2)
{
    int maxValue;
    
    if(c1.a>c2.b)
    {
        cout<<"Greater number: "<<c1.a<<endl;
        maxValue = c1.a;
    }

    else
    {
        cout<<"Greater number: "<<c2.b<<endl;
        maxValue = c2.b;
    }

    return maxValue;
}

int main()
{
    Class1 obj1;
    Class2 obj2;

    obj1.setValue();
    obj2.setValue();
    cout<<endl;
    int maxValue = max(obj1, obj2);

    return 0;
}

Upvotes: 0

A K
A K

Reputation: 183

I see some discrepancies in your code. First of all, what are you trying to return in setValue() method. You have made return type int instead of void into those setValue() method that is gonna give some error for sure. Into that friend function that finds for the maximum value also need to return an int as you have declared into the prototype of max(). I believe you should not return anything into from setValue() method.


Another this that you have done terribly wrong is:

Class1 c1;
Class2 c2;

Inside of max method, instead of passing the objects as arguments to the max method. you have not passed the objects to the arguments of max() method. Creating a new objects of class1 and class2 inside of max method will not help. At the same time output will be undefined from max method.


Solution:

#include <iostream>
using namespace std;

class Class1
{
    int a;

    public:
        void setValue();
    
    friend int max(Class1 obj1, Class2 obj2);
};

class Class2
{
    int b;

    public:
        void setValue();

    friend int max(Class1 obj1, Class2 obj2);
};

void Class1::setValue()
{
    cout<<"Enter the first number:";
    cin>>a; // this sets the value of private data-member a.
}


void Class2::setValue()
{
    cout<<"Enter the second number:";
    cin>>b; // this sets the value of private data-member b.
}

int max(Class1 obj1, Class2 obj2)
{

    int max;


    if(obj1.a>=obj2.b)
    {
        cout<<"Greater number: "<<obj1.a<<endl;
        max=obj1.a;
    }

    else
    {
        cout<<"Greater number: "<<obj2.b<<endl;
        max=obj2.b;
    }
    return max;
}

int main()
{
    Class1 obj1;
    Class2 obj2;

    obj1.setValue();
    obj2.setValue();
    cout<<endl;
    int maximum=max(obj1, obj2);
    cout<<"Maximum is: "<< maximum<< endl;
    return 0;
}

Hope, this will help. :-)

Upvotes: 1

eerorika
eerorika

Reputation: 238411

You've declared that max as well as both member functions setValue return int. Those functions execute to the end without returning a value. The behaviour of the program is undefined.

Solution: Either return a value, or declare the function to return void.


Here:

Class1 c1;
Class2 c2;

You default initialise two variables. The members of those objects will be default initialised which in the case of int means that they have an indeterminate value.

On this line:

if(c1.a>c2.b)

You compare indeterminate value to another indeterminate value. Same goes when outputting the greater value. The behaviour of the program is undefined.

Solution: Initialise all objects whose value you use. In this case, I suspect that you were supposed to not create new, uninitialised variables, but to read existing objects that were created in main. Typically, that is achieved by passing the values into the function as arguments.

Upvotes: 1

Related Questions