CREATE CLASSES AND FUNCTION AS A FRIEND a. Create a method or function of nonmember as a friends. b. Create classes as a friend .

which we want a class or function wants to access the private data from the class .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.

the reserved word friend precedes the function prototype in the class definition. . ‡ A friend function of a class is a member function of the class. ‡ The word friends appear only the function prototype in the class. A method or function of nonmember as a friends.A. but has access to all the member of the class. ‡ The function want to being a friend to a class.

.Consider the following statement: Class class_IllusFriends { Friend void two (/*parameter*/) ... . . . }.

A function as a friend ‡By placing the keyword friend in front of the function s name. . the function can achieve private member of the class.B.

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 to been done using keyword friend .

public: }.. public: . }. .Example below shows how class square was declared as a friend to class shape class Shape { friend class Square. class Square { private: . ..


D and 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. C#. .NET.Overload Method ‡ Also 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 . PHP.

int y. } public int Add(int a. int c) { return a+b+c.Overloaded method based on program situation public class AddingNumbers { public int Add(int a. int z) . int y) public int add(int x. int b. } } Calling Overloaded Methods? public int add(int x. int b) { return a+b.


operator overloading (less commonly known as operator ad-hoc polymorphism) is a specific case of polymorphism in which some or all of operators like +.Operator overloading ‡ In computer programming. or == have different implementations depending on the types of their arguments. =. sometimes the programmer can implement support for new types. ‡ Sometimes the overloading's are defined by the language. .

‡ 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. .

c)) . this is effectively a more concise way of writing: add (a. ‡ assuming the '* operator has higher precedence than '+'. multiply (b.‡ In a language that supports operator overloading.

seconds += rhs. temp.hours. } temp. if (temp.Example: Time operator+(const Time& lhs.hours += rhs. const Time& rhs) { Time temp = lhs. temp.hours++.seconds -= 60. } .minutes -= 60.minutes += rhs.minutes++. return temp.minutes.seconds >= 60) { temp.minutes >= 60) { temp. temp.seconds. if (temp. } temp.

the arguments being passed are the operands. ‡ In C++. and the temp object ‡ is the returned value. which means it has left and right operands.‡ Addition is a binary operation. .

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)). } .


.Define ‡ Templates are a feature of the C++ programming language that allow functions and classes to operate with generic types. ‡ This allows a function or class to work on many different data types without being rewritten for each one.

‡ To use this function template we use the following format for the function call: function_name <type> (parameters).Declare Function Templates With Parameter ‡ Format for declaring function templates with type parameters is: template <class identifier> function_declaration. . template <typename identifier> function_declaration.

template <class number> number integer ( number a) { number display. y=integer(w). } . c. c=integer(a). } void main() { int a=10. y. return (display). cout<< c << endl. cout<< y << endl. display = a. int w=5.Program using templates //function template #include<iostream> using namespace std.

Sign up to vote on this title
UsefulNot useful