Thursday, April 30, 2020

Encapsulation



Encapsulation is a mechanism that binds together code and the data it manipulates and keeps both safe from outside interference and misuse. In an object oriented language, code and data may be combine in such a way that itself contain box is created. In other words, an object is a device that supports encapsulation.

Within an object, code, data or both may be private to that object or may be public. Private code or data is known to an accessible only by another part of the object. That is private code or data may not be access by a piece of the program that exists outside the objects. When the code or data is public, other part of the programs may access it even though it is defined within an object. The public parts of an object are used to provide a control interface to the private elements of the object.
 
Virtual function:

When we use the same function name in both the base class and the derived class. The function in base class is declared as virtual using the keyword virtual preceding it normal declaration. When the function is made virtual, C++ determines which function to use at run time based on the type of object pointed to by the base pointer rather than type of the pointer. Thus by making the base pointer to point to different objects, we can execute the different version of the virtual function.

#include<iostream.h>

#include<conio.h>

class base

{

public:

void display( )

{
cout << “Base class display function \n”;
}
virtual void show(   )
{
cout << “base class show () function \n”;
}
};
class d:public base
{
public:
void display ( )
{
cout<< “ derived class display function \n”;
}
void show ( )
{
cout<< “ derived class show function \n”;
};
void main( )
{
base *b, obj;
d obj;
b=&obj;
b->display( );
b->show( );
b=&obj2;
b->display( );
b-> show( );
getch( );
}
 
 
Pure virtual function:
Virtual void diplay () =0;
Such functions are called pure virtual function. A pure virtual function is a function declared in a base class that has no definition relative to the base class. In such cases, the compiler requires each derived class to either define the function or re-declared it as a pure virtual function. A class containing pure virtual function cannot be used to declare any object of its own, such classes are called abstract base class.
The main objective of abstract base class is to provide some properties to the derived classes and to create a base pointer requires for achieving run time polymorphism.

#include<iostream.h>

#include<conio.h>

class base

{

public:

virtual void disp( )=0;
};
class d1:public base
{
public:
void display ( )
{
cout << “In the d1 class n1”;
}
};
class d2:public base
{
public:
void disp( )
{
cout << “In the class d2 \n”;
}
};
void main( )
{
base *p;
d1 obj1;
d2 obj2;
p=&obj1;
p->disp( );
p=&obj2;
p->disp( );
getch( );
}
  

Calling  a virtual function through a base class reference.
#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void vfun( )
{
cout<< “this is a base vfun ( ) \n”;
}
};
class d1: public base
{
public:
void vfunc( )
{
cout<< “ This is vfun( )\n”;
}
};
class d2: public base
{
public:
void vfun( );
{
cout<< ‘ In the d2 class \n”;
}
};
void f(base &r)
{
r.vfunc( );
}
void main( )
{
clrscr( );
base b;
d1.obj1;
d2.obj2;
f(b);
f(obj1);
f(obj2);
getch( );
}


Example of accessing the virtual function using the hierarchical inheritance
class base
{
public:
virtual void fun( )
{
cout<< “In the base class:”;
}
};
class d1:public base
{
public:
void vfun( )
{
cout<< “ In the d1  class”;
}
};
class d2: public base
{
public:
void vfun( )
{
cout<< “ In the d2 class”;
}
};
void main ( )
{
clrscr( );
base *ptr, b; //object
d1 obj1; //object
d2 obj2;
ptr=&b;
ptr->vfun( ); //calling the function
ptr=&obj1;
ptr->vfun( );
ptr=&obj2;
ptr->vfun( );
getch( );
}


Virtual function in multilevel inheritance
#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void fun( ) // member function
{
cout<< “In the base class:”;
}
};
class d1:public base    //public derivation
{
public:
void vfun( )
{
cout<< “ In the d1  class”;
}
};
class d2: public base
{
public:
void vfun( )
{
cout<< “ In the d2 class”;
}
};
void main ( )
{
clrscr( );
base *p, b; //object
d1 obj1; //object
d2 obj2;
p=&b; // point to base
p->vfun( ); //access base vfun( )calling the function
p=&obj1; // point to d1
p->vfun( ); // access d1 vfun( )
p=&obj2;
p->vfun( ); // access d2  vfun()
getch( );
}



Example of Virtual function inherited in hierarchical inheritance
#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void fun( ) // member function
{
cout<< “In the base class:”;
}
};
class d1:public base    //public derivation
{
public:
void vfun( )
{
cout<< “ In the d1  class”;
}
};
class d2: public base
{
public:
void vfun( )
{
cout<< “ In the d2 class”;
}
};
void main ( )
{
clrscr( );
base *p, b; //object
d1 obj1; //object
d2 obj2;
p=&b; // point to base
p->vfun( ); //access base vfun( )calling the function
p=&obj1; // point to d1
p->vfun( ); // access d1 vfun( )
p=&obj2;
p->vfun( ); // access d2  vfun()
getch( );
}


When a function is declared as ‘virtual’ by a base class, it may be over-ridden by a derived class. However, the function does not have to be over-ridden when a derived class fails to over rides a virtual function, then when an object of a derived class accesses that function, the function defined but her base class is used.