• 0

Homework for Friend Functions


Question

Please help me soplve the programming problem that has been given to me as homework. I know its bad to get the source from someone else, but plezz help Im a newbie and have no idea what friend functions do: :blink:

Create 2 classes DM and DB which store the value of distance. DM store distances in meters and cm and DB in feet and inches. Write a program that can read values for the class objects and add one object DM with another object of DB.

Use a friend function to carry out the addition operation the object that stores the results may be a DM object or a DB object, depending upon the units in which the results are required. The display should be in the format of feet and inches or meters and cms depending on the object on display.

PS: I'll study what friend function means this weekend. But have to do this homework submission by tomorrow, so plezzz help. I know this may be a cakewalk for most C++ programmers, but Im still a school student.

Link to comment
Share on other sites

15 answers to this question

Recommended Posts

  • 0
A friend function is a function that is not a member of a class but has access to the class's private and protected members. Friend functions are not considered class members; they are normal external functions that are given special access privileges. Friends are not in the class's scope, and they are not called using the member-selection operators (. and ?>) unless they are members of another class. A friend function is declared by the class that is granting access. The friend declaration can be placed anywhere in the class declaration. It is not affected by the access control keywords.

from MSDN.

An Example.

// friend_functions.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;
class Point
{
    friend void ChangePrivate( Point & );
public:
    Point( void ) : m_i(0) {}
    void PrintPrivate( void ){cout << m_i << endl; }

private:
    int m_i;
};

void ChangePrivate ( Point &i ) { i.m_i++; }

int main()
{
   Point sPoint;
   sPoint.PrintPrivate();
   ChangePrivate(sPoint);
   sPoint.PrintPrivate();
}

Link to comment
Share on other sites

  • 0

That's the definition that the notes given by the professor had, but no idea how to implement it! I think I would need the source for the abive problem!!!! Plezz

Link to comment
Share on other sites

  • 0

I just gave you an example that shows how a friend function is declared, implemented, and used. I'm certainly not going to do your homework for you. Why don't you try and mimic what I've shown you and *try* to code something. Post your code when you need help.

Link to comment
Share on other sites

  • 0

This is what I've replicated but have no idea how its gonna work or what it is doing. Just replicated the program given by you!

Plezz give me the source so that atleast I can understand how to implement the given problem after looking at the code!

#include<iostream.h>
#include<conio.h>
class db;
class dm
{
  float metre, cm;
  public: getdm()
  {
    cout << "Enter the distance in meters & centimeters";
    cout << "\nMeters: ";
    cin >> metre;
    cout <<"& Centimeters: ";
    cin >> cm;
    return 0;
  }
    friend add(dm,db);
};
class db
{
  float feet, inches;
  public: getdb()
  {
    cout << "Enter the distance in feets & inches";
    cout << "\nFeet: ";
    cin >> feet;
    cout <<"& Inches: ";
    cin >> inches;
    return 0;
  }
  friend add(dm,db);
};
add(dm meter,db feet)
{
   return 0;
}
main()
{
   clrscr();
   dm A; db B;
   A.add();
   B.add();
   getch();
}

Link to comment
Share on other sites

  • 0

main()
{
  clrscr();
  dm A; db B;
  A.add();
  B.add();
  getch();
}

The function add() is not a member of dm or db. Why? Because...

Friends are not in the class's scope, and they are not called using the member-selection operators (. and ?>)...
Link to comment
Share on other sites

  • 0

You didn't want help; you wanted someone to do the work for you. No one wants to do the work for you; you don't learn and you would be added to the already long list of worthless programmers.

Keep this in mind next time you want "help".

Link to comment
Share on other sites

  • 0

wot a boneidle lazy basta*d. if you arent even prepared to put a bit of work in why the hell do expect help.

i come across people like you ever year. we recruit at least one university graduate every 12 months. some of them have geniune skills, but still need the odd bit of help and got average marks. the ones that pis* me off are the ones with really high grades, but lack basis programming knowledge and have just winged the course and copied everything. they usually get laughed out of the office, but only once we sit them down with a group of developers and make them sweat a bit by throwing them into the deep end. pretty sure it sends them home and they then learn stuff.

Link to comment
Share on other sites

  • 0

Thanx dangermoose and all guys, for pricking my conscience and making me work hard! I finally got the logic to the friend function and didn't go to school today. I got the program which now seems very simple to understand. But then havent yet understood the use of using friend function, since you can actually are killing the logic of encapsulation that is one of the basics of OOP.

#include<iostream.h>
#include<conio.h>
//Program to add distances using friend function
class db;
class dm
{
  float metre,cm,tot;
  public: getdm()
  {
    cout << "Enter the distance in meters & centimeters";
    cout << "\nMeters: ";
    cin >> metre;
    cout <<"& Centimeters: ";
    cin >> cm;
    tot = metre + 0.01 *(cm);
    return 0;
  }
    friend add(dm,db);
};
class db
{
  float feet, inches, total;
  public: getdb()
  {
    cout << "\n\nEnter the distance in feets & inches";
    cout << "\nFeet: ";
    cin >> feet;
    cout <<"& Inches: ";
    cin >> inches;
    total = feet + 0.0833333 *(inches);
    return 0;
  }
  friend add(dm,db);

};
add(dm C,db D)
{
   float feets,meter;
   int key;
   feets = 3.280839*C.tot;
   meter = 0.3048*D.total;
   cout << "In what units do want the addition: ";
   cout << "1.) Meters: ";
   cout << "2.) Feets: ";
   key = getch();
   if(key=='1'|| key=='2')
   {
     if(key=='1') cout << "\n\nMeters: " <<meter+C.tot;
     if(key=='2') cout << "\n\nFeets: "<<feets+D.total;
   }
   else cout <<"\n\nIncorrect Entry: PROGRAM WILL EXIT";
   return 0;
}
void main()
{
   clrscr();
   dm A; db B;
   A.getdm();
   B.getdb();
   add(A,B);
   getch();
}

Thanx all you guys.... Neowin.Net rocks!!!!

Link to comment
Share on other sites

  • 0
But then havent yet understood the use of using friend function, since you can actually are killing the logic of encapsulation that is one of the basics of OOP.

Friends are useful when you implement and override global operators. In other words, instead of making add() your friend, you would make operator+() your friend and implement it like so...

dm operator+(const dm& C, const db& D)
{
    // ... similar code to the add() function...
}

It eliminates the need of having to put public setter and getter methods in your dm or db classes.

Link to comment
Share on other sites

  • 0
the ones that pis* me off are the ones with really high grades, but lack basis programming knowledge and have just winged the course and copied everything.  they usually get laughed out of the office, but only once we sit them down with a group of developers and make them sweat a bit by throwing them into the deep end.  pretty sure it sends them home and they then learn stuff.

I have no clue how you would wing a 400 level csi course... lol

Link to comment
Share on other sites

  • 0
I have no clue how you would wing a 400 level csi course... lol

You cheat.

An EE friend told me about those kind of people in his classes. He guessed that around 75% of the class didn't have a clue and cheated to get through it. Sad.

Link to comment
Share on other sites

  • 0

I agree. I hate people that expect you to do their work for them here. OK I may ask for a tip or two, but I enjoy learning it, not just copying it and changing variables.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.