1

I am quite new to c++ and have been struggling on a part of an assignment for the last week.

I know it's a simple thing but I can't wrap my head around the right way to go about it.

I need to create four objects for the class (Employee) that I've made and assign them to the array I've made

heres my default constructor:

Employee() // Deafault Constructor 
    {
        empNumber = 00000;
        hours, payRate = 0;
        name = "Name";
    }

And my attempt to create 4 objects with different data saved into the array elements:

int Employees[4];
    Employees[0] = Employee(67258, 40, 35, "Craig");
    Employees[1] = Employee(25387, 30, 20, "Carol");
    Employees[2] = Employee(11945, 38, 30, "Suzie");
    Employees[3] = Employee(90832, 28, 40, "Sam");

Like I said, basically brand new and I've been googling up a storm to no avail.

Any help or suggestions or even links are welcome!

Here's my whole code:

class Employee
{
private:

    int empNumber;
    string name;
    double hours, payRate;

public:

    Employee() // Deafault Constructor 
    {
        empNumber = 00000;
        hours = 0;
        payRate = 0;
        name = "Name";
    }

    Employee(int e_empNumber, double e_hours, double e_payRate, string e_name) // Non-Default Constructor
    {
        empNumber = e_empNumber;
        hours = e_hours;
        payRate = e_payRate;
        name = e_name;
    }

    void getEmployeeData()
    {
        cout << "Employee #: " << empNumber << ' ';
        cout << "Name: " << name << ' ';
        cout << "Hours worked: " << hours << ' ';
        cout << "Pay Rate: " << payRate << endl;
    }

};

int main()
{
    int Employees[4];
    Employees[0] = Employee(67258, 40, 35, "Craig");
    Employees[1] = Employee(25387, 30, 20, "Carol");
    Employees[2] = Employee(11945, 38, 30, "Suzie");
    Employees[3] = Employee(90832, 28, 40, "Sam");

    system("pause");
    return 0;
}

and the errors

4
  • Note that hours, payRate = 0; only assigns 0 to the second variable, hours remains uninitialized. Commented Nov 24, 2019 at 20:59
  • Can you show us your code (see minimal reproducible example) and the exact error message you get? Commented Nov 24, 2019 at 20:59
  • 7
    You have an array of int and you are trying to insert the type Employee. They are not the same thing Commented Nov 24, 2019 at 21:00
  • it's all I know, googling now - Wow @Amedeus, your comment helped so much, thank you! Commented Nov 24, 2019 at 21:07

3 Answers 3

1

Do that to create and fill the employees:

Employe employees[4];
    employees[0] = Employee(67258, 40, 35, "Craig");
    employees[1] = Employee(25387, 30, 20, "Carol");
    employees[2] = Employee(11945, 38, 30, "Suzie");
    employees[3] = Employee(90832, 28, 40, "Sam");

What you did is just creating an array of ints. Here you see how to do it:

Type variable_name[size];
Sign up to request clarification or add additional context in comments.

2 Comments

Ya! The comment from Amedeus helped me figure it out! Thank you for showing me too! This is great its been stressing me out all week!
Happy it helped. Please select an answer as correct.
0

Thanks for the help everyone! I got it working to the specifications!

Here's my final code:

#include <iostream>
using namespace std;




class Employee
{
private:

    int empNumber;
    string name;
    double hours, payRate;

public:

    Employee() // Deafault Constructor 
    {
        empNumber = 00000;
        hours = 0;
        payRate = 0;
        name = "Name";
    }

    Employee(int e_empNumber, double e_hours, double e_payRate, string e_name) // Non-Default Constructor
    {
        empNumber = e_empNumber;
        hours = e_hours;
        payRate = e_payRate;
        name = e_name;

        void getEmployeeData();
        {
            cout << "Employee #: " << empNumber << " - ";
            cout << "Name: " << name << " - ";
            cout << "Hours worked: " << hours << " - ";
            cout << "Pay Rate: " << payRate << endl;
        }
    }



};

int main()
{
    Employee Employees[4];
    Employees[0] = Employee(67258, 40, 35, "Craig");
    Employees[1] = Employee(25387, 30, 20, "Carol");
    Employees[2] = Employee(11945, 38, 30, "Suzie");
    Employees[3] = Employee(90832, 28, 40, "Sam");

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

        void getEmployeeData();
    }

    system("pause");
    return 0;
}

This will cout the Employee data stored in the array

1 Comment

Very small recommendation: The class name is correct: Employee, but it is recommended to use the variable name Employees starting as a lower case: employees. These small things will help you distinguish between type names and variables.
0

Just thought of adding a bit more insight.

When you call this:

Employee Employees[4];

The default ctor will be called to initialize the array. So four objects are automatically created. Then when you do the assignments:

Employees[0] = Employee(67258, 40, 35, "Craig");
Employees[1] = Employee(25387, 30, 20, "Carol");
Employees[2] = Employee(11945, 38, 30, "Suzie");
Employees[3] = Employee(90832, 28, 40, "Sam");

Four new objects are created using the parameterized ctor which will replace the originally created four. So the original ones are useless (since they were never used) and are just made for waste.

You can verify what I said by adding the following:

To default ctor

cout << "default ctor" << endl;

To parameterized ctor

cout << "parameterized ctor" << endl; 

If you want to avoid multiple construction of objects, you can use this:

Employee Employees[4] = {
  Employee(67258, 40, 35, "Craig"),
  Employee(25387, 30, 20, "Carol"),
  Employee(11945, 38, 30, "Suzie"),
  Employee(90832, 28, 40, "Sam")
};

This will not call the default ctor and will construct only four Employee objects.

Alternatively, you can use std::vector (#include <vector>) which gives you flexibility with size and does not use default ctor if you use push_back function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.