Last active
September 3, 2018 08:01
-
-
Save dibakarsutradhar/f8fb899c1150d64c17188abccaa5e386 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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