Skip to content

Instantly share code, notes, and snippets.

@dibakarsutradhar
Created September 3, 2018 06:39
Show Gist options
  • Save dibakarsutradhar/a3841363819678882b84db032fb010fb to your computer and use it in GitHub Desktop.
Save dibakarsutradhar/a3841363819678882b84db032fb010fb to your computer and use it in GitHub Desktop.
It's a wrong code
// 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(){
return 3.14*radius*radius; // member function can access private data member radius
}
}; // always put semicolons after defining a class
//main function
int main(){
Circle ball; // create a object called ball
ball.radius = 3.5; // trying to access private data member radius directly from outside the class (It's PROHIBETED)
cout << "Radius is: " << ball.radius << endl;
cout << "Area is: " << ball.compute_area();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment