Link to home
Start Free TrialLog in
Avatar of c9725458
c9725458

asked on

Setting up a Class

How do I set up a Person Class to include attributes name,date of birth,date of joining,payroll reference. Methods are: default constructor;constuctor with all details;display;changeName;getName;getDob;getReference;validateAge;?????????????
Avatar of msmits
msmits

Probably by reading a text-book on C++ first...
You could just define the class with all the items you mention as member variables and the functions you mention as member functions.

Avatar of c9725458

ASKER

That doesn't really help. I'm not too good at C++. I've tried reading about this but I don't get it
Adjusted points to 100
Maybe it is best to attend the lectures next time :-) This is very basic C++, the description I gave should be enough to get you on the way. If it doesn't you may be needing an easier book on C++.

If you will attempt your homework and post what you get done, we would be happy to ofter suggestions.  Or if you have SPECIFIC questions we can answer them. Other than that, it would be unethical for us to help you.
Thanks I'll do that.
Am I on the right track for creating the .h file?

.h file

class Person{
             charName;
             intDOB;
             intDateOfJoining;
             intPayrollRef;
Person();    //default constructor??

bool SetElement(whatever attributes);

int GetElement(whatever attributes,
               bool& bError);  
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Now

char Name;

will store only one character.  Most names are longer than that.  You can use a fixed size aray to store the name so long as you don't have names longer than the array size like

char Name[21];

which can store a name up to 20 characters long (plus one more char for terminator.)  The other option is to store the name in a dynamically alocated array.  The class must manage this array to prevent memory leaks.  If you are not familair with new and delete, go with the fixed size array.

You are hoping to store dates in a integer?  how will you do that?  Most people store dates as 3 seperate values (month, day, year).  Since you have more than one date in the class you might want to create a date class to store the date.  It could store three values.  The month, the date, and the year.  The month could be an integer or an enumerated type.  The date and the year should probably be integers.  

Try these changes and post back what you get.
Still on the right track?
.cpp
#include<Person.h>
#include<iostream.h>
#include<conio.h>

Person ::Person();

bool Person::SetElement(variables)
{  
    //main part
};  //end of Person::SetElement

char Person::GetElement (variables,
                         bool& bError)
{
  // main part
} //end of Person::GetElement()
Thanks for the help.
I'll come back in the next couple of days and check my solution with you if that's alright.
I'm not sure what you are hoping to do with the set element/get element stuff.  Why not continue to get the class's data members into the class (like in your first try).  Then we'll worry about the procedure member.
Her are my comments on what you've done...

class Person{
// this is fine

  char Name;
// this only holds a single charactre - not much of a name!
// change to 'char Name[MAXNAME];' and add before this
// 'enum { MAXNAME=xxx};' where xxx is max number of chars (+1)
// that a name can hold

  int DOB;
// I'd use one of the standard C types for the date rather
// than putting it in an int.

  int DateOfJoining;
// same here

  int PayrollRef;
// assuming a payroll ref is jsut an integer number

  Person();    //default constructor??
// yes .. this is the default constructor

  bool SetElement(whatever attributes);
// instead of this, a better choise is to have separate
// set/get for each attribute.  eg.
//   bool SetPayrollRef(int newPayrollRef) {
//     PayrollRef = newPayrollRef;
//     return true; // cannot fail
//   }

int GetElement(whatever attributes,
               bool& bError);  
// I'd reverse this to make it consistent with SetElement
// andalso make it a const function (because it does not
// change your person object
// ie.
//    bool GetElement(whatever& attributes) const;
// eg.
//    bool GetPayrollRef(int& oldPayrollRef) const {
//      oldPayrollRef = PayrollRef;
//      return true; // cannot fail
//    }

Hope this helps you.

How is it going?  I figured you would try my suggestions and get back.  Or did you get it working?
I'm working on the next part of the assesment now. Thanks for your help.
I'll post it when I've had a good go.
Hope my comments were helpful as well.