Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

unable to run sum of 2 no.s with friend of 2 classes. pls help..

+4 votes
asked Sep 26 by Tanveer Singh (260 points)
my program link - https://onlinegdb.com/4InM1Bxxj

#include <iostream>
#include <conio.h>
using namespace std;

class third;
class one;
class two
{
    int a;
    public:
    void set (int i)
    {
        a=i;
    }
    friend void max (two,one,third);
};
class one
{
    int b;
    public:
    void set (int i)
    {
        b=i;
    }
    friend void max (two,one,third);
};
 class third
 {
     int c;
     public:
     void set (int i)
     {
         c=a+b;
     }
     friend void max (two,one,third);
 }
    void max (two x,one y,third z)
    {
        z.c=x.a+y.b;
        cout<<z.c;
    }
int main()
{
    one obj;
    obj.set(100);
    two obj1;
    obj1.set(200);
    third obj;
    obj2.set (obj + obj.1);
}

2 Answers

+1 vote
answered Sep 27 by Peter Minarik (63,140 points)
edited Sep 28 by Peter Minarik

I'm not sure what you're trying to achieve here as your code is hard to follow. For instance, max() does not count the maximum but sets a value based on two other values, and then prints this value. Normally, the parameter list would have been one, two, three, but for some reason, you chose a different order.  You do not use constructors to initialize your classes, but rather use a set() method. Furthermore, you have three identical classes, they only differ in their names. Why not just have one?

Again, your logic is hard to follow.

I've "fixed" your code to compile and run:

#include <iostream>
#include <conio.h>
using namespace std;

class third;
class one;

class two
{
    int a;
public:
    void set (int i)
    {
        a = i;
    }
    friend void max(two, one, third);
};

class one
{
    int b;
public:
    void set(int i)
    {
        b = i;
    }
    friend void max(two, one, third);
};

class third
{
    int c;
public:
    void set(int i)
    {
        c = i;
    }

    friend void max(two, one, third);
};

void max(two x, one y, third z)
{
    z.c = x.a + y.b;
    cout << z.c;
}

int main()
{
    one obj;
    obj.set(100);
    two obj1;
    obj1.set(200);
    third obj2;
    max(obj1, obj, obj2);
    return 0;
}

Here's a better solution to the problem. Use whatever you deem appropriate for your needs:

#include <iostream>

template<class T> class Number
{
private:
    T _value;
public:
    Number(T value) : _value(value) { }

    friend T Sum<T>(const Number<T> & a, const Number<T> & b, const Number<T> & c);
};

template<class T> T Sum(const Number<T> & a, const Number<T> & b, const Number<T> & c)
{
    return a._value + b._value + c._value;
}

int main()
{
    Number<int> i1(100);
    Number<int> i2(200);
    Number<int> i3(300);
    std::cout << Sum(i1, i2, i3) << std::endl;

    Number<double> d1(0.1);
    Number<double> d2(0.2);
    Number<double> d3(0.3);
    std::cout << Sum(d1, d2, d3) << std::endl;
    return 0;
}
commented Sep 29 by Tanveer Singh (260 points)
thank you very much...
+1 vote
answered Sep 27 by Rohit Kumar Reddy (160 points)
#include <iostream>
#include <conio.h>
using namespace std;

class third;
class one;
class two
{
    public:
    int a;
    void set (int i)
    {
        a=i;
    }
    friend void add(two,one,third);
};
class one
{
    public:
    int b;
    void set (int i)
    {
        b=i;
    }
    friend void add(two,one,third);
};
 class third
 {
     int c;
     public:
     void set (int i)
     {
         c=i;
     }
     friend void add(two,one,third);
 };
    void add(two x,one y,third z)
    {
        z.c=x.a+y.b;
        cout<<z.c;
    }
int main()
{
    one obj;
    obj.set(100);
    two obj1;
    obj1.set(200);
    third obj2;
    obj2.set(obj.b + obj1.a);
    add(obj1,obj,obj2);
    return 0;
}
commented Sep 29 by Tanveer Singh (260 points)
thank you very much...
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...