Link to home
Start Free TrialLog in
Avatar of b_acs
b_acsFlag for United States of America

asked on

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!!
Avatar of Infinity08
Infinity08
Flag of Belgium image

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

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

Avatar of b_acs

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of b_acs

ASKER

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."
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of b_acs

ASKER

"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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>>> 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).