Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ Class Help

Name: Anonymous 2009-03-30 22:42

Halp /prog/
I'm supposed to make a program that asks how many integers to sums up, then using a for loop, allows the user to input the integers. I posted earlier about it, but now I'm trying to use a class, and failing horribly. This is my first programming language class, so if you could provide as many tips as possible that would be appreciated (other than the obvious 'GTFO' or 'change your major lol randumb xD')


//////////////////////////////////////////////////////////////////
//    Lab 5 - For loop Sum
//    Header.h
//////////////////////////////////////////////////////////////////

#include <iostream>
using namespace std;

class Sum
{
    public:
        Sum();    //constuctor
        void calcSum(int);    //adds to the total
        int setSum();    //displays total
    private:
        int m_total;
}

Sum::Sum ()
{
    calcSum(m_total);
    setSum();
}

void Sum::calcSum (int a_temp)
{
    int m_total = m_total + a_temp;
}

int Sum::setSum (void)
{
    return m_total;
}


//////////////////////////////////////////////////////////////////
//    Lab 5 - For loop Sum
//    Test.cpp
//////////////////////////////////////////////////////////////////

#include <iostream>
using namespace std;

#include "Header.h"

int main()
{
    int x = 0;    // x = the number of times the for loop will run
    int i = 0;
    int num = 0;
    Sum total;

    cout << "Number of integers to sum: ";
    cin >> x;

    for(i = 0; i >= x; i++)
    {
        cout << "Enter number " << (i+1) << ": ";
        cin >> num;
        cout << endl;
        total.calcSum(num);
    }

    cout << "\n\nTotal sum: " << total.setSum() << endl;

    return 0;
}

Name: Anonymous 2009-03-31 15:46

>>19
you should set m_total to 0 in the constructor
No, you should set it to 0 in the initializer list.

>>16
And we have to combine the Header and the Definitions due to the professors preference.
Your professor is an idiot if he's suggesting that. It works fine for this example, but is a terrible practice in The Real Worldtm.

Is there any significant difference in having them combined/not?
Yes. Assume you wanted to use your useless Sum class in another class's implementation (ie, within another *.cpp file). You'd include the sum.h header in the second implementation file, naturally.

And the linker would shit down your throat for breakfast.

The reason is that #include (and headers in general) are simply preprocessing sugar. The contents of the header files are just dumped verbatim into wherever. So if you put definitions into a header file, then include that header file in multiple compilation units, each compilation unit will have a copy of the same definition. The linker won't know which one to use when it puts everything together, and will barf.

So please, for the sake of the linker's stomache keep all your un-inlined function definitions out of the header files. Thanks.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List