C++ Class Validation

Posted on 2008-11-07
Last Modified: 2013-12-14
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?  


#include <iostream>

#include <iomanip>

using namespace std;

class Student


      // private member variables


              long socialSecurity;

              char name[80];



      // public variables / methods       



             // 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");   



    // 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;





    return 0;



Open in new window

Question by:lwoodtri
    LVL 9

    Accepted Solution

    What you have to do is the followiing:
    Keep the constructor you already implemented and then add an empty constructor where you use the set members
    It should be something like this: in the definition part

     Student(char *n="unassigned",long sn=999999999);

    In the implementation keep the constructor you already did and add the other one


    LVL 1

    Author Comment

    Cool, I got that part . . however . . what if the assignment happens to be 0 or -1 for the value of SSN?  Would I use an

    if ( ssn =< 0 || ssn > 999999999)?

    would that work for validation?  
    LVL 9

    Expert Comment

    For sure that would work!

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
    How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from ( Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now