lwoodtri
asked on
C++ Class Validation
I am trying to figure out how to verify that the assignment of the character array and the ssn actually have something in it, but I am having difficulty in setting that up. The ssn is not supposed to be =< 0 and the name is not supposed to be blank.
How do I revert to the default constructor if the values entered ( programmed into ) since there is no input do not meet the required specs?
Thanks.
How do I revert to the default constructor if the values entered ( programmed into ) since there is no input do not meet the required specs?
Thanks.
#include <iostream>
#include <iomanip>
using namespace std;
class Student
{
// private member variables
private:
long socialSecurity;
char name[80];
// public variables / methods
public:
// default constructor
Student(char *n="unassigned",long sn=999999999);
// Name get / set
void setName(char *n);
char *Student::getName();
// Social Security set / get
void setSSN(long sn);
long getSSN();
};
// default constructor
Student::Student(char *n, long sn)
{
strcpy(name, n);
socialSecurity = sn;
}
// implementation section
void Student::setName(char *n)
{
strcpy(name, n);
}
// Student setSSN()
void Student::setSSN(long sn)
{
socialSecurity = sn;
}
char *Student::getName()
{
return name;
}
// Student getSSN
long Student::getSSN()
{
return socialSecurity;
}
int main()
{
// instantiate class objects
Student student1;
Student student2;
// set class object 2
student2.setName("John Doe");
student2.setSSN(11122555);
// class output
// output stream for student 1 using default constructor
cout << "The name for student1 is " << student1.getName()
<< " and ssn is " << student1.getSSN() << endl;
// output stream for student 2
cout << "The name for student2 is " << student2.getName()
<< " and ssn is " << student2.getSSN() << endl;
system("pause");
return 0;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For sure that would work!
ASKER
if ( ssn =< 0 || ssn > 999999999)?
would that work for validation?