Solved

Setting up a Class

Posted on 1998-04-28
16
210 Views
Last Modified: 2010-04-10
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;?????????????
0
Comment
Question by:c9725458
  • 7
  • 5
  • 2
  • +1
16 Comments
 
LVL 3

Expert Comment

by:msmits
ID: 1165390
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.

0
 

Author Comment

by:c9725458
ID: 1165391
That doesn't really help. I'm not too good at C++. I've tried reading about this but I don't get it
0
 

Author Comment

by:c9725458
ID: 1165392
Adjusted points to 100
0
 
LVL 3

Expert Comment

by:msmits
ID: 1165393
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++.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1165394
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.
0
 

Author Comment

by:c9725458
ID: 1165395
Thanks I'll do that.
0
 

Author Comment

by:c9725458
ID: 1165396
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);  
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1165397
Good start.  you are missing spaces after your types.  should be "char name" not "charname".

continued.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:nietod
ID: 1165398
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.
0
 

Author Comment

by:c9725458
ID: 1165399
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()
0
 

Author Comment

by:c9725458
ID: 1165400
Thanks for the help.
I'll come back in the next couple of days and check my solution with you if that's alright.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165401
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.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1165402
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.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1165403
How is it going?  I figured you would try my suggestions and get back.  Or did you get it working?
0
 

Author Comment

by:c9725458
ID: 1165404
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.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1165405
Hope my comments were helpful as well.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

943 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

16 Experts available now in Live!

Get 1:1 Help Now