Here is an example of a typical method declaration: Show
public double calculateAnswer(double wingSpan, int numberOfEngines, double length, double grossTons) { //do the calculation here } The only required elements of a method declaration are the method's return type, name, a pair of parentheses, More generally, method declarations have six components, in order:
Modifiers, return types, and parameters will be discussed later in this lesson. Exceptions are discussed in a later lesson. Definition: Two of the components of a method declaration comprise the method signature—the method's name and the parameter types. The signature of the method declared above is: calculateAnswer(double, int, double, double) Naming a MethodAlthough a method name can be any legal identifier, code conventions restrict method names. By convention, method names should be a verb in lowercase or a multi-word name that begins with a verb in lowercase, followed by adjectives, nouns, etc. In multi-word names, the first letter of each of the second and following words should be capitalized. Here are some examples: run runFast getBackground getFinalData compareTo setX isEmpty Typically, a method has a unique name within its class. However, a method might have the same name as other methods due to method overloading. Overloading MethodsThe Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists (there are some qualifications to this that will be discussed in the lesson titled "Interfaces and Inheritance"). Suppose that you have a class that can use calligraphy
to draw various types of data (strings, integers, and so on) and that contains a method for drawing each data type. It is cumbersome to use a new name for each method—for example, public class DataArtist { ... public void draw(String s) { ... } public void draw(int i) { ... } public void draw(double f) { ... } public void draw(int i, double f) { ... } } Overloaded methods are differentiated by the number and the type of the arguments passed into the method. In the code sample, You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart. The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type. Note: Overloaded methods should be used sparingly, as they can make code much less readable. Developing programming and coding technologies. Website design. Cyber space concept. IntroductionIn this tutorial, we will learn how to create a friend function in C++ with the help of some examples. Data hiding is a fundamental concept in object-oriented programming, and it restricts the access of private members from outside the class.
What is a Friend Function in C++?A friend function in C++ is defined as a function that can access private, protected and public members of a class. The friend function is declared using the friend keyword inside the body of the class. Friend Function Syntax: class className { ... .. ... friend returnType functionName(arguments); ... .. ... } By using the keyword, the ‘friend’ compiler understands that the given function is a friend function. We declare friend function inside the body of a class, whose private and protective data needs to be accessed, starting with the keyword friend to access the data. We use them when we need to operate between two different classes at the same time. What is Friend Function?Friend functions of the class are granted permission to access private and protected members of the class in C++. They are defined globally outside the class scope. Friend functions are not member functions of the class. So, what exactly is the friend function? A friend function in C++ is a function that is declared outside a class but is capable of accessing the private and protected members of the class. There could be situations in programming wherein we want two classes to share their members. These members may be data members, class functions or function templates. In such cases, we make the desired function, a friend to both these classes which will allow accessing private and protected data of members of the class. Generally, non-member functions cannot access the private members of a particular class. Once declared as a friend function, the function is able to access the private and the protected members of these classes. User-defined Function typesFriend functions in C++ have the following types
Declaration of a friend function in C++class class_name { friend data_type function_name(arguments/s); //syntax of friend function. }; In the above declaration, the keyword friend precedes the function. We can define the friend function anywhere in the program like a normal C++ function. A class’s function definition does not use either the keyword friend or scope resolution operator (: 🙂. Friend function is called as function_name(class_name) and member function is called as class_name. function_name. Use of Friend function in C++As discussed, we require friend functions whenever we have to access the private or protected members of a class. This is only the case when we do not want to use the objects of that class to access these private or protected members. To understand this better, let us consider two classes: Tokyo and Rio. We might require a function, metro(), to access both these classes without any restrictions. Without the friend function, we will require the object of these classes to access all the members. Friend functions in c++ help us avoid the scenario where the function has to be a member of either of these classes for access. C++ function overloading Two functions can have the same name if the number and type of argument passed is different. Functions that have the same name , but have different arguements are called Overloading functions. Friend functions are also used in operator overloading. The binary operator overloading in c++ using the friend function can be done as explained below. Binary operator overloading in C++ using Friend functionThe operator overloading function precedes a friend keyword in this approach. It also declares a function class scope. The friend operator function takes 2 parameters in a binary operator. It then varies one parameter in a unary operator. The function will be implemented outside the class scope. But, the working and the implementation are the same as the binary operator function. If you want to build your knowledge in C++, consider getting certified. This Introduction to C++ Free Course will help you learn the required skills and also a certificate on completion that can be added to your social profiles. You can also check out our Full Stack Program by IIT Roorkee. Characteristics of Friend Function in C++
Implementing Friend FunctionsFriend Functions can be implemented in two ways: A method of another class:We declare a friend class when we want to access the non-public data members of a particular class. A Global function:A ‘global friend function’ allows you to access all the private and protected members of the global class declaration. A simple example of a C++ friend function used to print the length of the box. Code:#include <iostream> using namespace std; class Box { private: int length; public: Box (): length (0) {} friend int printLength (Box); //friend function }; int printLength (Box b) { b. length +=10; return b. length; } int main () { Box b; cout <<” Length of box:” <<printLength (b)<<endl; return 0; } Output:Length of box:10 Simple example when the function is friendly for two classes. Code:#include<iostream> using namespace std; class B; //forward declaration. class A { int x; public: void setdata (int i) { x=i; } friend void max (A, B); //friend function. } ; class B { int y; public: void setdata (int i) { y=i; } friend void max (A, B); }; void max (A a, B b) { if (a.x >= b.y) std:: cout<< a.x << std::endl; else std::cout<< b.y << std::endl; } int main () { A a; B b; a. setdata (10); b. setdata (20); max (a, b); return 0; } Output: 20 In the above example, max () function is friendly to both class A and B, i.e., the max () function can access the private members of two classes. Implementing through a method of another classA class cannot access the private members of another class. Similarly, a class cannot access its protected members of a class. We need a friend class in this case. A friend class is used when we need to access private and protected members of the class in which it has been declared as a friend. It is also possible to declare only one member function of another class to be a friend. Declaration of friend classclass class_name { friend class friend_class;// declaring friend class }; class friend_class { }; All functions in friend_class are friend functions of class_name. A simple example of a friend class:Code:#include <iostream> using namespace std; class A { int x=4; friend class B; //friend class }; class B { public: void display (A &a) { cout<<”value of x is:” <<a.x; } }; int main () { A a; B b; b. display (a); return 0; } Output:value of x is:4 Implementing a global functionCode:#include<iostream> using namespace std; class space { int x; int y; int z; public: void setdata (int a, int b, int c); void display(void); friend void operator- (space &s); }; void space ::setdata (int a, int b, int c) { x=a; y=b; z=c; } void space::display(void) { cout<<x<<" "<<y<<" "<<z<<"\n"; } void operator- (space &s) { s.x =- s.x; s.y =- s.y; s.z =- s.z; } int main () { space s; s. setdata (5,2,9); cout<<"s:"; s. display (); -s; cout<<"-s:"; s. display (); return 0; } Output:s: 5 2 9 -s: -5 -2 -9 In the above example operator- is the friend function globally declared at the scope of the class. Friend Class in C++What is a Friend class in C++?Friend Class is a class that can access both private and protected variables of the class in which it is declared as a friend, just like a friend function. Classes declared as friends to any other class will have all the member functions as friend functions to the friend class. Friend functions are used to link both these classes.
class One{ <few lines of code here> friend class Two; }; class Two{ <few lines of code> }; Note : Unless and until we declare, class friendship is neither mutual nor inherited. To make you understand in detail:
Advantages of friend function in C++
Summary of C++ Friend Function
This brings us to the end of the blog on Friend functions in C++. Hope this helps you to up-skill your C++ skills. Also, if you are preparing for Interviews, check out these Interview Questions for C++ to ace it like a pro. FAQs
In C++, a function that has access to a class’s private, protected, and public members is referred to as a friend function. Within the class’s body, the friend keyword is used to declare the friend function. What is friend function with example? In C++, a friend function is a unique function that, although not being a member of a class, has the ability to access secret and protected data. Using the term “friend” inside the class, a friend function is a non-member function or regular function of a class that is specified as a friend. What are the advantages of the friend function in C++? Some of the advantages of the friend function in c++ are What are the limitations of friend function? The major disadvantage of friend functions is that they occupy the maximum size of the memory and can’t do any run-time polymorphism concepts. When a member function is defined outside of the class declaration the function name must be?If you define a member function outside of its class definition, it must appear in a namespace scope enclosing the class definition. You must also qualify the member function name using the scope resolution ( :: ) operator.
When a member function is defined inside the class then it is function treated as?If a member function is defined inside a class declaration, it is treated as an inline function, and there is no need to qualify the function name with its class name.
How is a member function of a class defined or declared?Member functions are operators and functions that are declared as members of a class. Member functions do not include operators and functions declared with the friend specifier. These are called friends of a class. You can declare a member function as static ; this is called a static member function.
Which is used to define a member function of a class?Which keyword is used to define the static member functions? Explanation: The static keyword is used to declare any static member function in a class.
|