Data member initialization

This is a discussion on Data member initialization within the C++ Programming forums, part of the General Programming Boards category; Ah yes, here I kneel to my virtual knee to beg for the assistance of another.... I have established an ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    52

    Data member initialization

    Ah yes, here I kneel to my virtual knee to beg for the assistance of another....

    I have established an 'Employee' class. You can see from the crude code that I've hacked out what the data members are. Here's the problem, when returning the values in a cout statement, there's nothing there, just the random values from the heap.

    I've triple checked my constructors, 'get' functions, and 'set' functions...as far as the int and float functions, all is okay, perhaps somebody could clue me in as to where I'm amiss, it's all starting to look like a mess to me.

    As is indicated in the previous paragraph....I'm confident in my 'get' and 'set' functions for int and float. However, I KNOW that I'm doing something wrong with trying to extract the string value from my private members. I humbly ask if somebody could give me an example of the proper syntax involved, that or point me in a direction where the proper syntax is given? I've pored over all of my books and texts, searched this board, and looked at a few other sites and have come up with nil....

    Here's the code (crude as it may be...)

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    
    #include <string.h>
    using namespace std;
    
    #define CLS() system("cls")
    
    
    class Employee
    {
    private:
    	int Age;
    	int YearsofService;
    	float Salary;
    	string firstName;
    	string lastName;
    	string SSN;
    	int Dependents;
    	string fullName()
    	{
    		lastName[0] = ',';
    		lastName[1] = ' ';
    		char *strcat(char *lastName, const char *firstName);
    	}
    		
    
    public:
    	
    	Employee(void): Age(0), YearsofService(0), Salary(0.0f), firstName("unknown"), lastName("unknown"), SSN("000-00-0000"), Dependents(0)
    	{}
    	
    	Employee(int age, int years, float salary, string fname, string lname, string ssn, int depend);	
    	~Employee() {}
    
    	
    	int getAge() { return Age; }
    	int getYears() {return YearsofService; }
    	float getSal() { return Salary; }
    	string getFName() { return firstName; }
    	string getLName() { return lastName; } 
    	string getSSN() { return SSN; } 
    	int getDepends() { return Dependents; } 
    //	string getfullName() const { return fullName; }
    
    	void setAge(int age) { Age = age; }
    	void setYears(int years) { YearsofService = years; }
    	void setSal(float salary) { Salary = salary; }
    	void setFName(string fname) { firstName = fname; }
    	void setLName(string lname) { lastName = lname; }
    	void setSSN(string ssn) { SSN = ssn; }
    	void setDepends(int depend) { Dependents = depend; }
    	//void setfullName(string firstname, string lastname) { fullName = name; }
    };
    
    Employee::Employee(int age, int years, float salary, string fname, string lname, string ssn, int depend)
    {
    	cout << "Calling constructor" << endl;
    }
    
    
    
    //void Header();
    //void Output(Employee);
    
    
    
    int main(void)
    {
    	cout << setprecision(2);
    	cout.setf(ios::fixed);
    
    	
    //here is where I initialize....
    	Employee emp1(45, 18, 41900.00, "Sheila", "Savoy", "000-11-2222", 3);
    	
    	Employee emp2( 32, 8, 39800.00, "John", "Templeton", "111-22-3333", 3);
    	
    	Employee emp3( 29, 5, 29500.00, "Margorie", "Bassette","222-33-4444", 1);
    
    	
    	cout << setw(5) << right << "First Name: " << left << emp1.getFName() << endl;
    	cout << setw(5) << right << "Last Name:  " << left << emp1.getLName() << endl;
    	cout << setw(5) << right << "Social Security Number: "  << left << emp1.getSSN() << endl;
    	cout << setw(5) << right << "Age: " << left << emp1.getAge() << endl;
    	cout << setw(5) << right << "Years of Service: " << left << emp1.getYears() << endl;
    	cout << setw(5) << right << "Salary:  " << left << emp1.getSal() << endl;
    	cout << setw(5) << right << "Dependents:  " << left << emp1.getDepends() << endl;
    	cout << endl << endl;
    	cout << setw(5) << right << "First Name: " << left << emp2.getFName() << endl;
    	cout << setw(5) << right << "Last Name:  " << left << emp2.getLName() << endl;
    	cout << setw(5) << right << "Social Security Number: "  << left << emp2.getSSN() << endl;
    	cout << setw(5) << right << "Age: " << left << emp2.getAge() << endl;
    	cout << setw(5) << right << "Years of Service: " << left << emp2.getYears() << endl;
    	cout << setw(5) << right << "Salary:  " << left << emp2.getSal() << endl;
    	cout << setw(5) << right << "Dependents:  " << left << emp2.getDepends() << endl;
    	cout << endl << endl;	
    	cout << setw(5) << right << "First Name: " << left << emp3.getFName() << endl;
    	cout << setw(5) << right << "Last Name:  " << left << emp3.getLName() << endl;
    	cout << setw(5) << right << "Social Security Number: "  << left << emp3.getSSN() << endl;
    	cout << setw(5) << right << "Age: " << left << emp3.getAge() << endl;
    	cout << setw(5) << right << "Years of Service: " << left << emp3.getYears() << endl;
    	cout << setw(5) << right << "Salary:  " << left << emp3.getSal() << endl;
    	cout << setw(5) << right << "Dependents:  " << left << emp3.getDepends() << endl;
    
    return 0; 
    
    }
    Thank you to anyone who may have a suggestion or a proper reference point...

    OBTW...There is so much about this that is yet to be resolved, but this is the skeleton of my program and I'm running into these problems, perhaps this is a sign to get a job at 7-11 and play X-Box...

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    52
    I'm responding to my own post to amend one portion of code.

    In my constructor taking in 7 different variables...

    Code:
    Employee(int age, int years....//so on and so forth)
     // after the arguments were passed I forgot to include this...
         {
            Age = age;
            YearsofService = years;
            //etc;
         }
    Now that I've done this, I'm getting another error...but this I can fix. However, I don't think that the data members can be initialized to the values set to it if there is no link between the 'get' and the private member. A penny for some thoughts.
    Thank you.

  3. #3
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    The problem was that you didn't do anything in your constructor so the values that were being pulled out were trash values from the memory.

    change your constructor to look like this:

    Employee::Employee(int age, int years, float salary, string fname, string lname, string ssn, int depend)
    {
    cout << "Calling constructor" << endl;

    Age = age;
    YearsofService = years;
    Salary = salary;
    firstName = fname;
    lastName = lname;
    SSN = ssn;
    Dependents = depend;
    }
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  4. #4
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Having a constructor that does something is quite handy...

    The problem lies here:
    Code:
    Employee::Employee(int age, int years, float salary, string fname, string lname, string ssn, int depend)
    {
    	cout << "Calling constructor" << endl;
    }
    Your constructor does nothing..... It doesn't initialize anything.

    Here is a little tip:
    Code:
    Employee::Employee(int age, int years, float salary, string fname, string lname, string ssn, int depend)
    {
       cout << "Constructor called!" << endl;
    
       Age = age;
       YearsofService = years;
       Salary = salary;
    
       strcpy(firstName, fname);
       strcpy(lastName, lname);
       strcpy(SSN, ssn);
       
       Dependents = depend;   
    }

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    52
    Thanks!

    Now that I've done that it's running fine. Great icon by the way.

    Here's the new and improved code of crudity....

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    
    #include <string.h>
    using namespace std;
    
    #define CLS() system("cls")
    
    
    class Employee
    {
    private:
    	int Age;
    	int YearsofService;
    	float Salary;
    	string firstName;
    	string lastName;
    	string SSN;
    	int Dependents;
    	string fullName()
    	{
    		lastName[0] = ',';
    		lastName[1] = ' ';
    		char *strcat(char *lastName, const char *firstName);
    	}
    		
    
    public:
    	
    	Employee(void): Age(0), YearsofService(0), Salary(0.0f), firstName("unknown"), lastName("unknown"), SSN("000-00-0000"), Dependents(0)
    	{}
    	
    	Employee(string fname, string lname, string ssn);
    	Employee(int age, int years, float salary, string fname, string lname, string ssn, int depend)	
    	{
    		
    		cout << "Constructor called." << endl;
    		
    		Age = age;
    		YearsofService = years;
    		Salary = salary;
    		firstName = fname;
    		lastName = lname;
    		SSN = ssn;
    		Dependents = depend;
    	}
    	
    	
    	
    	~Employee() {}
    
    	int getAge() { return Age; }
    	int getYears() {return YearsofService; }
    	float getSal() { return Salary; }
    	string getFName() { return firstName; }
    	string getLName() { return lastName; } 
    	string getSSN() { return SSN; } 
    	int getDepends() { return Dependents; } 
    	//string getfullName() const { return fullName; }
    
    	void setAge(int age) { Age = age; }
    	void setYears(int years) { YearsofService = years; }
    	void setSal(float salary) { Salary = salary; }
    	void setFName(string fname) { firstName = fname; }
    	void setLName(string lname) { lastName = lname; }
    	void setSSN(string ssn) { SSN = ssn; }
    	void setDepends(int depend) { Dependents = depend; }
    	//void setfullName(string firstname, string lastname) { fullName = name; }
    };
    
    
    int main(void)
    {
    	cout << setprecision(2);
    	cout.setf(ios::fixed);
    
    	cin.ignore(100,'\n');
    
    	Employee emp1(45, 18, 41900.00, "Sheila", "Savoy", "000-11-2222", 3);
    	
    	Employee emp2( 32, 8, 39800.00, "John", "Templeton", "111-22-3333", 3);
    	
    	Employee emp3( 29, 5, 29500.00, "Margorie", "Bassette","222-33-4444", 1);
    
    	
    	cout << setw(5) << right << "First Name: " << left << emp1.getFName() << endl;
    	cout << setw(5) << right << "Last Name:  " << left << emp1.getLName() << endl;
    	cout << setw(5) << right << "Social Security Number: "  << left << emp1.getSSN() << endl;
    	cout << setw(5) << right << "Age: " << left << emp1.getAge() << endl;
    	cout << setw(5) << right << "Years of Service: " << left << emp1.getYears() << endl;
    	cout << setw(5) << right << "Salary:  " << left << emp1.getSal() << endl;
    	cout << setw(5) << right << "Dependents:  " << left << emp1.getDepends() << endl;
    	cout << endl << endl;
    	cout << setw(5) << right << "First Name: " << left << emp2.getFName() << endl;
    	cout << setw(5) << right << "Last Name:  " << left << emp2.getLName() << endl;
    	cout << setw(5) << right << "Social Security Number: "  << left << emp2.getSSN() << endl;
    	cout << setw(5) << right << "Age: " << left << emp2.getAge() << endl;
    	cout << setw(5) << right << "Years of Service: " << left << emp2.getYears() << endl;
    	cout << setw(5) << right << "Salary:  " << left << emp2.getSal() << endl;
    	cout << setw(5) << right << "Dependents:  " << left << emp2.getDepends() << endl;
    	cout << endl << endl;	
    	cout << setw(5) << right << "First Name: " << left << emp3.getFName() << endl;
    	cout << setw(5) << right << "Last Name:  " << left << emp3.getLName() << endl;
    	cout << setw(5) << right << "Social Security Number: "  << left << emp3.getSSN() << endl;
    	cout << setw(5) << right << "Age: " << left << emp3.getAge() << endl;
    	cout << setw(5) << right << "Years of Service: " << left << emp3.getYears() << endl;
    	cout << setw(5) << right << "Salary:  " << left << emp3.getSal() << endl;
    	cout << setw(5) << right << "Dependents:  " << left << emp3.getDepends() << endl;
    
    	return 0; 
    
    }
    Thanks for the input!
    Last edited by Fyodorox; 04-29-2002 at 11:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  3. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  4. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-07-2007, 11:24 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21