Hey Folks.

I am working on a project in which i have to use Classes and do multiple things with different sorts of data. The basic question i have though is this: what am i doing wrong here in terms of this class declaration etc... i have looked around on here, other websites about classes, used my text book and my lecture notes - but none really correlate with how i need to set up this class/constructor etc.

Please let me know how i could fix this to work it - and then i should be on a roll. Please include a brief description of where i could start if you dont wish to help me 100%!

Thanks - your help is always appreicated!

#include <iostream>
#include <fstream>
#include <ostream>
#include <string>

using namespace std;

class Employee
  {
  private:    
  //class variables
  char last_name[39];
  char first_name[39];
  char ss_num[9];
  double salary;
  int years_employed;
  
  public:
  //class constructor
  Employee()
     {
     last_name = "";
     first_name = "";
     ss_num = "";
     salary = 0;
     years_employed = 0;
     }
  //class methods
  void print();
  void setName(char [], char []);
  void setSSN(char []);
  void setSalary(double);
  void setYearsEmployed(int);
  int getLeaveTime();
} ;

int main()
{
Employee myName;
myName("Hogan", "Tony", "12300123", 55000, 10);

system("PAUSE");    
return 0;   
}

As you can see im trying to make a constructor with a last name, first name, SSN, salary and then years employed. I cant seem to get it working - im sure there is something obvious going on tho!

CHeers

Recommended Answers

All 8 Replies

1. you can not initialize c-style character arrays like you are trying to do in the class constructor. My suggestion is to declare them as c++ string objects. If you can not do that then to initialize the arrays to empty string just set the first byte to 0, like this: last_name[0] = 0; line 30: you have to add the parameter names as well as their data types, and declare them as const because they are unchangeable by the setName function. void setName(const char* LastName, const char* FirstName); You need to change the other methods in lines 31-34 the same way.

line 40: too many parameters -- see line 30

#include <iostream>
#include <fstream>
#include <ostream>
#include <string>

using namespace std;

class Employee
  {
  private:    
  //class variables
  char last_name[39];
  char first_name[39];
  char ss_num[9];
  double salary;
  int years_employed;
  
  public:
  //class constructor
  Employee(char last_name = "", char first_name = "", char ss_num = "0", 
           double salary = 0, int years_employed = 0);

  //class methods
  void print();
  void setName(char LastName , char FirstName);
  void setSSN(char []);
  void setSalary(double);
  void setYearsEmployed(int);
  int getLeaveTime();
} ;

//Constructor????
Employee::Employee(char last, char first, char SSN, double sal, int years)
{
  strcpy(last_name, last);
  strcpy(first_name, first);
  strcpy(ss_num, 1);
  salary = sal;
  years_employed = years;
}
//Methods???
void Employee::setName(char last, char first)
{
     if (last < 0)
     strcpy(last_name, last);
     if (first < 0)
     strcpy(first_name, first);
}

int main()
{
//WHY DOESNT THIS WORK?
Employee emp;

//doesnt like my emp:: part is it just emp.setName() ???
emp.setName("Hogan", "Tony");

system("PAUSE");    
return 0;   
}

This is what i changed my code to - i have got lots of errors. Unsure as to the cause - if anyone could help me out with the basics of classes/constructors etc please help me out here. I don't understand them - thanks folks.

I just remove what it confused me.

#include <iostream>

#include <fstream>
#include <ostream>

#include <string>


 

using namespace std;

 

class Employee

  {  public:

  //class constructor

  Employee(char [],char [],char [],int,int);
//class methods
  int getLeaveTime();
  
  private:    

  //class variables

  char last_name[39];
  ;

  char first_name[39];

  char ss_num[9];

  double salary;

  int years_employed;

} ;
Employee::Employee(char f[],char g[],char h[],int t,int y)
{
    last_name[39]=f[39];

     first_name[39] = g[39];

     ss_num[39] = g[39];

     salary = t;

     years_employed = y;

    
}
 

int main()

{

Employee myName("Hogan", "Tony", "12300123", 55000, 10);

 

system("PAUSE");    

return 0;   

}

Replay if This HelpFull.

elete: lines 43, 45 and 47 of the code you posted are wrong. C-style character arrays can not be copied like that. You have to call strcpy function to copy them.

tones:

line 33: close, you forgot to make the variables pointers, see the stars here: Employee(char* last_name = "", char* first_name = "", char* ss_num = "0", double salary = 0, int years_employed = 0);

Yes you are right because I succeed to compile it but it was wrong when try to print it.
And now just a tested version.

#include <iostream>
#include <string>
using namespace std;

class Employee
{  
public:
Employee(char *,char*,char*, double, int);
void printFirst_Name();
private:    
char *last_name;
char *first_name;
char *ss_num;
double salary;
int years_employed;
};

Employee::Employee(char *a, char *s, char *d,double f,int g)
{
              last_name=a;
              first_name=s;
              ss_num=d;
              salary=f;
              years_employed=g;
 }
              
void Employee::printFirst_Name()
{
     cout <<first_name;
     cout <<" "<<salary;
}
int main()
{
Employee myName("Hogan", "Tony", "12300123", 55000, 10);
myName.printFirst_Name();//just example with First_name All others are same
cin.get();
return 0;
}

elite: you are still wrong! That does indeed copy the pointers but the class needs its own copy of those strings because the calling function may change the string contents. If you need to use character arrays then declare them as in the original post but in the class constructor you have to call strcpy to duplicate the string so that the calling function can not change the string contents.

My personal preference is to use std::string class instead of character arrays, but the OP may not be able to do that.

You are righ again.

#include <iostream>
#include <string.h>
using namespace std;

class Employee
{  
public:
Employee(char *,char *,char *, double, int);
void printFirst_Name();
private:    
char last_name[39];
char first_name[39];
char ss_num[39];
double salary;
int years_employed;
};

Employee::Employee(char *first, char *last, char *ss,double f,int g)
{
              strcpy(last_name,first);
              strcpy(first_name, last);
              strcpy(ss_num, ss);
              salary=f;
              years_employed=g;
}
void Employee::printFirst_Name()
{
     cout <<first_name;
     cout <<" "<<last_name;
     cout <<" "<<ss_num;
     cout <<" "<<salary<<" "<<years_employed;
}
int main()
{
Employee myName("Hogan", "Tony", "12300123", 55000, 10);
myName.printFirst_Name();//just example with First_name All others are same
cin.get();
return 0;
}

Is it correct now?

I was able to get the code working. Thanks for all of your help guys. Once again... this forum has been very helpful... thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.