Program that uses Classes and Data Objects

This is a project for a class Im taking

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 7677 Views - Last Post: 06 March 2010 - 04:42 PM Rate Topic: -----

#1 v8n3t  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 46
  • Joined: 08-January 09

Program that uses Classes and Data Objects

Posted 05 March 2010 - 10:42 AM

Hello everyone,

First off I want to say thank you to anyone who responds to this message; as this is a prayer for help because I can not find the answers online.

Below is the following code to my project.

My major problems are this:

1.) I can not figure out how to initialize the class
2.) I can not figure out how to pass the stored data inside the class for calculations
3.) I can not figure out how to make this program work at all.

Honestly and truthfully I just need to talk to someone over the phone and ask them real questions. I understand the basic concepts behind classes, unions, structs and OOP. I just do not know how to put all the pieces together correctly! This is so frustrating to me that I can not figure this stuff out.

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

//
//CLASS DECLARATION SECTION
//
class EmployeeClass {
public:
	void ImplementCalculations(string EmployeeName, double hours, double wage);
	void DisplayEmployInformation(void);
	void Addsomethingup (EmployeeClass, EmployeeClass, EmployeeClass);
	string EmployeeName ;
	int hours ;
	float wage ;
	float basepay ;
   	int overtime_hours ;
	float overtime_pay ;
	float overtime_extra ;
	float iTotal_salaries ;
	float iIndividualSalary ;
	int iTotal_hours ;
	int iTotal_OvertimeHours ;
};

int main()
{	system("cls"); 

	cout << "\nWelcome to the Employee Pay Center\n\n" endl;

/*
Use this section to define your objects.  You will have one object per employee.  You have only three employees.
The format is your class name and your object name.
*/

EmployeeClass.EmployeeName


/*
Here you will prompt for the first employee's information.
Prompt the employee name, hours worked, and the hourly wage.  For each piece of information, you will update the appropriate class member defined above.
*/

cout << "Enter the employee name:" endl;
cin >> EmployeeName;
cout << "Enter the hours worked:" endl;
cin >> hours;
cout << "Enter his or her hourly wage:" endl;
cin >> wage;


/*
Here you will prompt for the second employee's information.
Prompt the employee name, hours worked, and the hourly wage.  For each piece of information, you will update the appropriate class member defined above.
*/

cout << "Enter the employee name:" endl;
cin >> EmployeeName;
cout << "Enter the hours worked:" endl;
cin >> hours;
cout << "Enter his or her hourly wage:" endl;
cin >> wage;



/*
Here you will prompt for the third employee's information.
Prompt the employee name, hours worked, and the hourly wage.  For each piece of information, you will update the appropriate class member defined above.
*/

cout << "Enter the employee name:" endl;
cin >> EmployeeName;
cout << "Enter the hours worked:" endl;
cin >> hours;
cout << "Enter his or her hourly wage:" endl;
cin >> wage;


/*
Here you will implement a function call to implement the employ calcuations for each object defined above.  You will do this for each of the three employees or objects.
The format for this step is the following:
 [(object name.function name(objectname.name, objectname.hours, objectname.wage)] ;
*/



/*
This section you will send all three objects to a function that will add up the the following information:
- Total Employee Salaries 
- Total Employee Hours
- Total Overtime Hours

The format for this function is the following:
-	Define a new object.
-	Implement function call [objectname.functionname(object name 1, object name 2, object name 3)]
/*

} //End of Main Function


void EmployeeClass::ImplementCalculations (string EmployeeName, double hours, double wage){
//Initialize overtime variables
overtime_hours=0;
overtime_pay=0;
overtime_extra=0;

	if (hours > 40) 
	{		

/* 
This section is for the basic calculations for calculating overtime pay.
-	base pay = 40 hours times the hourly wage
-	overtime hours = hours worked - 40
-	overtime pay = hourly wage * 1.5
-	overtime extra pay over 40 = overtime hours * overtime pay
-	salary = overtime money over 40 hours + your base pay
*/

/*
Implement function call to output the employee information.  Function is defined below.
*/


	}	// if (hours > 40)
	else
	{	

/* Here you are going to calculate the hours less than 40 hours.
-	Your base pay is = your hours worked times your wage
-	Salary = your base pay
*/

/*
Implement function call to output the employee information.  Function is defined below.
*/

	} // End of the else

} //End of Primary Function

void EmployeeClass::DisplayEmployInformation () {
// This function displays all the employee output information.

/* 
This is your cout statements to display the employee information:
Employee Name ............. = 
Base Pay .................. = 
Hours in Overtime ......... = 
Overtime Pay Amount........ = 
Total Pay ................. = 
*/

} // END OF Display Employee Information

void EmployeeClass::Addsomethingup (EmployeeClass Employ1, EmployeeClass  Employ2){
	// Adds two objects of class Employee passed as 
	// function arguments and saves them as the calling object's data member values. 

/* 
Add the total hours for objects 1, 2, and 3.
Add the salaries for each object.
Add the total overtime hours.
*/

/*
Then display the information below.  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% EMPLOYEE SUMMARY DATA%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Total Employee Salaries ..... = 576.43
%%%% Total Employee Hours ........ = 108
%%%% Total Overtime Hours......... = 5
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/
	} // End of function



Is This A Good Question/Topic? 0
  • +

Replies To: Program that uses Classes and Data Objects

#2 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Program that uses Classes and Data Objects

Posted 05 March 2010 - 10:48 AM

I could be completely wrong, but don't you need a private class to!

Check a lot of my threads, everyone of them deals with classes!

http://www.dreaminco...ic=158384&st=20
Was This Post Helpful? 0
  • +
  • -

#3 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Program that uses Classes and Data Objects

Posted 05 March 2010 - 10:50 AM

Fixing your code would be a pain, since there are a lot of errors, and you won't really understand anything. This link: http://www.cplusplus...torial/classes/ may help you understand how classes work.

This post has been edited by sarmanu: 05 March 2010 - 10:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 v8n3t  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 46
  • Joined: 08-January 09

Re: Program that uses Classes and Data Objects

Posted 05 March 2010 - 11:30 AM

tarheelfan_08:

I noticed on that link that in the code you used a #include "CarClass.h".

Is this just a new source file containing your class information and then you saved the .cpp file as carclass.h and then your linking it at the top of your main source? If so, can I use this in my project? It seems to manage external files accordingly.

sarmanu,

I have read that same page over and over again. Its not new to me, just understanding this is hard. For example, I know my main class is EmployeeClass, and the object or data inside the class which should store my employee names is "EmployeeName".

So I need to reference it like such:

EmployeeClass.EmployeeName () ? This is where I am confused.
Was This Post Helpful? 0
  • +
  • -

#5 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Program that uses Classes and Data Objects

Posted 05 March 2010 - 11:35 AM

Yes it is a new file!
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Program that uses Classes and Data Objects

Posted 05 March 2010 - 11:38 AM

basic syntax
#include <iostream>

using namespace std;

class foo
{
// from here on, all the members are private unless declared otherwise (cannot be accessed in main)
	int x; //a variable member
	
	//now everything from here down can be accessed in main()
	public:
	foo(int something)
	:x(something)
	{} //when i first initialize my object in main, all call this constructor which sets x equal to what ever i pass in
	
	void output(void); //since my variable is private i need a public function to display it;
}

void foo::output(void) //look at syntax when declaring member functions
{
	cout<<x<<endl; //output x from foo
}

int main()
{
	foo myObject(10); //i called the constructor and passed in a number to be set to foo::x
	myObject.output(); //output x from my foo object;
	
	cin.ignore();
	cin.get();
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#7 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Program that uses Classes and Data Objects

Posted 05 March 2010 - 11:41 AM

The class instantation (creation) is done by writing the name of the class, followed by an user-given identifier for that class. Example: if I have a class X, I'd instantiate it like this:
X a; // X is the name of the class, followed by a name given by the user
     // I have chosen the name of my class, "a"


Great, you have learned how to instantiate a class! The next thing you know, is how to access member functions/variables of that class. This is easy! You write the name of the class object (that you given to your object), followed by a period (.), then, by the name of the function/variable. Example: if I have a class X, with a member variable called "Val", I would access the variable like this:
X a; // We need to instantiate the class
a.Val = 34; // This is how I would access the variable, and assign something to it



In your case, I'd instantiate your class like this:
EmployeeClass EmployeeName; // This is the name we gave to your class!


Now, I would use EmployeeName.MemberFunction/Variable to access classe's member functions/variables.

This post has been edited by sarmanu: 05 March 2010 - 11:42 AM

Was This Post Helpful? 0
  • +
  • -

#8 Guest_Matthew Balthrop*


Reputation:

Re: Program that uses Classes and Data Objects

Posted 05 March 2010 - 12:30 PM

Here is one way of fixing the portion of code here, use this as an example of how you might perform the remaining calculations.

-alias

#include <iostream> 
#include <string> 
#include <iomanip> 
 
using namespace std; 
 
// 
//CLASS DECLARATION SECTION 
// 
class EmployeeClass { 
public: 
        void ImplementCalculations(string EmployeeName, double hours, double wage); 
        void DisplayEmployInformation(void); 
        void Addsomethingup (EmployeeClass, EmployeeClass, EmployeeClass); 
        string EmployeeName ; 
        int hours ; 
        float wage ; 
        float basepay ; 
        int overtime_hours ; 
        float overtime_pay ; 
        float overtime_extra ; 
        float iTotal_salaries ; 
        float iIndividualSalary ; 
        int iTotal_hours ; 
        int iTotal_OvertimeHours ; 
}; 
 
int main() 
{       system("cls");  
 
        cout << "\nWelcome to the Employee Pay Center\n\n" endl; 
 
EmployeeClass firstObject;                 //Initializing an object of the class EmployeeClass

string name;                                //Creating variables to capture user input
int hours;
int wage; 

 
cout << "Enter the employee name:"<<endl; 

cin >>name;                                 //Accepting user input for the string 'name'

firstObject.EmployeeName(name);             //Using the object of EmployeeClass
                                            //to initialize a variable of the class 
                                            //to our user input
cout << "Enter the hours worked:"<<endl;    

cin >> hours; 

firstObject.hours(hours);

cout << "Enter his or her hourly wage:"<<endl; 

cin >> wage; 

firstObject.wage(wage);



Was This Post Helpful? 0

#9 v8n3t  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 46
  • Joined: 08-January 09

Re: Program that uses Classes and Data Objects

Posted 05 March 2010 - 03:25 PM

Hello again everyone,

So after reading all the responses here the answer was clear. I needed to define my strings for my objects, then define my objects of the class, then I could use them.

Now with the help of my instructor, he supplied some more code to help make things functional. I am getting some odd compiling errors that I am unaware of. The new code is listed below:

Here are the compile errors:

In function `int main()':
33: error: expected `;' before "endl"
40: error: expected `;' before "endl"
42: error: expected `;' before "endl"
44: error: expected `;' before "endl"
47: error: expected `;' before "endl"
49: error: expected `;' before "endl"
51: error: expected `;' before "endl"
54: error: expected `;' before "endl"
56: error: expected `;' before "endl"
58: error: expected `;' before "endl"
62: error: expected primary-expression before "double"
62: error: expected primary-expression before "double"
63: error: expected primary-expression before "double"
63: error: expected primary-expression before "double"
64: error: expected primary-expression before "double"
64: error: expected primary-expression before "double"
65: error: redeclaration of `EmployeeClass Employeebase'
38: error: `EmployeeClass Employeebase' previously declared here
66: error: `employeeone' undeclared (first use this function)
66: error: (Each undeclared identifier is reported only once for each function it appears in.)
66: error: `employeetwo' undeclared (first use this function)
66: error: `employeethree' undeclared (first use this function)
: In member function `void EmployeeClass::ImplementCalculations(std::string, double, double)':
79: warning: converting to `int' from `double'

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class EmployeeClass {
public:
	void ImplementCalculations(string EmployeeName, double hours, double wage);
	void DisplayEmployInformation(void);
	void Addsomethingup (EmployeeClass, EmployeeClass, EmployeeClass);
	string EmployeeName ;
	string Employeeone;
	string Employeetwo;
	string Employeethree;
	string Employeebase;
	int hours ;
	float wage ;
	float basepay ;
   	int overtime_hours ;
	float overtime_pay ;
	float overtime_extra ;
	float iTotal_salaries ;
	float iIndividualSalary ;
	int iTotal_hours ;
	int iTotal_OvertimeHours ;
};

int main()
{	
system("cls"); 

cout << "\nWelcome to the Employee Pay Center\n\n" endl;

EmployeeClass Employeeone;
EmployeeClass Employeetwo;
EmployeeClass Employeethree;
EmployeeClass Employeebase;

cout << "Enter the employee name:" endl;
cin >> Employeeone.EmployeeName;
cout << "Enter the hours worked:" endl;
cin >> Employeeone.hours;
cout << "Enter his or her hourly wage:" endl;
cin >> Employeeone.wage;

cout << "Enter the employee name:" endl;
cin >> Employeetwo.EmployeeName;
cout << "Enter the hours worked:" endl;
cin >> Employeetwo.hours;
cout << "Enter his or her hourly wage:" endl;
cin >> Employeetwo.wage;

cout << "Enter the employee name:" endl;
cin >> Employeethree.EmployeeName;
cout << "Enter the hours worked:" endl;
cin >> Employeethree.hours;
cout << "Enter his or her hourly wage:" endl;
cin >> Employeethree.wage;


Employeeone.ImplementCalculations(Employeeone.EmployeeName, double hours, double wage);
Employeetwo.ImplementCalculations(Employeetwo.EmployeeName, double hours, double wage);
Employeethree.ImplementCalculations(Employeethree.EmployeeName, double hours, double wage);
EmployeeClass Employeebase ;
Employeebase.Addsomethingup(employeeone,employeetwo,employeethree ) ;
}
// END MAIN


void EmployeeClass::ImplementCalculations (string EmployeeName, double hours, double wage){
overtime_hours=0;
overtime_pay=0;
overtime_extra=0;
 
            if (hours > 40)
            {                  
                basepay=40*wage ;                                               //This is the hourly wage times the hours.
                overtime_hours=hours-40;                   //Overtime hours (hours minus 40 hours)
                overtime_pay=wage*1.5 ;                      //Overtime pay is the hourly wage * 1.5
                overtime_extra=overtime_hours*overtime_pay; //Overtime pay over the standard pay
                iIndividualSalary=overtime_extra+basepay;       //This is the total salary for the employee.
                DisplayEmployInformation () ;                                             //Function call to display employee details
            }   // if (hours > 40)
            else
            {  
                basepay=hours*wage ;                         //calculate basepay
                iIndividualSalary=basepay ;                 //set basepay to current salary
                DisplayEmployInformation () ;                             //Function call to display employee details
            } // End of the else
 
} //End of Primary Function
 
void EmployeeClass::DisplayEmployInformation (){
// This function displays all the Employ output information
 
                cout << "\n\n";
                cout << "Employee Name ............. = " << EmployeeName << endl ;
                cout << setprecision(2) << fixed << showpoint <<"Base Pay .................. = " << basepay << endl ;
                cout << "Hours in Overtime ......... = " << overtime_hours << endl ;
                cout << setprecision(2) << fixed << showpoint << "Overtime Pay Amount........ = " << overtime_extra << endl ;
                cout << setprecision(2) << fixed << showpoint << "Total Pay ................. = " << iIndividualSalary << endl;
}
 
void EmployeeClass::Addsomethingup (EmployeeClass Employ1, EmployeeClass  Employ2, EmployeeClass Employ3){
            // Adds three objects of class Employee passed as
            // function arguments and save them as the calling object's data member values.
 
            iTotal_hours = Employ1.hours + Employ2.hours + Employ3.hours;
            iTotal_salaries = Employ1.iIndividualSalary + Employ2.iIndividualSalary + Employ3.iIndividualSalary ;
            iTotal_OvertimeHours = Employ1.overtime_hours + Employ2.overtime_hours + Employ3.overtime_hours ;
 
            cout << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%";
            cout << "\n%%%% EMPLOYEE SUMMARY DATA%%%%%%%%%%%%%%%%%%%%%%%";
            cout << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%";
            cout << setprecision(2) << fixed << showpoint << "\n%%%% Total Employee Salaries ..... = " << iTotal_salaries ;
            cout << "\n%%%% Total Employee Hours ........ = " << iTotal_hours ;
            cout << "\n%%%% Total Overtime Hours......... = " << iTotal_OvertimeHours ;
            cout << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%";
            cout << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
 
            }


This post has been edited by v8n3t: 05 March 2010 - 03:31 PM

Was This Post Helpful? 0
  • +
  • -

#10 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Program that uses Classes and Data Objects

Posted 05 March 2010 - 03:46 PM

Okay I got this code to compile and run.

This is what needed to be done:

- Look at your cout statements. You have no << operators before your endl's

At the very end of main you were calling your functions incorrectly. Call them like this:
Employeeone.ImplementCalculations(Employeeone.EmployeeName, Employeeone.hours, Employeeone.wage);
Employeetwo.ImplementCalculations(Employeetwo.EmployeeName, Employeetwo.hours, Employeetwo.wage);
Employeethree.ImplementCalculations(Employeethree.EmployeeName, Employeethree.hours, Employeethree.wage);


The last error was from the two lines just below those function calls. A variable employee base is already declared within your class so you can't use that name.

Simply change the name of the class you're trying to declare or comment those two lines out.

If you change the name you have to remember that C++ is case sensitive and capitalize the first letter of emloyeeone, two and three.

This post has been edited by eker676: 05 March 2010 - 03:48 PM

Was This Post Helpful? 1
  • +
  • -

#11 v8n3t  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 46
  • Joined: 08-January 09

Re: Program that uses Classes and Data Objects

Posted 05 March 2010 - 05:12 PM

Thank you very much for helping me fix the calling methods.

I fixed the cout with no over-loaders at the end shortly after posting my last post.

I had no idea I was calling them incorrectly tho. Thank you for helping me fix that.

The last error I get is with the compiling is this:

77 C:\Users\v8n3t\Phoenix\IT218\Final\final.cpp [Warning] converting to `int' from `double'

I read that this is a warning with converting a floating point int to a double. I did not see how to correct this, as people are saying to just comment it out. If I were to comment it out in my program, it would cause more harm and not allow for proper calculations.

Thoughts?
Was This Post Helpful? 0
  • +
  • -

#12 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Program that uses Classes and Data Objects

Posted 05 March 2010 - 07:23 PM

you kinda of defeat the purpose of your class when you declare all your workers in the same container. The point of a class is to have a set container in which you can reuse for multiple instances. So I would create a struct (same as a class except by default members are public) that just holds the data for one single employee, then create an array of your structs for how ever many employees you have. Then do your calculations.

take a look
#include <iostream>
#include <string>

using namespace std;

struct Employee
{
	string name;
	int hours;
	double wage;

	Employee(void)
		:hours(0),wage(0.0)
	{}
};


int main()
{
	Employee *workers = NULL;
	int amount;
	cout<<"\t EMPLOYEE PAY CENTER"<<endl<<endl;
	cout<<"ENTER NUMBER OF EMPLOYEES : ";
	cin>>amount;

	try
	{
		workers = new Employee[amount];
	}
	catch(bad_alloc &obj)
	{
		cerr<<"BAD ALLOCATION : "<<obj.what()<<endl;
	}


	for(int i=0; i<amount; i++)
	{
		cout<<"EMPLOYEE #"<<i+1<<endl;
		cout<<"NAME : ";
		cin>>workers[i].name;
		cout<<"HOURS : ";
		cin>>workers[i].hours;
		cout<<"WAGE : ";
		cin>>workers[i].wage;
	}

        delete[] workers;
	cin.ignore();
	cin.get();
	return 0;
}


This post has been edited by ImaSexy: 05 March 2010 - 07:24 PM

Was This Post Helpful? 0
  • +
  • -

#13 v8n3t  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 46
  • Joined: 08-January 09

Re: Program that uses Classes and Data Objects

Posted 06 March 2010 - 12:32 AM

bump,

any idea how to fix this problem?

77 C:\Users\v8n3t\Phoenix\IT218\Final\final.cpp [Warning] converting to `int' from `double'
Was This Post Helpful? 0
  • +
  • -

#14 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Program that uses Classes and Data Objects

Posted 06 March 2010 - 12:39 AM

 overtime_hours=hours-40;


overtime_hours is an integer variable. hours is a double. Assigning double to an integer, will result in decimal point truncation. Declare overtime_hours as double.
As for here:
if (hours > 40)


My compiler doesn't complain about this. Your does. It seems that it doesn't like that 40, which is an integer, and hours is a double. You could remove the warning by doing this:
if (hours > 40.0)


or use typecast:
if (hours > double(40))


This post has been edited by sarmanu: 06 March 2010 - 12:40 AM

Was This Post Helpful? 0
  • +
  • -

#15 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: Program that uses Classes and Data Objects

Posted 06 March 2010 - 08:06 AM

Though I agree with your method, I think he is being required to create a class for this project. IMO, they're both data structures and you should use the one that works best for your project. However, if the intent of this project is to utilize OOP and learn about classes then I understand.

-alias
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2