Classes with class keyword and Access Controls

The class keyword is similar to the struct keyword but by default it sets methods and attributes as private. In this snippet I explore the public and private attributes and methods.

For demonstration, this code snippet creates a simple class that calculates the Area and Circumference of a circle given a user provided radius. The pi value is private and can be initialized by the constructor. The radius is dynamic

#include <cstdio>

class ComputeCircularValues {

    float approx_pi = 3.14;

        // Constructor
        ComputeCircularValues(float pi_value){
            approx_pi = pi_value;            

        float Area(int radius) {
            return approx_pi * radius * radius;

        float Circumference(int radius){
            return 2 * approx_pi * radius; 

        float reset_pi_value(float pi_value){ 
            float approx_pi = pi_value;
            return approx_pi;

        float get_approx_pi(){
            return approx_pi;

int main() {

    // Using a constructor to assign private varibale pi for this instance.
    ComputeCircularValues compute_object(3.14);
    printf("The Area of a circle using Pi: %f and Radius %f is %f \n", 
    compute_object.get_approx_pi(), 5.0 , compute_object.Area(5));   

    ComputeCircularValues compute_object_2(3.1);
    printf("The Area of a circle using Pi: %f and Radius %f is %f \n" ,
    compute_object_2.get_approx_pi(), 5.0,  compute_object_2.Area(5));


The code above will use a constructor to define the approx_pi value from the user in every instance of the ComputeCircularValues class

Let's compile and execute the code.

$ g++ class_with_access_control.cpp -std=c++17 -o class_with_access_control
$ ./class_with_access_control
The Area of a circle using Pi: 3.140000 and Radius 5.000000 is 78.500000 
The Area of a circle using Pi: 3.100000 and Radius 5.000000 is 77.500000