Sie sind auf Seite 1von 4

Lab 04 : Understanding friends and overloading in C++

Objectives of the lab:


1. Understanding friends function and overloading in C++.
Friend Function.
 Friend function is not a member function of a class to which it is a friend.
 Friend function declear in the class with friend keyword.
 It must be define outside the class to which it is friend.
 Friend function can access any member of the class directly.
 It has no caller function or caller object.
 It should not be defined with membership label.(means can not use scope
resolution because it can be used only with member function)

Syntax:
class class_name
{
friend data_type function_name(argument/s); // syntax of friend
function.
};

Activity # 1:
#include<iostream>
using namespace std;
class Distance {
private:
int meter;
public:
friend int func(Distance); //friend function
};
int func(Distance d){
//function definition
d.meter=10; //accessing private data from non-member function
return d.meter;
}
int main(){
Distance D;
cout<<"Distace: "<<func(D);
return 0;
}

Arifa Awan
Lab 04 : Understanding friends and overloading in C++

Friends function access data from two classes:


 Friend function can become a friend more than one classes but member
function not.
ACTIVITY # 2:
#include<iostream>
using namespace std;
class B;
class A{
private:
int a;
public:
void setData(int x){
a=x;
}
friend void fun(A, B);
};
class B{
private:
int b;
public:
void setData(int y){
b=y;
}
friend void fun(A, B);
};
void fun(A obj1, B obj2){
cout << "sum of two numbers" << o1.a+o2.b;
}
int main(){
A obj1;
B obj2;
obj1.setData(2);
obj2.setData(4);
fun(obj1, obj2);
}

Arifa Awan
Lab 04 : Understanding friends and overloading in C++

Operator overloading:
The concept of overloading is generally used when a program block conceptually
executes the same task but with a slight distinctness in a set of parameters. The
concept of overloading is generally used when a program block conceptually
executes the same task but with a slight distinctness in a set of parameters.
There are some C++ operators which we can't overload.
 Class member access operator (. (dot), .* (dot-asterisk))
 Scope resolution operator (::)
 Conditional Operator (?:)
 Size Operator (sizeof)
Operator overloading is one of the most exciting features of object oriented
programming. It can transform complex, obscure program listings into intuitively
obvious ones. For example, statements like
d3.add_distances(d1, d2);
or the similar
d3 = d1.add_distances(d2);
can be changed to the much more readable
d3 = d1 + d2;
The term operator overloading refers to giving the normal C++ operators,
such as +, *, <=, and +=, additional meanings when they are applied to
user-defined data types like classes.
Defining Operator Overloading
To define an additional task to an operator, we must specify its means in
relation to the class to which the operator is applied. This is done by
using the operator function. The general form of an operator function is:
return_type classname :: operator op(arglist)
{
Function body // task defined
}
It can be class member and can be a friend function.
Return-type operator op (arg list)
{
// for inside the class

}
where :

Arifa Awan
Lab 04 : Understanding friends and overloading in C++

return_type : is the type of value returned by the specified operation


op : is the operator being overloaded
“operator op” : is the function name

Overloading Binary Operators


Binary operators act on two operands. Examples include +, -, *, and /.
The mechanism of overloading binary operators is the same as that of unary
operators. In the program below we will see how to add two
complex numbers using binary operator overloading.

Arifa Awan

Das könnte Ihnen auch gefallen