2

I'm stuck on an assignment question i need to do that asks me to change a struct into a class type.

I'm given the struct as,

struct Employee 
{
string firstName;
string secondName;
float salary;
}

From there the main question is:

Turn the employee record into a class type rather than a structure type. The employee record class should have private member variables for all data. Include public member functions for each of the following:

  • Create a default constructor that sets the first name and last name to blank strings and salary to 0.

  • Create an overloaded constructor that sets the member variables to specified values.

  • Create member functions to set each of the member variables to a given value as an argument(i.e mutators)

  • Create member functions to retrieve data from each member variable(i.e accessors).

I then need to add this class to a simple test program to output the data.

My code so far:

#include <iostream>
#include <string>

using namespace std;

class EmployeeRecord
{
public:
    EmployeeRecord(string firstName, string secondName, float salary);
    EmployeeRecord();
    void set(string firstName, string secondName, float salary);
    void update();
    string get_firstName();
    string get_secondName();
    float get_salary();
    void output(ostream& outs);

private:
    string firstName;
    string secondName;
    float salary;
};

int main()
{
    EmployeeRecord employee1, employee2;
    EmployeeRecord();
    employee1.set("Joe", "Soap", 1456.00);
    employee1.output(cout);

    return 0;
}

EmployeeRecord::EmployeeRecord()
{
    firstName = "";
    secondName = "";
    salary = 0.00;

}

My problem is that when I try to run the program i get undefined reference errors on lines 28 and 29 and I don't know what this means exactly. Any help to put me in the right direction would be appreciated as this is my first attempt at using classes in my code while trying to follow my textbooks example.

Sorry if this is a duplicate post or just a bad question.

Bezuid
  • 47
  • 2
  • 7
  • 2
    It already is. Tada! "classes" and "structures" define the same types in C++. – StoryTeller - Unslander Monica Sep 12 '17 at 12:20
  • Tell your prof the thing is already a class. – juanchopanza Sep 12 '17 at 12:20
  • A `struct` type is a `class` type and vice versa. The only difference is that default access for a `struct` is `public`, and default access for a `class` is `private`. – Peter Sep 12 '17 at 12:22
  • 1
    Where do you define the `set` and `output` functions? They need to be defined if you want to use them. – NathanOliver Sep 12 '17 at 12:22
  • Why change the name to EmployeeRecord? – rustyx Sep 12 '17 at 12:23
  • You've DECLARED the constructors and the methods. Now you need to IMPLEMENT them. Hint: how does the compiler know what `void set(...)` does? –  Sep 12 '17 at 12:24
  • there is no "struct type" but only class type. See here :http://en.cppreference.com/w/cpp/language/type – 463035818_is_not_an_ai Sep 12 '17 at 12:24
  • 1
    Consider using a different type for `salary`. A `float` cannot represent mine exactly. The easiest thing to do is to use an integral type and work in cents. – Bathsheba Sep 12 '17 at 12:27
  • It looks to me like you are missing some of the functions that are declared in the class. You have a constructor but you need to add `set()` and `output()` class methods. You are trying to use those methods but they are not defined, there is no code for them. – Richard Chambers Sep 12 '17 at 12:29
  • Also a `struct` is similar to a `class` however there are also a couple of differences. In most cases they can be used interchangeably. The primary difference is the default visibility. For a `class` the default visibility of members of the data structure is `private` and for a `struct` the default visibility of members of the data structure is `public` as it is in the C programming language from which C++ evolved. Your professor probably gave you this assignment so that you can learn the differences between the two so ignore the chattering cynical comments. – Richard Chambers Sep 12 '17 at 12:37

0 Answers0