C++ Abstraction


Hiding the details of the object and showing the functionality is called as an abstraction.


In C++ data abstraction refers to providing only essential information to the outside world and hiding the internal details.


Example: Phone call, we do not know the internal processing.

c++ abstraction

Advantages of Data Abstraction

  • Implementation details of the class are protected from the unexpected user-level errors, which corrupt the state of the object.
  • It helps to avoids the code duplication.
  • It helps to reuse the code and provide partitioning of the code across the classes.

Abstract Class

A class which consists atleast one pure virtual function is called an abstract class.

A Pure Virtual Function can be declared by equating it to zero.

An abstract class is used as a base class for the deriving purpose only. An abstract class can't create an object.


Example:

#include <iostream>     
using namespace std;  

 class A   
{    
    public:   
    virtual void show()=0;    
};    
 class B : A    
{    
    public:  
     void show()    
    {    
        cout << "Derived Class B" <<endl;    
    }    
};    
class C : A    
{    
    public:  
     void show()    
    {    
        cout << "Derived Class C" <<endl;    
    }    
};    
int main( ) {  
    B b;  
    C c;  
    b.show();
    c.show();
    return 0;  
}



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.