0% found this document useful (0 votes)
35 views22 pages

Chapter 4

The document discusses friend functions and operator overloading in C++. It defines friend functions and how they provide access to private members of a class. It also explains operator overloading, how to overload operators, and common operators that cannot be overloaded like sizeof. Examples of overloading operators like + and > are provided.

Uploaded by

Ram Bhardwaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views22 pages

Chapter 4

The document discusses friend functions and operator overloading in C++. It defines friend functions and how they provide access to private members of a class. It also explains operator overloading, how to overload operators, and common operators that cannot be overloaded like sizeof. Examples of overloading operators like + and > are provided.

Uploaded by

Ram Bhardwaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Friend Function & Operator

Overloading
Objective

In this chapter learner able to understand:


• Friend Function
• Operator Oevrloading
Friend Functions
 It is possible to grant a nonmember function access to the private members
of a class by using a friend.
 A friend function has access to all private and protected members of the
class for which it is a friend.
 To declare a friend function, include its prototype within the class,
preceding it with the keyword friend.
 Syntax:
public:
friend ret-type fun-name(arg-list);
Example:

#include <iostream.h>
class FriendDemo {
int a, b;
public:
friend int sum(FriendDemo fd);
void setvalue(int i, int j);
};
void FriendDemo::setvalue(int i, int j)
{
a = i;
b = j;
}
int sum(FriendDemo x)
{
return x.a + x.b;
}
int main()
{
FriendDemo fd_ob;
fd_ob.setvalue(30, 40);
cout << sum(fd_ob);
return 0;
}
Operator Overloading
Operator overloading
 Operator overloading means giving capability to the operator to work on
different types of operands.
 The operators +,* etc, works for type int, float etc. we can overload these
operators by giving them the capability to work on derived data types.
 By making the operators to work on derived data types just like they work
on built-in data types, we can have a consistent approach.
 This is done with the help of special function called operator function.
Syntax for operator overloading
return-type classname::operator #(arg-list)
{
//operations
}
 Often, operator functions returns an object of the class they operate on,but
ret-type can be any valid type. The # is a placeholder. When you create an
operator function, substitute the operator for the #.
 Operator functions must be either member functions (nonstatic ) or friend
function.
 A member function has no arguments for unary operators and only one for
binary operators.
 And if there is a friend function then a friend function will have only one
argument for unary operators and two for binary operators.
 Arguments may be passed either by value or by reference.
Rules for overloading operators

 Only existing operators can be overloaded. New operators can not be


created.
 The overloaded operator must have at least one operand that is of derived
data type.
 We can not change the basic meaning of an operator. This is to say, we can
not redefine the plus (+)operator to subtract one value from the other.
 Overloaded operators follow the syntax rules of the original operators.
They cannot be overridden.
 There are some operators that can not be overloaded.
 We cannot use friend functions to overload certain operators . However
member functions can be used to overload them.
Operators that cannot be overloaded

The size of operator (sizeof)


The membership operator (.)
The pointer to member operator ( .* )
The scope Resolution operator ( :: )
The conditional operator (?:)
Where a friend cannot be used

Assignment operator (=)


Function call operator ( () )
Subscripting operator ( [ ] )
Class member access operator (->)
//A PROGRAM OF OPERATOR(+) OVERLOADING
#include<iostream.h>
class Test
{
int x,y;
public:
void set()
{
cout<<"Enter any Two Number:";
cin>>x>>y;
}
void show()
{
cout<<x<<endl<<y;
}
Test operator +(Test p)
{
Test temp;
temp.x=x+p.x;
temp.y= y+p.y;
return (temp);
}
};
int main()
{
Test o1, o2, o3;
o1.set();
o2.set();
o3=o1+o2;
o3.show();
system(“pause”);
return 0;
}
//operator overloading of > Operator
#include<iostream.h>
class Test
{
int x,y;
public:
void set()
{
cout<<"Enter any Two Number :";
cin>>x>>y;
}
void show()
{
cout<<x<<y;
}
int operator >(Test p)
{
if( (x > p.x) && ( y > p.y) )
{
return(1);
}
else
{
return(0);
}
}
};
int main()
{
Test o1, o2;
o1.set();
o2.set();
if(o1>o2)
cout<<“o1 greater”<<endl
else
cout<<“o2 greater”<<endl

}
Namespaces in C++
Namespaces in C++
 The namespace keyword allows you to partition the global namespace by creating a
declarative region.
 A namespace defines a scope.
 Anything defined within a namespace statement is within the scope of that
namespace.
Syntax:

namespace <namespace-name> {
// declarations
}
Formatting Using the ios Members
Formatting Using the ios Members
 Each stream has associated with it a set of format flags that control the way
information is formatted. The ios class declares a bitmask enumeration called fmtflags in
which the following values are defined. (Technically, these values are defined within
ios_base, which is a base class for ios.)

 adjustfield, basefield ,boolalpha, dec


 fixed ,floatfield ,hex, internal
 left, oct, right, scientific
 showbase, showpoint ,showpos, skipws
 unitbuf, uppercase

 These values are used to set or clear the format flags.


Formatting Using the ios Members
member
field effect when set
constant
boolalpha read/write bool elements as alphabetic strings true and false).
showbase write integral values preceded by their corresponding numeric base prefix.
showpoint write floating-point values including always the decimal point.
independent
showpos write non-negative numerical values preceded by a plus sign (+).
flags
skipws skip leading whitespaces on certain input operations.
unitbuf flush output after each inserting operation.
uppercase write uppercase letters replacing lowercase letters in certain insertion operations.
numerical dec read/write integral values using decimal base format.
base hex read/write integral values using hexadecimal base format.
(basefield) oct read/write integral values using octal base format.
float format fixed write floating point values in fixed-point notation.
(floatfield) scientific write floating-point values in scientific notation.
the output is padded to the field width by inserting fill characters at a specified
internal
internal point.
adjustment
left the output is padded to the field width appending fill characters at the end.
(adjustfield)
the output is padded to the field width by inserting fill characters at the
right
beginning.
Setting the Format Flags

 To set a flag, use the setf() function. This function is a member of ios.
 To unset a flag, use the unsetf() function. This function is a member of ios.

fmtflags setf(fmtflags flags);

stream.setf(ios::showpos);

Here, stream is the stream you wish to affect.

Example:
cout.setf(ios::showpoint);
cout.setf(ios::showpos);
cout << 100.0; // displays +100.0
Thank You

You might also like