?
Solved

How do you set up a class?

Posted on 2008-11-10
10
Medium Priority
?
267 Views
Last Modified: 2013-11-17
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!!
0
Comment
Question by:b_acs
  • 6
  • 3
10 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22926748
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22926948
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22926957
>>>> 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:b_acs
ID: 22927054
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
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 2000 total points
ID: 22927078
>>>> 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
 

Author Comment

by:b_acs
ID: 22927102
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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 2000 total points
ID: 22927132
>>>> 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
 

Author Comment

by:b_acs
ID: 22927190
"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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 2000 total points
ID: 22927192
>>>> 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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22927226
>>>> 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

Featured Post

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.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

829 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