How do you set up a class?

I need to set up a class with a public constructor and a Print function, and four character array data members.  I will avoid giving out too many details on this problem since it is a homework assignment and I'm looking for assistance on learning how to create classes such as this.  I'm supposed to do everything from the constructor and that's what is throwiing me off, since I'm having trouble finding examples.  Please lend me your wisdom!!
b_acsAsked:
Who is Participating?
 
itsmeandnobodyelseConnect With a Mentor Commented:
>>>> char firstname;
that is one single char.

for a char array you need

   char firstname[30];

but you have to care for initialization and not writing beyond the buffer size.

Or - as recommended -


  std::string firstname;
0
 
Infinity08Commented:
I would suggest to read up on classes first. Here's a nice tutorial for example :

        http://www.cplusplus.com/doc/tutorial/classes.html
        http://www.cplusplus.com/doc/tutorial/classes2.html

Please ask whenever something is not clear.
0
 
itsmeandnobodyelseCommented:
You normally put the class definition int a (separate) header file and protect it to being included more than once:

//myclass.h
#ifndef MYCLASS_H
#define MYCLASS_H

class MyClass
{
public:
// put here the declaration of the constructor, destructor and the public functions

private:
// put here the data members

};

#endif


// myclass.cpp

// some system includes
#include <iostream>
using namespace std;  // then you don't need to use the std:: prefix

#include <myclass.h>

// put here the implementation of all the constructors, destructor, operators and functions

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
itsmeandnobodyelseCommented:
>>>> and four character array data members.
You should know that character arrays is plain C what could be used in C++ but is not recommended to do so.

You better would use std::string class instead of char arrays.

//myclass.h
#ifndef MYCLASS_H
#define MYCLASS_H

#include <string>

class MyClass
{
public:
// put here the declaration of the constructor, destructor and the public functions

private:
// put here the data members
    std::string lastname;
    std::string firstname;
    ...
};

#endif

0
 
b_acsAuthor Commented:
Ok this is what I have so far, but having issues still.....
Can't figure out what is wrong
                                                                     
                                                                     
                                                                     
                                             
#include<iostream> 
using namespace std;
 
class Employee{ 
private:  
char firstname; 
char lastname; 
char middlename; 
float ssn;
 
public: 
void getinfo(); 
void disinfo(); 
}; 
void Employee::getinfo() 
{ 
cout << " First name: "; 
cin >> firstname; 
cout << " Last name :"; 
cin >> lastname; 
cout << " Middle name : :"; 
cin >> middlename; 
cout << " SSN :"; 
cin >> ssn; 
} 
void Employee::disinfo() 
{ 
cout<<endl; 
cout<< " First Name = "<< firstname << endl; 
cout<< " Last Name =" << lastname << endl; 
cout<< " Middle Name =" << middlename << endl; 
cout<< " SNN =" << ssn << endl; 
} 
void main() 
{ 
Employee a; 
cout << " Enter the following information " << endl; 
a.getinfo(); 
cout << " \n Contents of class "<< endl; 
a.disinfo(); 
} 

Open in new window

0
 
b_acsAuthor Commented:
Ok, now it works!!  Thanks!
I'm ignorant, what do you mean by

"but you have to care for initialization and not writing beyond the buffer size."
0
 
itsmeandnobodyelseConnect With a Mentor Commented:
>>>> void Employee::getinfo()
a get function should have a return value. Name it showInfo if you want to output on screen only.

If you want to *get* input from user, name it 'inputUserInfo' . But it rarely makes sense to make it a member of class Employee. Either make it a global function called in main or add a new class UserInfo where it may be a member function of.

class UserInfo
{
public:
      bool  inputEmployee(Employee& empl);

};


int main()
{
     UserInfo ui;
     Employee e;
     bool ok = ui.inputEmployee(e);
     ---
     return 0;
}
0
 
b_acsAuthor Commented:
"If you want to *get* input from user, name it 'inputUserInfo' . But it rarely makes sense to make it a member of class Employee. Either make it a global function called in main or add a new class UserInfo where it may be a member function of."

Normally I would make it a global function.  That makes more sense to me, lol.  This was a hoemwork assignment and the instructions specifically said create one class and do everything within the constructor.

Thank you
0
 
itsmeandnobodyelseConnect With a Mentor Commented:
>>>> but you have to care for initialization and not writing beyond the buffer size."
The problem with C char arrays is that they were *not* initialized by default. You *have* to initialize themm in the constructor:

Employee::Employee()
{
     firstname[0] = '\0';  
     lastname[0] = '\0';
     ...
}


Actually that is still half work cause it only initializes the very first byte of the array. But as strings in char arrays were zero-terminated in C/C++, it would return you a zero-length string after construction.

A full initialization would be made using a loop or runtime functions like memset:

    for (int i = 0; i < sizeof(lastname); ++i)
        lastname[i] = '\0';

or

    memset(lastname, 0, sizeof(lastname));

You see, it is not really comfortable...

The next problem arises when the user enters the name.

   cin >> lastname;

If the user inputs more than 29 characters  the cin would write beyond the allocated buffer-size and it could give a crash.

Furthermore if a space was entered in the name (Prince of Wales) the cin would stop reading and you only have 'Prince' in the array.

So, you have to do something like

   cin.read(lastname, sizeof(lastname));

to solve the buffer overflow issue.

If using std::string you neither need to initialize and the input works with

   getline(cin, lastname);

with no size limitation (2^32-1 > 4 billion).

 

0
 
itsmeandnobodyelseCommented:
>>>> and the instructions specifically said create one class and do everything within the constructor.
Ok. We were lost...

The problem is that teachers often teach C (what they learned when they were young) and call it C++.

Note, OOP (object oriented programming) wouldn't do *everything* in the constructor. A constructor has the task to create a new object. The default is to have *no* arguments. Then the constructor would initialize all (data) members to an empty value. You also could add constructors which get their initial data passed by arguments, in your case

   Employee::Employee(const char* ln, const char* fn, const char* mn, double s)
   {
       if(ln != NULL && strlen(ln) < sizeof(lastname))
           strcpy(lastname, ln);
       else
            throw ("wrong lastname");
       ....

   }

But you *never* would do i/o in a constructor.

Note, forget the float data type. And short and long as well. You only need char, int and double. And the unsigned char and unsigned int. In case of UNICODE the wchar_t type may be used additionally (it is a short at Windows and an int at most UNIX).

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.