Abstraction in C++ | Advantages of data abstraction in C++ | Example of C++ abstraction | C++ Abstraction | My CS Tutorial
Data Abstraction is a act to represent the essential feature of an object without including background and explanation. The Data Abstraction is a feature to provide the access of member data without show the hidden data. classes use the concept of abstraction and defined as abstract attributes(Data) such as size, weight and cost etc, and function to operate on these attributes are called member data and function to operate these data are called Member function. Classes used the concept of Abstraction.
The major advantage of using this feature is that when the code evolves and you need to make some adjustments in the code then you only need to modify the high level class where you have declared the members as private.
Since none class is accessing these data members directly, you do not need to change the low level(user level) class code.
Imagine if you had made these data members public, if at some point you want to change the code, you would have to make the necessary adjustments to all the classes that are accessing the members directly.
Helps the user to avoid writing the low level code
Avoids code duplication and increases reusability.
Can change internal implementation of class independently without affecting the user.
Helps to increase security of an application or program as only important details are provided to the user.
In C++, we use access labels to define the abstract interface to the class. A class may contain zero or more access labels −
Members defined with a public label are accessible to all parts of the program. The data-abstraction view of a type is defined by its public members.
Members defined with a private label are not accessible to code that uses the class. The private sections hide the implementation from code that uses the type.
There are no restrictions on how often an access label may appear. Each access label specifies the access level of the succeeding member definitions. The specified access level remains in effect until the next access label is encountered or the closing right brace of the class body is seen.
#include <iostream>
using namespace std;
class implementAbstraction
{
private:
int a, b;
public:
// method to set values of
// private members
void set(int x, int y)
{
a = x;
b = y;
}
void display()
{
cout<<"a = " <<a << endl;
cout<<"b = " << b << endl;
}
};
int main()
{
implementAbstraction obj;
obj.set(10, 20);
obj.display();
return 0;
}
a = 10
b = 20
You can see in the above program we are not allowed to access the variables a and b directly, however one can call the function set() to set the values in a and b and the function display() to display the values of a and b.
Data abstraction in C++ | My CS Tutorial |
Data Abstraction in C++
Data Abstraction is a act to represent the essential feature of an object without including background and explanation. The Data Abstraction is a feature to provide the access of member data without show the hidden data. classes use the concept of abstraction and defined as abstract attributes(Data) such as size, weight and cost etc, and function to operate on these attributes are called member data and function to operate these data are called Member function. Classes used the concept of Abstraction.
Advantage of data abstraction
The major advantage of using this feature is that when the code evolves and you need to make some adjustments in the code then you only need to modify the high level class where you have declared the members as private.
Since none class is accessing these data members directly, you do not need to change the low level(user level) class code.
Imagine if you had made these data members public, if at some point you want to change the code, you would have to make the necessary adjustments to all the classes that are accessing the members directly.
Helps the user to avoid writing the low level code
Avoids code duplication and increases reusability.
Can change internal implementation of class independently without affecting the user.
Helps to increase security of an application or program as only important details are provided to the user.
Access Labels Enforce Abstraction
In C++, we use access labels to define the abstract interface to the class. A class may contain zero or more access labels −
Members defined with a public label are accessible to all parts of the program. The data-abstraction view of a type is defined by its public members.
Members defined with a private label are not accessible to code that uses the class. The private sections hide the implementation from code that uses the type.
There are no restrictions on how often an access label may appear. Each access label specifies the access level of the succeeding member definitions. The specified access level remains in effect until the next access label is encountered or the closing right brace of the class body is seen.
#include <iostream>
using namespace std;
class implementAbstraction
{
private:
int a, b;
public:
// method to set values of
// private members
void set(int x, int y)
{
a = x;
b = y;
}
void display()
{
cout<<"a = " <<a << endl;
cout<<"b = " << b << endl;
}
};
int main()
{
implementAbstraction obj;
obj.set(10, 20);
obj.display();
return 0;
}
Output:
a = 10
b = 20
You can see in the above program we are not allowed to access the variables a and b directly, however one can call the function set() to set the values in a and b and the function display() to display the values of a and b.
Abstraction in C++ | Advantages of data abstraction in C++ | Example of C++ abstraction | C++ Abstraction | My CS Tutorial
_______________________________________
_______________________________________
Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this tutorial or program or want to give any suggestion send email on hc78326@gmail.com
Created by-- HARSH CHAUHAN
No comments:
Post a Comment