Program To Demonstrate Data Hiding , Encapsulation And Abstraction Using C++

LEVEL: Beginner

Whats in this post?

  1. Code
  2. Output
  3. Background and Explanation of the code


To copy code: Click View Source “View Source” on the right of this code snippet.

Background and Explanation of the code

The above program calculates the roots of the quadratic equations.The data members a, b, c  are hidden from main ( ) by declaring them private in the class quad_equ to prevent them from any accidental modification outside this class.User of this class needs to input values for coefficients of quadratic equation a, b, c and calculate their roots.To accomplish this, public member functions read ( ) and cal_roots ( ) are made that can be accessed from main ( ). Thus, data members (a,b,c) and its associated member functions ( read ( ) and cal_roots ( )) are encapsulated in a class quad_equ and hence provide controlled access to its data members from main ( ). The members functions are declared inside the class but defined outside so as to separate the interface from its actual implementation.Thus , it provide true abstraction.On executing the program, we get the required output.


Enter coffiecients of Quad.Equ  a, b, c =  1  6  5

Roots are real and unequal

x1 =  -1

x2 =  -5