## jasonlys

Hi I'm new to C++ and would really appreciate if someone could help me. How do you exactly put the friend function into this program? Not that I haven't tried. I really have no idea where to put and what to change. My program is grouped into three different parts. Thank you in advance =D

``````#ifndef DISTANCE_H
#define DISTANCE_H

class Distance {
public:
Distance(int = 0, int = 0, int = 0);
Distance distance ( const Distance &);

void print(void);
void printDistance();

private:
int x, y, z;
double d;

};

#endif

#include <iostream>
#include <cmath>
using namespace std;

#include "distance.h"

Distance::Distance(int a, int b, int c)
{
x = a;
y = b;
z = c;
}

Distance Distance::distance(const Distance &h)
{
Distance k;
int w;
k.x = x - h.x;
k.y = y - h.y;
k.z = z - h.z;
w = pow(k.x,2)+pow(k.y,2)+pow(k.z,2);
k.d = sqrt(w);
return k;
}

void Distance::print()
{
cout << "[ "<< x << ", "<< y << ", "<< z << " ]";
}

void Distance::printDistance()
{
cout << d;
}
#include "Distance.h"
#include "distance.cpp"

void main(){
int i,j,k,l,m,n;

cout << "Enter three points in XYZ plane: ";
cin >> i >> j >> k >> l >> m >> n;
Distance p0(i,j,k), p1(l,m,n), p2;

p2 = p1.distance(p0);
cout << " The distance between ";
p0.print();
cout << " and ";
p1.print();
cout << " is " ;
p2.printDistance();
cout <<endl;

}``````

## Narue 5,707

>How do you exactly put the friend function into this program?
What friend function? The syntax looks like this:

``````#include <iostream>

class Test {
int i;
public:
Test ( int init )
: i ( init )
{}

friend void display ( const Test& test );
};

void display ( const Test& test )
{
std::cout<< test.i <<'\n';
}

int main()
{
Test test ( 12345 );

display ( test );
}``````

## jasonlys

yea something like that. I can't seem to figure out where to put it and what to do with the program in order for it to load. I wanna allow access into the private members and print out the distance calculated.

## Narue 5,707

You're already doing that with member functions. Why don't you show me the main function you'd like to have, then I'll tell you how to accomplish that in your class definition.

## jasonlys

well basically my lecturer wants us to use friend and put it into the program above. thats all he said =(

## Narue 5,707

>thats all he said =(
So...if you don't know what he wants, how can you possibly expect me to understand what you want? I'm reasonably sure that if you engage your brain, you'll see that my example is basically what you want without all of the extra calculations and niceties.

## jasonlys

Im sorry =X okay what i want to do is to add friend into my classes and allow the program to print out the distance. Here's what I done so far and the error:

``````#ifndef DISTANCE_H
#define DISTANCE_H

class Distance {
public:
Distance(int = 0, int = 0, int = 0);
Distance distance ( const Distance &);

void print(void);
friend void printDistance(Distance& );

private:
int x, y, z;
double d;

};

#endif

#include <iostream>
#include <cmath>
using namespace std;

#include "distance.h"

Distance::Distance(int a, int b, int c)
{
x = a;
y = b;
z = c;
}

Distance Distance::distance(const Distance &h)
{
Distance k;
int w;
k.x = x - h.x;
k.y = y - h.y;
k.z = z - h.z;
w = pow(k.x,2)+pow(k.y,2)+pow(k.z,2);
k.d = sqrt(w);
return k;
}

void Distance::print()
{
cout << "[ "<< x << ", "<< y << ", "<< z << " ]";
}

void printDistance(Distance& distance)
{
cout << distance.d;
}

#include "Distance.h"
#include "distance.cpp"

void main(){
int i,j,k,l,m,n;

cout << "Enter three points in XYZ plane: ";
cin >> i >> j >> k >> l >> m >> n;
Distance p0(i,j,k), p1(l,m,n), p2;

p2 = p1.distance(p0);
cout << " The distance between ";
p0.print();
cout << " and ";
p1.print();
cout << " is " ;
p2.printDistance() ;
cout <<endl;

}``````

and my error is:

``````C:\Documents and Settings\User\Desktop\pratical.cpp(17) : error C2039: 'printDistance' : is not a member of 'Distance'
c:\documents and settings\user\desktop\distance.h(4) : see declaration of 'Distance'
Error executing cl.exe.``````

## Narue 5,707

>p2.printDistance() ;
The error tells you exactly what the problem is, and I'm sure it points you to this line. printDistance is a friend, not a member. Friends are not a part of the class, so you call them as you would if the class didn't exist:

``printDistance ( p2 );``