headers, classes & main

I am new to headers and the main cpp file. So i have to create a program that calculates an employee's salary using her sales and I have to average every single one of her inputs. So this is my header file so far. Please tell me if I have it correctly. However, I don't know how to start my main file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <iomanip>

using namespace std;

class Salary
{
public: 
    Salary();
Salary(double employeeSales, double employeeSalary, double totalSalary)
:sales{employeeSales}, salary{employeeSalary}
{
    while(sales != -1)
    {
        totalSalary = 200 + (.09 * sales);
    }
}
void setSales(double employeeSales)
{
    sales = employeeSales;
}
void setSalary(double employeeSalary)
{
    salary = employeeSalary;
}
double getSales() const{return sales;}
double getSalary() const{return salary;}
private:
    double sales;
    double salary;
};
Iam not sure what IDE your using but start a new project (Some IDE's auto create a main) but you will need 3 files. a header file (.h) an implementation (.cpp) and a main file (.cpp). Click new file select new class file it will ask you to name your class and header file here (most will likely will also ask if you want to create and implementation file also) then hit create. If it doesnt create a imp file just go to create file empty and name it (name_imp.cpp). so the same for the main file but name it (main.cpp). now you have 3 files and are ready to get started. Use your header files to declare functions the write them in your imp file. finally use them in your main file.
basic example:
header file (burrito.h)
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef BURRITO_H
#define BURRITO_H


class burrito
{
    public:
        burrito();

};

#endif


imp file (burrito_imp.cpp)
1
2
3
4
5
6
7
8
9
10
#include "burrito.h"
#include <iostream>
// this file needs iostream and namsespace std
using namespace std;

burrito::burrito()
{
   cout << "I am apple" << endl;
}


Main file (burrito_main.cpp)
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "burrito.h"

using namespace std;

int main()
{
    burrito CO;
    return 0;
}



Be sure to add your header file to the main and imp file
Best of luck
Bigler
I am using codeblocks so I don't need a imp.cpp..
However, I am having trouble running my main program however when I run my header it works..
Does anyone know why?
this is my header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <iomanip>

class Salary
{
public:
Salary();
Salary(double employeeSales, double employeeSalary)
:sales{employeeSales}
{
    while(sales != -1)
    {
        employeeSalary = 200 + (.09 * sales);
    }
}
void setSales(double employeeSales)
{
    sales = employeeSales;
}
void setSalary(double employeeSalary)
{
    salary = employeeSalary;
}

double getSales() const{return sales;}
double getSalary() const{return salary;}
private:
    double sales;
    double salary;
};


this is my main .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip>
#include "Salary.h"

using namespace std;
int main()
{
    Salary mySalary;
    double employeeSales;
    double employeeSalary;
    cout << fixed << setprecision(2);
    cout << "Enter sales in dollars(-1 to quit): ";
    cin >> employeeSales;
    mySalary.setSales(employeeSales);

    while(true)
    {
        mySalary.setSalary(employeeSalary);
        cout << "Salary is: $" << mySalary.getSalary();
        cout << "Enter sales in dollars(-1 to quit): ";
        cin >> employeeSales;
        mySalary.setSales(employeeSales);
    }
}


the error it gives me is undefined reference to Salary::Salary()
Last edited on
You have declared a default constructor in class Salary (line 7), but not implemented it.
Okay now my code works. However the formula in my header in the while loop doesn't transfer to my main.cpp when I run the program..
Is it correct to put the formula in the while loop in header or do I have to do it in int main or is there a way to have it work on both files? and also in my main cpp file.. the while statement is off because when I entered -1 it gives me 199 instead of returning to 0. any idea how this can be fixed?
Last edited on
I am using codeblocks so I don't need a imp.cpp..


But you should though. You should have main.cpp , Sales.hpp, Sales.cpp. I use hpp because it means a header file with c++ code inside. CodeBlocks has good options for creating the class and it's associated files for you.

You don't really need setSales nor setSalary, just use the constructor you have. Set functions are only needed if the state is going to change after the object has been created.

The concept of using -1 and the infinite loop on line 16 is wrong here. If you have multiple values then you should be using a container like std::vector. Try to avoid infinite loops if you can, they are only needed if there are multiple dependent ways of terminating the loop.

Edit:

Salary is not a good name for the class. The clue is that you have employee in the variable names, so Employee is a good name for the class, with salary and sales as member variables. Naming is important in OOP design - wrong names can lead to bad design.

The reason why it is evil to put code into header files, is that they can be included multiple times. You should always have header guards and / or #pragma once , but really the compiler only needs the declaration of the class. It uses that to go find the definition of class functions - it's not good to put them both together. An #include directive basically does a copy/paste of that file, so one can see how that is bad if the file is #include d in multiple files.
Last edited on
So the assignment is to do it using a while loop and class. So my professor only wants two files. He gave us a complex example to follow that he showed in class using two while loops- one in header and one in main.

However, can I just use the while loop in main only?
For example
header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <iomanip>
#include "Salary.h"

using namespace std;
int main()
{
    Salary mySalary;
    double employeeSales;
    double employeeSalary;
    cout << fixed << setprecision(2);
    cout << "Enter sales in dollars(-1 to quit): ";
    cin >> employeeSales;
    mySalary.setSales(employeeSales);

    while(true)
    {
        employeeSalary = 200 + (.09 * employeeSales);
        mySalary.setSalary(employeeSalary);
        cout << "Salary is: $" << mySalary.getSalary();
        cout << "Enter sales in dollars(-1 to quit): ";
        cin >> employeeSales;
        mySalary.setSales(employeeSales);
    }

}


cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <iomanip>
#include "Employee.h"

using namespace std;
int main()
{
    Employee myEmployee;
    double employeeSales;
    double employeeSalary;
    cout << fixed << setprecision(2);
    cout << "Enter sales in dollars(-1 to quit): ";
    cin >> employeeSales;


    while(employeeSales != -1)
    {
        employeeSalary = 200 + (.09 * employeeSales);
        cout << "Salary is: $" << myEmployee.getSalary();
        cout << "Enter sales in dollars(-1 to quit): ";
        cin >> employeeSales;
    }

}
You have put the same code in twice :+)

The while doesn't make sense in the header file: there is only one value. So you could make it an if statement with an else instead.

The while in main is also a bit pointless: all it does is keep overwriting the value in the variable, although it does print some answers. As it stands, the class is unnecessary because the calculation is done in main. The point was proabably to do the calculation in the class object, and show the results in main.

I guess this is some kind of attempt at using a sentinel (-1), but it doesn't make sense in the absence of an array or std::vector
I would, but the instructions were to use a while loop to do the program.

so heres my .h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Employee
{
public:
Employee(){};
    Employee(double employeeSales, double employeeSalary)
    :sales{employeeSales},salary(employeeSalary) {}
void setSales(double employeeSales) {
                    sales = employeeSales;}
void setSalary(double employeeSalary) {
                salary = employeeSalary;}
double getSales() const{return sales;}
double getSalary() const{return salary;}
private:
    double sales;
    double salary;
};


would that be okay?
I made some changes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Employee
{
public:

Employee() {}
    
Employee(const double salesArg, const double salaryArg)
                :sales{salesArg}, salary{salaryArg} 
                {}

void setSales(const double salesArg) {
                    sales{salesArg} ;
} // put closing braces where they can be seen

void setSalary(const double salaryArg) {
                salary {salaryArg} ;
}

double getSales() const { return sales; }
double getSalary() const { return salary; }

private:
    double sales;
    double salary;
};


Don't be afraid to use some vertical white space to aid easy reading, same for spaces between words, operators etc. Dontruneverythingtogether.

Stroustrup advises against having get / set functions for every member of a class - it may as well a plain struct with public access. But teachers and lecturers often do otherwise :+)

Your main function should get the input, then use those 2 values to call the constructor. This avoids the set functions as well. You could avoid the get functions by having a print function, which prints the data. That way the code is in the class where it should be, not in main.

The get functions would be necessary if one wanted to do some further calculation with the data, say if one had a collection of employees, and wanted to find the total sales for all of them.

Edit:

So i have to create a program that calculates an employee's salary using her sales and I have to average every single one of her inputs.


So that implies you need a collection of Employees, as I said earlier a std::vector<Employee> . So the -1 is a sentinel in a while loop to stop the push_back of data into the vector. And you will need a way of storing the data for the collection: The sums and average values.
Last edited on
Topic archived. No new replies allowed.