• 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
https://www.neowin.net/forum/topic/190328-homework-for-friend-functions/
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();
}

  • 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();
}

  • 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.

  • 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!!!!

  • 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.

  • 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

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

    • No registered users viewing this page.
  • Posts

    • Why should simply opening the Action Center ... require any significant amount of CPU cycles? Too many are ignoring the elephant in the room: today's CPUs may have similar clock frequencies to CPUs from a decade ago (~4 to 5 GHz), but they run with far higher performance per clock, even on the smallest E-cores. The Action Center is a static, minor desktop element. It ought to open instantly on any CPU made in the past 2.5 decades at even low clocks. Opening a program? Boost away. Unzipping a file? Boost to your heart's content. Right-clicking the notification area? This should be an ultra-efficient, low-impact event that opens instantly. Compare today's UI elements to Windows 7 UI elements. Today's UI elements hardly do anything more substantial or important or critical, yet now require modern-day processors to jump half a GHz to reduce their latency.
    • This is not an option lmao: it is part of the KB5094126 update, pushed to all users. There is no GUI to configure it. Reading the article should be required for commenting.
    • Can you recommend me a good Pixel 7 on eBay? Because some are saying Gen 1, and I don't think that's what I want, right?
    • Close, but it is for any application starting, not just during boot time. In fact, it probably will not improve boot time at all because during a boot, the CPU is already boosting, so a pre-boost wouldn't change anything. I agree that it isn't exciting (especially considering every other OS already does it), but it is a good thing, even if they are just playing catch up.
    • Any so called performance increase will be in milliseconds, which nobody will actually notice in real world usage.
  • Popular Contributors

    1. 1
      +primortal
      501
    2. 2
      PsYcHoKiLLa
      198
    3. 3
      +Edouard
      156
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      71
  • Tell a friend

    Love Neowin? Tell a friend!