fork(1) download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6.  
  7. class Circle {
  8.  
  9. // private data member
  10.  
  11. private:
  12.  
  13. double radius;
  14.  
  15.  
  16.  
  17. // public member function
  18.  
  19. public:
  20.  
  21. void compute_area(double r)
  22.  
  23. {
  24.  
  25. // member function can access private
  26.  
  27. // data member radius
  28.  
  29. radius = r;
  30.  
  31.  
  32.  
  33. double area = 3.14 * radius * radius;
  34.  
  35.  
  36.  
  37. cout << "Radius is: " << radius << endl;
  38.  
  39. cout << "Area is: " << area;
  40.  
  41. }
  42. };
  43.  
  44.  
  45. // main function
  46.  
  47. int main()
  48. {
  49.  
  50. // creating object of the class
  51.  
  52. Circle obj;
  53.  
  54.  
  55.  
  56. // trying to access private data member
  57.  
  58. // directly outside the class
  59.  
  60. obj.compute_area(1.5);
  61.  
  62.  
  63.  
  64. return 0;
  65. }
Success #stdin #stdout 0.01s 5396KB
stdin
Standard input is empty
stdout
Radius is: 1.5
Area is: 7.065