Download - friend,overload,template
-
8/8/2019 friend,overload,template
1/24
OBJECT ORIENTED
PROGRAMMING(OOP)
POOBHARATHII A/L BALAKRISHNAN 19DIP09F2057
AHMAD SAFWAN BIN AHMAD NASIR 19DIP09F2036
MOHD ASHRAFFUDDIN BIN ASMADI 19DIP09F2002
MOHAMAD UMARUL FITRI BIN LATIF 19DIP09F2004
-
8/8/2019 friend,overload,template
2/24
CLASSES AND
FUNCTION AS A
FRIEND
-
8/8/2019 friend,overload,template
3/24
CREATE CLASSES AND
FUNCTION AS A FRIEND
a. Create a method or function of non-
member as a friends.
b. Create classes as a friend
-
8/8/2019 friend,overload,template
4/24
Definition of friend :
As in the real world
A function or class can be defined as a
friend to other classes Class becomes the friend to another class.
This concept is considered important to
apply if there is a situation, which we wanta class or function wants to access the
private data from the class
-
8/8/2019 friend,overload,template
5/24
A. A method or function of non-
member as a friends.
A friend function of a class is a member
function of the class, but has access to all
the member of the class.
The function want to being a friend to a
class, the reserved word friend precedes
the function prototype in the class
definition.
The word friends appear only the function
prototype in the class.
-
8/8/2019 friend,overload,template
6/24
Consider the followingConsider the following
statement:statement:Class class_IllusFriends
{
Friend void two (/*parameter*/)
..
....
};
-
8/8/2019 friend,overload,template
7/24
B. A function as a friend
By placing the keyword
friend in front of the
functions name, the
function can achieve private
member of the class.
-
8/8/2019 friend,overload,template
8/24
Declaring class as a friend
To make a class as a friend for
another class, declaration of a
friend has to be done on a
prototype class
Declaration of friend has tobeen done using keyword
friend
-
8/8/2019 friend,overload,template
9/24
ExampleExample below shows how class squarebelow shows how class square waswas
declareddeclared as aas a friend tofriend to classclass shapeshape classclass
ShapeShape
{
friend class Square;
..
public:
..};
class Square
{
private:.
public:
};
-
8/8/2019 friend,overload,template
10/24
OVERLOADED
METHOD
-
8/8/2019 friend,overload,template
11/24
Overload MethodAlso known as function polymorphism
The function will perform different operation
depends on argument list in the function call
a feature found in various programming
languages such as Ada, C#,VB .NET, C++, PHP, Dand Java
that allows the creation of several methods with
the same name which differ from each other in
terms of the
type of the input and the type of the output of
the function.
-
8/8/2019 friend,overload,template
12/24
Overloaded method based on program
situationpublic class AddingNumbers
{
public int Add(int a, int b)
{
return a+b;
}public int Add(int a, int b, int c)
{
return a+b+c;
}
}
Calling Overloaded Methods?
public int add(int x, int y)
public int add(int x, int y, int z)
-
8/8/2019 friend,overload,template
13/24
OVERLOADED
OPERATOR
-
8/8/2019 friend,overload,template
14/24
Operator overloading
In computer programming, operator overloading
(less commonly known as operator ad-hoc
polymorphism) is a specific case of polymorphism
in which some or all of operators like +, =, or ==have different implementations depending on the
types of their arguments.
Sometimes the overloading's are defined by the
language; sometimes the programmer can
implement support for new types.
-
8/8/2019 friend,overload,template
15/24
Operator overloading is claimed to be
useful because it allows the developer to
program using notation "closer to the
target domain"
allows user-defined types a similar level of
syntactic support as types built into the
language.
It can easily be emulated using function
calls;
-
8/8/2019 friend,overload,template
16/24
In a language that supports operator
overloading,
assuming the '* operator has higherprecedence than '+', this is effectively
a more concise way of writing:
add (a, multiply (b,c))
-
8/8/2019 friend,overload,template
17/24
Example:Time operator+(const Time& lhs, const Time& rhs) {
Time temp = lhs;temp.seconds += rhs.seconds;
if (temp.seconds >= 60) {
temp.seconds -= 60;
temp.minutes++;
}temp.minutes += rhs.minutes;
if (temp.minutes >= 60) {
temp.minutes -= 60;
temp.hours++;
}
temp.hours += rhs.hours;
return temp;
}
-
8/8/2019 friend,overload,template
18/24
Addition is a binary operation,
which means it has left and right
operands.
In C++, the arguments beingpassed are the operands, and the
temp object
is the returned value.
-
8/8/2019 friend,overload,template
19/24
Note that a unary operator defined as a class
method would receive no apparent argument (it
only works from this):
bool Time::operator!() const{
return ((hours == 0) && (minutes == 0)
&& (seconds == 0));
}
-
8/8/2019 friend,overload,template
20/24
TEMPLATES
-
8/8/2019 friend,overload,template
21/24
Define
Templates are a feature of the C++programming language that allowfunctions and classes to operate with
generic types. This allows a function or class to work on
many different data types without beingrewritten for each one.
-
8/8/2019 friend,overload,template
22/24
Declare Function Templates
With Parameter Format for declaring function templates with typeparameters is:
template function_declaration;
template function_declaration;
To use this function template we use the following
format for the function call:
function_name (parameters);
-
8/8/2019 friend,overload,template
23/24
Program using templates//function template
#include
using namespace std;
template
number integer ( number a)
{
number display;
display = a;
return (display);}
void main()
{
int a=10, c;
int w=5, y;
c=integer(a);y=integer(w);
cout
-
8/8/2019 friend,overload,template
24/24