Thread: help with getters and setters in classes.

  1. #1
    Registered User
    Join Date
    May 2020

    help with getters and setters in classes.

    I'm using object orientating to make an audio plug in. This reads in the audio file as an array. I'm trying to display the mean of the array to the console window using getters and setters.

    I have a Mean function in my class.cpp file which is:

    double AudioFile::Mean()
        if (inputSIG != NULL)
            double sum = 0, mean;
            for (int i = 0; i < numSamples; i++)
                //sum = last values sum + next value in array
                sum = sum + inputSIG[i];
            mean = sum / (float)numSamples;
            return mean;

    I have mean established in my private variables in the class header file.

     double mean;

    and it's set to 0 (so its empty and can be filled in when the array in read in with the sound file) in the class source file like this:

    How should I go about displaying the calculated mean? How would I apply getters and setters?
    I think it would be something like this:
    //in header file
    double getmean() {return mean;}
    void setmean (mean) {mean}
    //in main
    cout << audio.getmean << endl;

    I hope this makes sense. I'm basically calculating the mean within a function in a class and trying to get that mean/set an empty mean variable to be that value so I can print it out.
    Thanks for any help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    What's the difference between
    double AudioFile::Mean()
    double AudioFile::getmean()

    > double sum = 0, mean;
    Your local variable 'mean' is different to the class member variable with the same name.

    TBH, having to do this seems a long-winded way of going about it.
    double temp = myAudioThing.Mean();
    cout << myAudioThing.getmean() << endl;
    Your 'Mean' function already calculates and returns the result, so just go with that.
    cout << myAudioThing.Mean() << endl;
    But I'd give it a better name like say CalculateMean.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2020
    Thank you very much!!
    I understand now.
    You are a hero.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Style of getters
    By threahdead in forum C++ Programming
    Replies: 8
    Last Post: 04-28-2011, 12:02 PM
  2. Conversions between base classes and derived classes
    By tharnier in forum C++ Programming
    Replies: 14
    Last Post: 03-18-2011, 10:50 AM
  3. Classes access other classes local variables
    By parad0x13 in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2010, 04:36 AM
  4. Classes with Other Classes as Member Data
    By njd in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2005, 09:30 AM
  5. Help accessing classes and derived classes
    By hobbes67 in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 02:46 PM

Tags for this Thread