Write the class definition for a class named Employee. The class should include data members for an employee object's name and salary (the salary will be an integer). The class should contain two member functions: the constructor and a function that allows a program to assign values to the data members. Add two member functions to the employee class. One member function should allow any program using an employee object to view the contents of the salary data member. The other member function should allow the program to view the contents of the employee name data member. (Hint: have the member functions simply return the contents of the appropriate data member).

Add another member function to the class. This function should calculate an employee object's new salary, based on a raise percentage, provided by the program (main function). Before calculating the raise, the member function should verify that the raise percentage is greater or equal to zero. If the raise percentage is less then zero, the member function should display an error message.

Write main function that will create an array of employee objects, assign values to the objects, display the names and current salaries for all objects, ask user for the raise percentage and then calculate and display new salaries for all objects.

I need to write a a class and a program to make it run like the one below but with the requirements above. I am in a C++ program, but i miss some lessons. I need someone to help me because I don't know how to start it. :(

#include<iostream>
#include<string>
using namespace std;

class person//class declaration
{
public:
    //const member function -
    //doesn't change value of private
    //variable of the class
    //get and set functions
    //get - allows user to use private 
    //variable of the class
    string get_name()//inline function - 
        //function declaration is replaced with 
        //function definition
    { 
        return name;
    }
    //set - allows user to set value for 
    //private variable of the class
    void set_name(string n)
    {
        name=n;
    }
    //constructors
    person();//default
    person(string, int);//with parameters
    void input();
    void print() const;//constant function
private:
    string name;
    int age;
};

//class definition
person::person()
{
    name="";
    age=0;
}
person::person(string n, int a)
{
    name=n;
    age=a;
}
void person::input()
{
    cout<<"Enter name and age"<<endl;
    cin>>name>>age;
}
void person::print() const
{
    cout<<"Name "<<name<<" age "<<age<<endl;
}


void main()
{
    person a;
    person b("Bob",22);

    a.print();
    b.print();
    b.set_name("Mary");
    cout<<b.get_name()<<endl;
    person ar[5];
    for(int i=0; i<5; i++)
    {
        ar[i].input();
    }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

class Person
{
  public:
    Person();
    Person(string pName, int pSalary);
    void setName(string pName);
    void setSalary(int pSalary);
    void printAll() const;
  private:
    string name;
    int salary;
};

I had the exact same assignment as you. OMG, brings back memories. Here's a snippet of what I did. It's not everything, but if you look at the code it'll give you an idea of how to proceed.

void person::input()
{
    cout<<"Enter name and age"<<endl;
    cin>>name>>age;
}

I did this part differently. What I did for a similar assignment was to have something like:

void setName(string pName);

in the public section of your object. Then I would use:

void Person::setName(string pName)
{
    name = pName;
}

to set the name. You can set both name and salary at the same time if you edit the code. Then, in main you can:

Person me;
me.setName("Necrolin");

can anyone help me with this program errors?

Error 1 error C2470: 'Employee' : looks like a function definition, but there is no parameter list; skipping apparent body c:\users\5vistastudent.bdbldg1701.001\documents\visual studio 2008\projects\hw\hw\d.cpp 29 HW
Error 2 error C2143: syntax error : missing ')' before ',' c:\users\5vistastudent.bdbldg1701.001\documents\visual studio 2008\projects\hw\hw\d.cpp 58 HW
Error 3 error C2143: syntax error : missing ';' before ',' c:\users\5vistastudent.bdbldg1701.001\documents\visual studio 2008\projects\hw\hw\d.cpp 58 HW
Error 4 error C2059: syntax error : ')' c:\users\5vistastudent.bdbldg1701.001\documents\visual studio 2008\projects\hw\hw\d.cpp 58 HW
Error 5 error C2065: 'employee' : undeclared identifier c:\users\5vistastudent.bdbldg1701.001\documents\visual studio 2008\projects\hw\hw\d.cpp 60 HW
Error 6 error C2228: left of '.input' must have class/struct/union c:\users\5vistastudent.bdbldg1701.001\documents\visual studio 2008\projects\hw\hw\d.cpp 60 HW
Error 7 error C2065: 'employee' : undeclared identifier c:\users\5vistastudent.bdbldg1701.001\documents\visual studio 2008\projects\hw\hw\d.cpp 61 HW
Error 8 error C2228: left of '.set' must have class/struct/union c:\users\5vistastudent.bdbldg1701.001\documents\visual studio 2008\projects\hw\hw\d.cpp 61 HW
Error 9 error C2065: '_name' : undeclared identifier c:\users\5vistastudent.bdbldg1701.001\documents\visual studio 2008\projects\hw\hw\d.cpp 61 HW
Error 10 error C2065: '_salary' : undeclared identifier c:\users\5vistastudent.bdbldg1701.001\documents\visual studio 2008\projects\hw\hw\d.cpp 61 HW
Warning 11 warning C4832: token '.' is illegal after UDT 'Employee' c:\users\5vistastudent.bdbldg1701.001\documents\visual studio 2008\projects\hw\hw\d.cpp 62 HW
Error 12 error C2275: 'Employee' : illegal use of this type as an expression c:\users\5vistastudent.bdbldg1701.001\documents\visual studio 2008\projects\hw\hw\d.cpp 62 HW
Warning 13 warning C4832: token '.' is illegal after UDT 'Employee' c:\users\5vistastudent.bdbldg1701.001\documents\visual studio 2008\projects\hw\hw\d.cpp 62 HW
Error 14 error C2275: 'Employee' : illegal use of this type as an expression c:\users\5vistastudent.bdbldg1701.001\documents\visual studio 2008\projects\hw\hw\d.cpp 62 HW
Error 15 fatal error C1075: end of file found before the left brace '{' at 'c:\users\5vistastudent.bdbldg1701.001\documents\visual studio 2008\projects\hw\hw\d.cpp(55)' was matched c:\users\5vistastudent.bdbldg1701.001\documents\visual studio 2008\projects\hw\hw\d.cpp 64 HW


#include<iostream>
#include<string>
using namespace std;
class Employee
{
private:
string _name;
int _salary;
public:
void input();
void output();
void print();
int employee();
void set(string name, int salary);
int get_salary();
string get_name();
int computerNewSalary(int raisepercentage) const;
};
void Employee::set(string name, int salary)
{
_name=name;
_salary=salary;
}
void Employee::input()
{
cout<<"Enter name and salary :"<<endl;
cin>>_name>>_salary;
}
void Employee:input()
{
cout<<"Name"<<_name<<"Salary "<<_salary<<endl;
}
int Employee::get_salary()
{
return _salary;
}
string Employee::get_name()
{
return _name;
}
int Employee::computerNewSalary(int raisepercentage) const
{
if (raisepercentage<0)
{
cout<<"Invalid raise percentage."<<endl;
return _salary;
}
else
{
return _salary*(1+(raisepercentage/100));
}
}

int main(int argc, char** argv)
{
int i;
Employee;
for(i=0; i<3;, ++i);
{
employee.input();
employee.set(_name,_salary);
cout<<"Employee"<<i<<""<<Employee.get_name()<<"Salary;"<<Employee.get_salary()<<endl;
}

for(i=0; i<3;, ++i);

Delete the ";" at the end and the comma in the middle

For (i = 0; i < 3; ++i)
{

}

int main(int argc, char** argv)
{
 int i;
 Employee;
 for(i=0; i<3;, ++i);
{
  employee.input();
  employee.set(_name,_salary);
  cout<<"Employee"<<i<<""<<Employee.get_name()<<"Salary;"  <<Employee.get_salary()<<endl;
}

_name and _salary are private in the class and are not accessible in main. You will have to declare them in main one more time. Plus, they haven't been initialized. When you set the name you're passing values to the class. This is just the same as if you were using a plain old function.

string _name = "Bill";
int _salary = 9999;
Employee person;
person.set(_name, _salary);

Here's a partial program to help you out. Read it, try to understand it, and see if you can finish your assignment.

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

class Employee
{
public:
    Employee();  //Default with no name and no salary
    Employee(string eName, int eSalary); //create with name...

    void input();
    void output() const;
    void computeNewSalary(int raisepercentage);
private:
    string _name;
    int _salary;
};

//Default constructor
Employee::Employee()
{
    _name = "";
    _salary = 0;
}

//Constructor with name/salary
Employee::Employee(string eName, int eSalary)
{
    _name = eName;
    _salary = eSalary;
}

//Set name and salary
void Employee::input()
{
    cout << "Enter name and salary: ";
    cin >> _name >> _salary;
}

//output, should be "const" as it is not changing any variables
void Employee::output() const
{
    cout << "Name: " << _name << endl << "Salary: " << _salary << endl;
}

//Raise salary within your object
void Employee::computeNewSalary(int raisepercentage)
{
    _salary = _salary * (raisepercentage/100.0);
}

int main()
{
    //Create default object
    Employee me;
    //Set name and salary
    me.input();
    //Print out name and salary
    me.output();
    //Raise salary
    me.computeNewSalary(150);
    me.output();
    return EXIT_SUCCESS;
}

Is anyone have an idea of how to create a main function?

Is anyone have an idea of how to create a main function?

Why don't you just use a few for loops and an Employee array. Here's a really rough example (it compiles & works, but it's not pretty) =)

int main()
{
    Employee employees[10];
    int percent;
    
    for (int i = 0; i < 10; ++i)
    {
        employees[i] = Employee();
        employees[i].input();
        cout << "How much to raise salary: ";
        cin >> percent;
        employees[i].computeNewSalary(percent);
        employees[i].output();
    }
    
    return EXIT_SUCCESS;
}

Note: Obviously I compiled this with the object that I posted previously.... It works, but it doesn't meet a lot of your requirements. You'll have to go over your assignment carefully and tweak anything I wrote before you use it to make sure you get points. For example, I didn't verify that percent is greater than zero, etc. But at least you have an example that works.

This article has been dead for over six months. Start a new discussion instead.