C++- 04
C++- 04
Friend function,
Inline function,
Files
VIRTUAL FUNCTION
When user use the same function name in both the base and derived classes, the
function in base class is declared as virtual using the keyword ‘virtual’ preceding its
normal declaration.
When a function is made virtual, C++ determines which function to use at runtime,
based on the type of object pointed to the base pointer.
Thus, by making the base pointer to point two different objects, it can execute
different versions of the virtual function.
Virtual functions can be accessed through the use of a pointer declared as a pointer
to the base class.
Also, the prototype of the base class version of a virtual function & all the derived
class versions must be identical.
If two functions with the same name have different prototype, C++ considers them
as overloaded functions, and the virtual function mechanism is ignored.
Virtual function Example
class base
{ int main()
public: {
virtual void show ( ) base *bp;
{ base b;
cout<<” Base class”; derived d;
} bp=&b;
}; bp->show();
class derived: public base
{ bp=&d;
public: bp->show();
void show( ) return 0;
{ }
cout<<” Derived class”;
}
};
RULES FOR VIRTUAL FUNCTIONS
1. The virtual functions must be members of some class.
2. They cannot be static members.
3. They are accessed by using object pointers.
4. A virtual function can be a friend of another class.
5. A virtual function in a base class must be defined, even though it is not used.
6. The prototypes of the base class version of virtual function & all derived class version must be identical. If
two functions have different prototypes, then C++ considers them as overloaded functions & not as virtual
functions.
7. We cannot have virtual constructors, but we can have virtual destructors.
8. A base pointer can point to any type of derived object, the reverse is not true. i.e. we cannot use a pointer
to derived class to access an object of the base type.
9. When base pointer is only relative to its base type.
10. Virtual functions are defined in base class; they need not be redefined in derived class.
STATIC AND DYNAMIC BINDING
• In static binding, object is bound to its function call at compile time. While
in dynamic binding selection of the appropriate function is done
dynamically at runtime.
• In static binding, compiler knows the function information (argument type,
number etc) at the compile time itself so as to select appropriate function
for a particular call (also called early binding).
• In dynamic binding, function is linked with a particular class much later
after the compilation (also known as late binding).
• Function overloading and operator overloading are the examples of static
binding. Virtual functions are used to implement dynamic binding.
WHAT IS FRIEND FUNCTION? CHARACTERISTICS OF
FRIEND FUNCTION
A non- member function that is allowed to access the private variable of a class is called a friend of
the class.
A function is made a friend of a class by the keyword “friend”.
Eg- class classname
{
private :
public :
friend return- type function name (arguments);
};
The characteristics of friend function are as follows.
• A friend function can access the private members of a class.
• The friend declaration is unaffected by its location in the class.
• The definition of a friend function does not require the name with the scope resolution operator
prefixed to it, as it is required for a class member function.
FRIEND FUNCTION EXAMPLE
#include<iostream.h>
#include<conio.h>
class A
{ private: int m,n;
public : void setvalue( )
{ m = 5, n= 6; }
friend void sum()
{ cout<< m+n; }
};
void main()
{
A ab;
ab.setvalue();
sum();
}
INLINE FUNCTION
1. When a function is called, a lot of time is spent in executing a series of instructions, for task
such as jumping to the function, saving registers and returning to the calling function.
2. C++ provides a solution of inline functions to this problem inline functions makes a program
run faster because the overhead of a function call and return is eliminated.
3. However, it makes program to take up more memory, because the statements that define inline
function are reproduced at each point where the function is called
4. “An inline function is a function that is expanded inline when it is invoked.”
i.e. the compiler replaces function call with the corresponding function code.
The inline function are defined (syntax) as follows:
inline function name
{ function body }
• It uses file streams as an interface between the programs and the files.
• The streams that supplies data to the program is known as input stream,
while the stream that receives data from the program is known as output
stream.
• In other words, input stream extracts (or reads) data from the file and the
output stream inserts (or writes) data to the file.
• The input operation involves the creation of an input stream and linking it
with the program & the input file.
• Similarly, the output operation involves the creation of an output stream
and linking it with the program & the output file
Input Stream
Read data Data Input
File pointers
Get pointer - Input pointer - Reading the content of given file location.
Put pointer - Output pointer - Writing the content to a given file location.