Solved

Setting up a Class

Posted on 1998-04-28
16
224 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

856 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