0% found this document useful (0 votes)
39 views

Abstraction

1. Abstract classes allow for abstraction in C++ by hiding internal details and exposing only functionality through pure virtual functions that derived classes must implement. 2. An example shows an abstract Shape class with a pure virtual draw() function. Rectangle and Circle classes inherit from Shape and provide their own draw() implementations. 3. Data abstraction in C++ can be achieved through classes using private/public access specifiers or through header files that hide implementation details from users while exposing an interface.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Abstraction

1. Abstract classes allow for abstraction in C++ by hiding internal details and exposing only functionality through pure virtual functions that derived classes must implement. 2. An example shows an abstract Shape class with a pure virtual draw() function. Rectangle and Circle classes inherit from Shape and provide their own draw() implementations. 3. Data abstraction in C++ can be achieved through classes using private/public access specifiers or through header files that hide implementation details from users while exposing an interface.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Interface IT Solutions : 9503105630

Interfaces in C++ (Abstract Classes)


Abstract classes are the way to achieve abstraction in C++. Abstraction in C++ is the
process to hide the internal details and showing functionality only. Abstraction can be
achieved by two ways:

1. Abstract class
2. Interface

Abstract class and interface both can have abstract methods which are necessary for
abstraction.

C++ Abstract class


In C++ class is made abstract by declaring at least one of its functions as
<>strong>pure virtual function. A pure virtual function is specified by placing "= 0" in
its declaration. Its implementation must be provided by derived classes.

Let's see an example of abstract class in C++ which has one abstract method draw().
Its implementation is provided by derived classes: Rectangle and Circle. Both classes
have different implementation.

38.5M
680
Prime Ministers of India | List of Prime Minister of India (1947-2020)

1. #include <iostream>
2. using namespace std;
3. class Shape
4. {
5. public:
6. virtual void draw()=0;
7. };
8. class Rectangle : Shape
9. {
10. public:

"Programming isn't about what you know; it's about what you can figure out.”
Data source :@ javatpoint
Interface IT Solutions : 9503105630

11. void draw()


12. {
13. cout < <"drawing rectangle..." < <endl;
14. }
15. };
16. class Circle : Shape
17. {
18. public:
19. void draw()
20. {
21. cout <<"drawing circle..." < <endl;
22. }
23. };
24. int main( ) {
25. Rectangle rec;
26. Circle cir;
27. rec.draw();
28. cir.draw();
29. return 0;
30. }

Output:

drawing rectangle...
drawing circle...

"Programming isn't about what you know; it's about what you can figure out.”
Data source :@ javatpoint
Interface IT Solutions : 9503105630

Data Abstraction in C++


o Data Abstraction is a process of providing only the essential details to the
outside world and hiding the internal details, i.e., representing only the essential
details in the program.
o Data Abstraction is a programming technique that depends on the seperation
of the interface and implementation details of the program.
o Let's take a real life example of AC, which can be turned ON or OFF, change the
temperature, change the mode, and other external components such as fan,
swing. But, we don't know the internal details of the AC, i.e., how it works
internally. Thus, we can say that AC seperates the implementation details from
the external interface.
o C++ provides a great level of abstraction. For example, pow() function is used
to calculate the power of a number without knowing the algorithm the function
follows.

In C++ program if we implement class with private and public members then it is an
example of data abstraction.

Data Abstraction can be achieved in two ways:

o Abstraction using classes


o Abstraction in header files.

Abstraction using classes: An abstraction can be achieved using classes. A class is


used to group all the data members and member functions into a single unit by using
the access specifiers. A class has the responsibility to determine which data member is
to be visible outside and which is not.

Abstraction in header files: An another type of abstraction is header file. For example,
pow() function available is used to calculate the power of a number without actually

"Programming isn't about what you know; it's about what you can figure out.”
Data source :@ javatpoint
Interface IT Solutions : 9503105630

knowing which algorithm function uses to calculate the power. Thus, we can say that
header files hides all the implementation details from the user.

38.5M
680
Prime Ministers of India | List of Prime Minister of India (1947-2020)

Access Specifiers Implement Abstraction:

o Public specifier: When the members are declared as public, members can be
accessed anywhere from the program.
o Private specifier: When the members are declared as private, members can
only be accessed only by the member functions of the class.

Let's see a simple example of abstraction in header files.

// program to calculate the power of a number.

1. #include <iostream>
2. #include<math.h>
3. using namespace std;
4. int main()
5. {
6. int n = 4;
7. int power = 3;
8. int result = pow(n,power); // pow(n,power) is the power function
9. std::cout << "Cube of n is : " <<result<< std::endl;
10. return 0;
11. }

Output:

Cube of n is : 64

In the above example, pow() function is used to calculate 4 raised to the power 3. The
pow() function is present in the math.h header file in which all the implementation
details of the pow() function is hidden.

Let's see a simple example of data abstraction using classes.

1. #include <iostream>
2. using namespace std;

"Programming isn't about what you know; it's about what you can figure out.”
Data source :@ javatpoint
Interface IT Solutions : 9503105630

3. class Sum
4. {
5. private: int x, y, z; // private variables
6. public:
7. void add()
8. {
9. cout<<"Enter two numbers: ";
10. cin>>x>>y;
11. z= x+y;
12. cout<<"Sum of two number is: "<<z<<endl;
13. }
14. };
15. int main()
16. {
17. Sum sm;
18. sm.add();
19. return 0;
20. }

Output:

Enter two numbers:


3
6
Sum of two number is: 9

In the above example, abstraction is achieved using classes. A class 'Sum' contains the
private members x, y and z are only accessible by the member functions of the class.

Advantages Of Abstraction:
o Implementation details of the class are protected from the inadvertent user
level errors.
o A programmer does not need to write the low level code.
o Data Abstraction avoids the code duplication, i.e., programmer does not have
to undergo the same tasks every time to perform the similar operation.
o The main aim of the data abstraction is to reuse the code and the proper
partitioning of the code across the classes.
o Internal implementation can be changed without affecting the user level code.

"Programming isn't about what you know; it's about what you can figure out.”
Data source :@ javatpoint

You might also like