Thread: help with getters and setters in classes.

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    4

    help with getters and setters in classes.

    Hi,
    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:

    Code:
    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.

    Code:
    private:
     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:
    Code:
    AudioFile::AudioFile()
    {
    mean=0;
    }
    

    How should I go about displaying the calculated mean? How would I apply getters and setters?
    I think it would be something like this:
    Code:
    //in header file
    double getmean() {return mean;}
    void setmean (mean) {mean}
    //in main
    audio.Mean();
    audio.setmean(mean);
    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
    Location
    The edge of the known universe
    Posts
    39,659
    What's the difference between
    double AudioFile::Mean()
    and
    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.
    Code:
    double temp = myAudioThing.Mean();
    myAudioThing.setmean(temp);
    cout << myAudioThing.getmean() << endl;
    Your 'Mean' function already calculates and returns the result, so just go with that.
    Code:
    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
    Posts
    4
    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