Creating C++ classes and objects I'm brand new to programming (in general) and C++ (in particular).  I'm learning the basics of classes and objects.

I'm trying to do the following:  


Create a class called X.  It has 4 member functions, with 0 arguments, 1 argument, 2 arguments and 3 arguments, respectively.
In main(), make an object of this X class that calls each of these 4 member functions.
Modify the class so that it has a single member function with all of the arguments defaulted.




#include "stdafx.h"
#include &lt;iostream&gt;
#include &lt;cmath&gt;
using namespace std;

class X
{
public:

    void setNum_1(int);
    void setNum_2(int);
    void setNum_3(int);
    void setNum_4(int);

    double getNum_1();
    double getNum_2(int num_1);
    double getNum_3(int num_1, int num_2);
    double getNum_4(int num_1, int num_2, int num_3);

private:
    int num_1;
    int num_2;
    int num_3;
    int num_4;
};

int _tmain(int argc, _TCHAR* argv[])
{
    X testObject;

    int lNum_1 = 0;
    int lNum_2 = 0;
    int lNum_3 = 0;
    int lNum_4 = 0;

    cout &lt;&lt; endl;
    cout &lt;&lt; "Please enter an integer: ";
    cin &gt;&gt; lNum_1;

    cout &lt;&lt; "Please enter an integer: ";
    cin &gt;&gt; lNum_2;

    cout &lt;&lt; "Please enter an integer: ";
    cin &gt;&gt; lNum_3;

    cout &lt;&lt; "Please enter an integer: ";
    cin &gt;&gt; lNum_4;

    testObject.setNum_1(lNum_1);
    testObject.setNum_2(lNum_2);
    testObject.setNum_3(lNum_3);
    testObject.setNum_4(lNum_4);

    cout &lt;&lt; endl;
    cout &lt;&lt; "The 1st number returned is: " &lt;&lt; testObject.getNum_1() &lt;&lt; endl;
    cout &lt;&lt; "The 2nd number returned is: " &lt;&lt; testObject.getNum_2(lNum_1) &lt;&lt; endl;
    cout &lt;&lt; "The 3rd number returned is: " &lt;&lt; testObject.getNum_3(lNum_1, lNum_2) &lt;&lt; endl;
    cout &lt;&lt; "The 4th number returned is: " &lt;&lt; testObject.getNum_4(lNum_1, lNum_2, lNum_3) &lt;&lt; endl;
    cout &lt;&lt; endl;

    return 0;
}

void X::setNum_1(int n_1)
{
    num_1 = n_1;
}

void X::setNum_2(int n_2)
{
    num_2 = n_2;
}

void X::setNum_3(int n_3)
{
    num_3 = n_3;
}

void X::setNum_4(int n_4)
{
    num_4 = n_4;
}

double X::getNum_1()
{
    return num_1;
}

double X::getNum_2(int num_1)
{
    return num_1 + num_1;
}

double X::getNum_3(int num_1, int num_2)
{
    return num_1 + num_2;
}

double X::getNum_4(int num_1, int num_2, int num_3)
{
    return num_1 + num_2 + num_3;
}


I was wondering if you can recommend some ways that I can improve this code (or let me know if I've totally botched something, which is likely the case!).
First of all, you should avoid "getters" and "setters" whenever possible.  This is because they can be bad for encapsulation as they reveal the internal members.  This would be an ideal example if you were merely trying to understand getters and setters, but it's still not the best approach to learning how classes should work, especially when you start using them for more complex programs.

You should become aware of object construction.  That is, when you first create an object to hold some data at the start, it should be given to that object upon construction.

Instead of using setters, you should construct the object after getting the input:

X testObject(lNum_1, lNum_2, lNum_3, lNum_4);


This will construct the object right away with the initial values.  However, you'll first need to provide a constructor that will handle this.

You can use an initializer list:

X::X(int num_1, int num_2, int num_3, int num_4)
    : num_1(num_1)
    , num_2(num_2)
    , num_3(num_3)
    , num_4(num_4)
{}


As for the getter, it would only be useful if you need the value.  If you just need to display the members, then you can create a display() function:

void X::display() const
{
    std::cout &lt;&lt; "num_1: " &lt;&lt; num_1 &lt;&lt; "\n";
    std::cout &lt;&lt; "num_2: " &lt;&lt; num_2 &lt;&lt; "\n";
    std::cout &lt;&lt; "num_3: " &lt;&lt; num_3 &lt;&lt; "\n";
    std::cout &lt;&lt; "num_4: " &lt;&lt; num_4 &lt;&lt; "\n";
}


(Notice that I've made this function const.  Since it only displays data members and doesn't modify them, adding this keyword will prevent any accidental modifications.)

It would then be called by itself, rather than calling the getter for each member:

testObject.display();


There's much that can be said about all of this, but I've given you a basic idea on where to start.

Miscellaneous:


Try not to get into the habit of using using namespace std.  If you start writing larger programs, you may run into name-clashing issues if this is misused.  More info about that here.
Since you're not using &lt;cmath&gt;, it should just be removed.
_tmain() should just be main(); the former does not exist in C++.
You could leave out the command line parameters if you're not going to use them.

Otherwise, you could certainly replace your input with command line arguments:

./program_name 1 2 3 4


If you do this, you should also be sure to check the value of argc so that the program won't try to run with too few arguments:

if (argc &lt; 5)
{
    std::cerr &lt;&lt; "You must input four numbers!";
    return EXIT_FAILURE;
}


The program name is counted as an argument, so it should be 5.