Solved

Setting up a Class

Posted on 1998-04-28
16
202 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.

747 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

15 Experts available now in Live!

Get 1:1 Help Now