Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

What is the difference between a friend function for a class and a member function for the class?

Short Answer

Expert verified
Based on the given step-by-step solution, the difference between a friend function and a member function for a class can be summarized as follows: 1. A friend function is not a member of a class but can access its private and protected members. A member function is part of the class and can access all its members. 2. Friend functions are declared using the 'friend' keyword, while member functions are declared within the class without this keyword. 3. Friend functions are called like regular functions with the object as a parameter, while member functions are called using the dot operator.

Step by step solution

01

Definition of Friend Function and Member Function

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 used to extend the functionality of a class without modifying the class itself. A member function, on the other hand, is a function that is declared within a class and is part of that class. It has access to the members of the class and can interact with them. Member functions are used to define the behavior of a class.
02

Access Specifiers

Friend functions have access to private and protected members of the class because they are declared as friends of the class. This allows them to operate on the class's data members directly. However, they do not have access to the class's methods as those are part of the class itself. Member functions have access to all the members (data and functions) of the class, private and protected included, as they are part of the class itself.
03

Declaration

Friend functions are declared using the keyword 'friend' followed by the function declaration inside the class definition. ```cpp class ExampleClass { private: int data; public: friend void friendFunction(ExampleClass&); }; ``` Member functions are declared within the class definition without the 'friend' keyword: ```cpp class ExampleClass { private: int data; public: void memberFunction(); }; ```
04

Calling

Friend functions are called like regular (non-member) functions, using the object of the class as an argument: ```cpp ExampleClass obj; friendFunction(obj); ``` Member functions are called using the object of the class and the dot (.) operator: ```cpp ExampleClass obj; obj.memberFunction(); ``` In summary, a friend function is not a member of a class but can access its private and protected members, while a member function is a part of the class and can access all its members. Friend functions are declared using the 'friend' keyword, and member functions are declared within the class without this keyword. Friend functions are called like regular functions with the object as a parameter, while member functions use the dot operator.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Suppose you wish to add a friend function for subtraction to the class Money defined in Display \(11.3 .\) What do you need to add to the description of the class Money that we gave in Display \(11.3 ?\) The subtraction function should take two arguments of type Money and return a value of type Money whose value is the value of the first argument minus the value of the second argument.

Change the class TemperatureList given in Display 11.10 by adding a member function called get_size, which takes no arguments and returns the number of temperatures on the list.

a. Explain carefully why no overloaded assignment operator is needed when the only data consists of built-in types. b. Same as part (a) for a copy constructor c. Same as part (a) for a destructor.

Here is a definition of a class called Pairs. Objects of type Pairs can be used in any situation where ordered pairs are needed. Your task is to write implementations of the overloaded operator >> and the overloaded opera- tor << so that objects of class Pairs are to be input and output in the form (5,6) (5,-4) (-5,4) or (-5,-6). You need not implement any constructor or other member, and you need not do any input format checking. #include using namespace std; class Pairs { public: Pairs( ); Pairs(int first, int second); //other members and friends friend istream& operator>> (istream& ins, Pairs& second); friend ostream& operator<< (ostream& outs, const Pairs& second); private: int f; int s; };

Is it possible using operator overloading to change the behavior of \(+\mathrm{on}\) integers? Why or why not?

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free