Skip to content

Instantly share code, notes, and snippets.

@dibakarsutradhar
Last active September 3, 2018 08:01
Show Gist options
  • Save dibakarsutradhar/f8fb899c1150d64c17188abccaa5e386 to your computer and use it in GitHub Desktop.
Save dibakarsutradhar/f8fb899c1150d64c17188abccaa5e386 to your computer and use it in GitHub Desktop.
// c++ oop private access modifier
#include <iostream>
using namespace std;
// defining a class
class Circle {
private: // we defined that this class data members will be private
double radius;
public: // and we kept the member function public
double compute_area(double r){
radius = r; // member function can access private data member radius
double area = 3.14*radius*radius;
cout << "Radius is: " << radius << endl;
cout << "Area is: " << area;
}
}; // always put semicolons after defining a class
//main function
int main(){
Circle ball; // create a object called ball
ball.compute_area(3.5); // trying to access private data member radius directly from outside the class
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment