How to use a friend function in this code.

This code asks the user to input the reistance and voltage and calculates current and power - i'm learning friend function but i can't understand them. Could someone please explain them and tell me how i could apply them to this code? Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>

//-----------interface--------------//
class Ohm
{
      public:
             Ohm(double res,double vol)
             {
                     R = res;
                     V = vol;
             }
             void current(double,double);
             void power(double, double);
      private:
              double R;
              double V;
              double I;
              double P;
              friend double thing(Ohm O);  //<--how do i use this?
};

//-----------implementation----------//
void Ohm::current(double R, double V)
{
                  double I=V/R;
                  std::cout<<"The current is: ";
                  std::cout<<I<<"\n";
}

void Ohm::power(double I, double V)
{
                  double P=I*V;
                  std::cout<<"The power is: ";
                  std::cout<<P<<"\n";
}

//---------------main-----------------//
int main()
{
    int r,v;
    std::cout<<"Enter Resistance:"; std::cin>>r;
    std::cout<<"Enter Voltage: "; std::cin>>v;
    
    Ohm circuit(r,v);
    circuit.current(R,V);
    circuit.power(R,V);
    system("pause");
    return 0;
}
When you declare a function (or another class) to be a friend of your class, you effectively allow it to access your private members as if they were public to it. Code Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;

class MyClass
{
private:
    int a;
    int b;

public:
    MyClass():a(0),b(0){}
    ~MyClass(){}

    void member_set(int a_, int b_)
    {
        a=a_;
        b=b_;
    }

    friend void print(const MyClass & mc);
};

void print(const MyClass & mc)
{
    cout << "a=" << mc.a;
    cout << ", b=" << mc.b << endl;
}

int main()
{
    MyClass my_object;
    my_object.member_set(-1,2);

    print(my_object);

    cout << "\nhit enter to quit...";
    cin.get();
    return 0;
}

More info on friend functions -> http://cplusplus.com/doc/tutorial/inheritance/

In your main function you should do:

1
2
circuit.current(r,v);
circuit.power(r,v);

But you could just make the current and power functions take no arguments, since you hold that data in your class anyway.
I removed the arguments from the current and power functions (was such an idiot to put them there in the first place) - now I have another problem: when circuit calls the current function it manages to calculate the current - but this value of current doesnt get passed to Power so Power always comes out to be zero - how do I sort this out?
I'm dreadfully sorry - but i'm really quite bad at this classes stuff.
Mmmm... How do your functions look like? Like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//-----------implementation----------//
void Ohm::current()
{
                  double I=V/R;
                  std::cout<<"The current is: ";
                  std::cout<<I<<"\n";
}

void Ohm::power()
{
                  double P=I*V;
                  std::cout<<"The power is: ";
                  std::cout<<P<<"\n";
}

If yes, you still have a problem. You see, that I in your current function is not the member variable I. You create a new, temporary variable here. A partial solution would be to remove the double keyword from the above piece of code (from both current and power function). I'm saying partial because then you would also have a problem if you wanted to calculate first the power and then the current (because in calculating the power you use the current). A better way would be to calculate these values when you create your object (in the constructor) and just print the values in your current and power functions. Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
using namespace std;

class MyClass
{
    int a;
    int b;
    int c;
    int d;

    void calculate()
    {
        c=a+b;
        d=a*b;
    }

public:
    MyClass(int a_, int b_)
    {
        a=a_;
        b=b_;
        calculate();
    }

    void set_a(int a_)
    {
        a=a_;
        calculate();
    }

    void set_b(int b_)
    {
        b=b_;
        calculate();
    }

    void print()
    {
        cout << "a=" << a;
        cout << ", b=" << b;
        cout << ", c=" << c;
        cout << ", d=" << d << endl;
        cout << endl;
    }
};

int main()
{
    MyClass my_object(2,3);
    my_object.print();

    my_object.set_a(5);
    my_object.print();

    my_object.set_b(2);
    my_object.print();

    cout << "hit enter to quit...";
    cin.get();
}

waqqassheikh wrote:
I'm dreadfully sorry

Don't be.

waqqassheikh wrote:
i'm really quite bad at this classes stuff

The only real thing about that statement is the association you believe to be with it.
Last edited on
Thanks a million!
It works perfectly now.

Here's the final code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>

//-----------interface--------------//
class Ohm
{
      public:
             Ohm(double res,double vol)
             {
                     R = res;
                     V = vol;
                     thingy();
                     
             }
             void print();
      private:
              double R;
              double V;
              double I;
              double P;
              void thingy()
              {
                   I=V/R;
                   P=I*V;
              }
      
};
//---------------implementation-------//
void Ohm::print()
{
     std::cout<<"R: "<<R<<std::endl;
     std::cout<<"V: "<<V<<std::endl;
     std::cout<<"I: "<<I<<std::endl;
     std::cout<<"P: "<<P<<std::endl;
}     

//---------------main-----------------//
int main()
{
    int r,v;
    std::cout<<"Enter Resistance:"; std::cin>>r;
    std::cout<<"Enter Voltage: "; std::cin>>v;
    
    Ohm circuit(r,v);
    circuit.print();
    system("pause");
    return 0;
}

Topic archived. No new replies allowed.