Pass array from class to main - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Pass array from class to main

neverbend007neverbend007 IrelandPosts: 0Member
edited November 2013 in Beginner C/C++

I am posting this on here as I tried asking for help on stackoverflow.com but got nothing but stupid arrogant remarks. I have been doing c++ for only a few weeks and I know that to experienced programmers there is probably a lot of errors in this but I am looking for some help.

I am trying to create an array of 10 Employee objects in a separate class file and then access it from my main. I have three separate files. Employee.cpp, Employee.h and EmployeeArray10.h

Employee.cpp is obviously the main, Employee.h holds the constructors, getters, setters for the Employee() class and EmployeeArray10.h is where the array of ten Employee() objects is created. The problem I am having is how do I create and the access the array created in the EmployeeArray10.h file from main() and consequently use the tostring() function from the employee() class on the objects created in the EmployeeArray10.h

I have been staring at this for hours, guidance would be appreciated. Please tell me if this is not clear enough.

#include <iostream>
#include <string>
#include "Employee.h"
#include "EmployeeArray10.h"

using namespace std;

int main(){

Employee employee1;
Employee employee2(001, "Rob", "Address");
Employee employee3(002, "Tom", "The Street", "Science Dept", 25000.00, "99-CW-4753");

employee1.tostring();
employee2.tostring();
employee3.tostring();

EmployeeArray10 employeeArray;

for(int i=0; i <=10; i++){

    employeeArray[i].EmployeeArray10.Employee.tostring();
}

return 0;

}

Employee.h :

#include <iostream>
#include <string>

using namespace std;

class Employee
{

private:
int number;
string name;
string address;
string department;
double salary;
string regNumber;


// CONSTRUCTOR METHODS
public:
Employee()
{
    setNumber(0);
    setName("");
    setAddress("");
    setDepartment("");
    setSalary(0.0);
    setRegNumber("");
}
Employee(int pNum, string pName, string pAddress)
{
    setNumber(pNum);
    setName(pName);
    setAddress(pAddress);
    setDepartment("");
    setSalary(0.0);
    setRegNumber("");
}
Employee(int pNum, string pName, string pAddress, string pDepartment, double pSalary, string pRegNumber)
{
    setNumber(pNum);
    setName(pName);
    setAddress(pAddress);
    setDepartment(pDepartment);
    setSalary(pSalary);
    setRegNumber(pRegNumber);
}
// SET METHODS
void setNumber(int pNum)
{
    number = pNum;
}
void setName(string pName)
{
    name = pName;
}
void setAddress(string pAddress)
{
    address = pAddress;
}
void setDepartment(string pDepartment)
{
    department = pDepartment;
}
void setSalary(double pSalary)
{
    salary = pSalary;
}
void setRegNumber(string pRegNumber)
{
    regNumber = pRegNumber;
}
// GET METHODS
int getNumber()
{
    return number;
}
string getName()
{
    return name;
}
string getAddress()
{
    return address;
}
string getDepartment()
{
    return department;
}
double getSalary()
{
    return salary;
}
string getRegNumber()
{
    return regNumber;
}
// MISC METHODS
void tostring()
{
    cout << "\n\nEmployee Object\nNumber: " << number <<
    "\nName: " << name <<
    "\nAddress: " << address <<
    "\nDepartment: " << department <<
    "\nSalary: " << salary <<
    "\nTax payable: " << taxPayable(salary) <<
    "\nCar reg. no.: " << regNumber <<"\n\n";

}

double taxPayable(double mSalary){

    double result = 0;

    if (mSalary <= 20000 && mSalary >=1){

        result = mSalary * 0.2;
    }

    else if(mSalary > 20000){

        result = (20000 * 0.2) + ((mSalary - 20000) * 0.4);

    }

    return result;
}
};

EmployeeArray10.h

#include <iostream>
#include <string>

using namespace std;

class EmployeeArray10{

public:
EmployeeArray10(){

    Employee *newEmpArray = new Employee[10];

        for(int i=0; i <=10; i++){

            newEmpArray[i].setNumber(i+1);

        }

}
};

Comments

  • pseudocoderpseudocoder Posts: 687Member ✭✭✭

    First and foremost, I'm not terribly knowledgeable in cpp; however, I'll try to help if I can.

    EmployeeArray10 employeeArray;
    ...
    employeeArray[i].EmployeeArray10.Employee.tostring();
    

    employeeArray is the instantiated object and not the Employee array. To get to it, you need to use the Employee class member functions.

    Your EmployeeArray10 class needs member functions to walk through the array and some way to tell where you are.

    class EmployeeArray10
    {
        private : int index;
                     int asize;
                     Employee *newEmpArray;  
                     ...
    
        public : EmployeeArray10(const int &init_size = 10) 
                    {
                        asize = init_size;
                        index = 0;
                        newEmpArray = new Employee[asize];
                        ...
                    }
                    ~EmployeeArray10() { delete [] newEmpArray; }
    
                    int getIndex() const { return index; }
                    void setData(field1, field2, fieldN)
                    {
                        newEmpArray[index].setNumber(field??);
                        newEmpArray[index].setName(field??);
                        ++index;
                        ...
                    }
        ...
    };
    

    Then getting back to your loop in main; you'd add data like

    for(int i=0; i < 10; i++)
    {
        employeeArray.setData(field1, field2, fieldN, ...);
    }

    You'd need to modify the tostring function to accept the Employee class as a parameter if you wanted to print all of the array at once; otherwise, you can create a member function in the EmployeeArray10 class to walk through each index position sort of like the addData function.

    for(i=0; i < 10; ++i)
        newEmpArray[index].tostring();
    }

    There's most likely a more preferred way of doing this, but it's about the extent of my level. Hopefully, it will help or at least give you an idea of something to try. Good luck.

Sign In or Register to comment.