Hi there,
I have done a small exercise (ex 4.17 on C++ how to program - deitel and deitel) which basically is about writing a program that uses a while statement to determine and print the largest number of 10 numbers input by the user. The program should use 3 variables, counter, number and largest.
So, here is the program:
header file:
//exercise_4-17.h file, holding the class definition
class LargestNumber
{
public:
void processNumbers( ) ; //processes the numbers
};
cpp file:
//exercise_4-17.cpp file, holding the functions definitions
#include <iostream>
using namespace std ;
#include "exercise_4-17.h"
void LargestNumber::processNumbers( )
{
int counter = 1 ;
int largest = 0 ;
int number ;
while( counter <= 10 )
{
cout << "Insert number: " ;
cin >> number ;
if( number > largest )
{
largest = number ;
cout << "The largest number so far is: " << largest << endl ;
}
else
{
cout << "The largest number so far is: " << largest << endl ;
}
counter++ ;
}
}
and main file:
//exercise_4-17_main.cpp holds the main file
#include "exercise_4-17.h"
int main( )
{
LargestNumber numbers; //create LargestNumber object
numbers.processNumbers();
return 0;
}
Now, this exercise was rather easy, so I was wondering whether it would be good practice to like use private members in the class: say, I could declare the variable in the class and make them private and then access them via a getter and change them with a setter. WOuld that have any advantage, or is it an unnecessary complication?
thanks