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 thisX
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 <iostream>
#include <cmath>
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 << endl;
cout << "Please enter an integer: ";
cin >> lNum_1;
cout << "Please enter an integer: ";
cin >> lNum_2;
cout << "Please enter an integer: ";
cin >> lNum_3;
cout << "Please enter an integer: ";
cin >> lNum_4;
testObject.setNum_1(lNum_1);
testObject.setNum_2(lNum_2);
testObject.setNum_3(lNum_3);
testObject.setNum_4(lNum_4);
cout << endl;
cout << "The 1st number returned is: " << testObject.getNum_1() << endl;
cout << "The 2nd number returned is: " << testObject.getNum_2(lNum_1) << endl;
cout << "The 3rd number returned is: " << testObject.getNum_3(lNum_1, lNum_2) << endl;
cout << "The 4th number returned is: " << testObject.getNum_4(lNum_1, lNum_2, lNum_3) << endl;
cout << 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!).