Software & Finance

C++ - Abstraction

Abstraction is an important concept in C++. If you are developing the class library that can be used by N number of peoples, then you may not want to expose all the properties and implementation details of the class. Exposing the methods what is mandatory and hiding the rest.

Here is one example for abstraction which can take input and produces the result. The purpose of this class this finding the standard deviation for the given series and nothing else. Do not argue here that we must expose mean and variance as this is also an useful feature as a part of class implementation.

If we need an abstraction concept explained in C++ code that compiles and gives the output, then I have to take the complex real time example. Otherwise I have to take an example that can not expressed and written in C++ like how a car driver does not to know how the engine and internal part works.

I have written a class call StdDeviation and it exposes only two methods SetValues() and GetStandardDeviation(). It hides the implementation for how standard deviation is calculated. The implementation details here are calculating the mean and variance and they are not exposed to the user.

Source Code

#include <stdio.h>

#include <iostream>

#include <tchar.h>

#include <math.h>


class StdDeviation



    int max;

    int value[100];


    double mean;


    double CalculateMean()


        double sum = 0;

        for(int i = 0; i < max; i++)

        sum += value[i];

        return (sum / max);



    double CalculateVariane()


        mean = CalculateMean();

        double temp = 0;

        for(int i = 0; i < max; i++)


            temp += (value[i] - mean) * (value[i] - mean) ;


        return temp / max;





    int SetValues(int *p, int count)


        if(count > 100)

            return -1;

        max = count;

        for(int i = 0; i < count; i++)

        value[i] = p[i];

        return 0;



    double GetStandardDeviation()


        return sqrt(CalculateVariane());





int _tmain(int argc, _TCHAR* argv[])


    int arrSeries[] = { 10, 20, 20, 30, 30, 30, 40, 50 };

    StdDeviation sd;

    sd.SetValues(arrSeries, sizeof(arrSeries) / sizeof(arrSeries[0]));

    double v = sd.GetStandardDeviation();


    std::cout << "Population Standard Deviation is: " << v << "\n\n";



Population Standard Deviation is: 11.6592