Advertisement

Data abstraction in C++ - Code with abuzar


Through this article we are teaching about the Data abstraction in C++ 


https://abuzartec.blogspot.com/2021/04/data-abstraction-in-c.html

Data abstraction in C++ is one of the most important features in object oriented programming in C++ . Abstraction means displaying only essential information and hiding the personal information to the user . Data abstraction refers only to show relevant data and hide the unnecessary data or background detail to the user .

Consider a real life example for driving a car . The man only knows pressing accelerator for increasing the speed press break for stop or reduce the speed of the car and change the gear etc. but he doesn't know the inner mechanism of the car so this is the real life example of the Data abstraction .

Abstraction using classes :  Abstraction using classes in C++ . Can help us to group data member and data function . A class can decide which type of data show to the user and which type of data can be hide from the user .

  When you make a program for banking system where you can show relevant data and hide the unnecessary data or background details which is not useful for the customer , so here you can use the abstraction class 


Data Abstraction Example in C++ 


#include <iostream>
using namespace std;

class Adder {
   public:
      // constructor
      Adder(int i = 0) {
         total = i;
      }
      
      // interface to outside world
      void addNum(int number) {
         total += number;
      }
      
      // interface to outside world
      int getTotal() {
         return total;
      };
      
   private:
   // hidden data from outside world
      int total;
};

int main() {
   Adder a;
   
   a.addNum(10);
   a.addNum(20);
   a.addNum(30);

  cout<<"Total"<<a.getTotal()<<endl;
   return 0;
}

When the above code is compiled and executed the result is .

Output 

Total 60




Please! Write comment if you find anything incorrect .

This article is contributed by Mohd Abuzar . if  you like code with abuzar and would like to contribute you can also write an article or mail at abuzar.abuzar.mohd325@gmail.com .




























Post a Comment

0 Comments