13 Replies - 3974 Views - Last Post: 20 November 2011 - 02:02 PM Rate Topic: -----

#1 blondielox  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 24-August 11

Struct and class ...not comprehending ?

Posted 19 November 2011 - 05:44 PM

My assignment is:


• Use the example on p. 333 to create your own structure related to an overtime pay calculator and define the variables needed within your structure to do those calculations.

• Define and create a base class using the same variables that may serve the same function as the structure.


This is the example:

BOOK Novel =
{
“Paneless Programming”, // Initial value for Title
“I.C. Fingers”, // Initial value for Author
“Gutter Press”, // Initial value for Publisher
1981 // Initial value for Year
};


I am reading the material but not really comprehending it. Can some one help me out here? In the file am I supposed to have a struct and a class or does the class replace the struct?

I started to create a struct but that is as far as I got.
#include <iostream>

using namespace std;
using std::cout;
using std::endl;
using std::cin;


struct Overtime
{
       double wage;
       double hours;
       double otpay;
       
};



Is This A Good Question/Topic? 0
  • +

Replies To: Struct and class ...not comprehending ?

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Struct and class ...not comprehending ?

Posted 19 November 2011 - 06:03 PM

> I am reading the material but not really comprehending it. Can some one help me out here? In the file am I supposed to have a struct and a class or does the class replace the struct?

Both a class and a struct, but they both serve the same purpose.

I'm don't see the purpose of this exercise. The only difference between classes and structs is that classes have private access and private inheritance by default.
Was This Post Helpful? 1
  • +
  • -

#3 blondielox  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 24-August 11

Re: Struct and class ...not comprehending ?

Posted 19 November 2011 - 06:06 PM

So what you are saying is I need to build a struct and add the function to perform the calculation as well as create a class that calls the function? This is where I am getting totally confused.
Was This Post Helpful? 0
  • +
  • -

#4 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 247
  • Joined: 21-June 11

Re: Struct and class ...not comprehending ?

Posted 19 November 2011 - 06:28 PM

The only real difference between a struct and a class is the public, private, and protected declarations.

Taking a 'struct' for instance:
struct Thing {
    int X;
};



The integer X is declared with as a public variable. Structs are classes where *everything* is public.

Now for a class, the reason you'd use this is if you wanted a private or protected variable, like so:

class Thing {
    public:
        int getX() {
            return X;
        }
    private:
        int X;



So the tl;dr version is that structs are just classes where every function and variable are public.
Was This Post Helpful? 0
  • +
  • -

#5 blondielox  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 24-August 11

Re: Struct and class ...not comprehending ?

Posted 19 November 2011 - 06:31 PM

I was just looking at my assignments and the points they carry. This one is only worth 20 points so I may just skip it for now.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2929
  • View blog
  • Posts: 10,132
  • Joined: 08-August 08

Re: Struct and class ...not comprehending ?

Posted 19 November 2011 - 06:45 PM

Things only get harder from here if you don't understand structs and classes. They're not going to go away.
Was This Post Helpful? 0
  • +
  • -

#7 blondielox  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 24-August 11

Re: Struct and class ...not comprehending ?

Posted 19 November 2011 - 07:38 PM

I understand that. I have another assignment that carries more weight that I have to work on and then I can come back to this. Even if I don't get a good grade on it for it being late at least I will have worked on it. And in all honesty, I have already figured out that programming is definitely not my forte however this is the last class before I get my degree so being that I have invested all this time and money on school I need to at least pass the class and understand as much as I can before I decide which direction I want to take from here.
Was This Post Helpful? 0
  • +
  • -

#8 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Struct and class ...not comprehending ?

Posted 19 November 2011 - 08:05 PM

Start with a struct ...
struct Overtime 
{
   double hours;
   double rate;
   // constructor ...
   Overtime( double h = 0.0, double r = 0.0 )// defaults are zero
   {
      hours = h;
      rate = r;
   }
   double pay() {  return hours * rate; }
}; 



Note: you do not need to store the pay in your struct as you can always calculate the pay from the rate * hours ... which the function pay() provides ... See below ...

Now in your program you can ...
Overtime ot1( 10.5, 20.25 ); // construct first overtime obj with values ...
cout << ot1.pay() << endl; // print overtime pay for this obj
Overtime ot2; // construct ot2 with zero hours and rate ...
ot2.hours = 8.25; // set hours for ot2 obj
ot2.rate = 15.5; // set overtime rate ...
cout << ot2.pay() << endl; // print overtime pay for this obj

You may like to see here ...
http://developers-heaven.net/forum/index.php/topic,127.msg270.html#msg270

This post has been edited by David W: 19 November 2011 - 08:27 PM

Was This Post Helpful? 0
  • +
  • -

#9 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: Struct and class ...not comprehending ?

Posted 19 November 2011 - 08:22 PM

View Postblondielox, on 20 November 2011 - 04:06 AM, said:

So what you are saying is I need to build a struct and add the function to perform the calculation as well as create a class that calls the function? This is where I am getting totally confused.


From what you have told us it looks as if you create a struct and then a class with the same variables. You do not need to create any functions.


Quote

Define and create a base class using the same variables that may serve the same function as the structure.


I would say the word function here means purpose.

So :

Quote

Define and create a base class using the same variables that may serve the same purpose as the structure.

Was This Post Helpful? 0
  • +
  • -

#10 blondielox  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 24-August 11

Re: Struct and class ...not comprehending ?

Posted 19 November 2011 - 08:27 PM

David W - I will check out that link tomorrow when I am more awake...lol. On my way to bed now.....

#define - I see what you are saying.

Been a long day...had to be at work at 6 this morning after working major overtime all week and am just a little exhausted. Hopefully a good nights sleep will help me to get back on track in the morning.
Was This Post Helpful? 0
  • +
  • -

#11 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Struct and class ...not comprehending ?

Posted 19 November 2011 - 08:49 PM

Now with a class ...
class Overtime 
{
private:
   double hours;
   double rate;
public:
   // constructor ...
   Overtime( double h = 0.0, double r = 0.0 )// defaults are zero
   {
      hours = h;
      rate = r;
   }
   void set_hours( double h ) { hours = h; }
   void set_rate( double r ) { rate = r; }
   double pay() {  return hours * rate; }
}; 



Note: you do not need to store the pay in your class as you can always calculate the pay from the rate * hours ... which the function pay() provides ... See below ...

Now in your program you can ...
Overtime ot1( 10.5, 20.25 ); // construct first overtime obj with values ...
cout << ot1.pay() << endl; // print overtime pay for this obj
Overtime ot2; // construct ot2 with zero hours and rate ...
ot2.set_hours( 8.25 ); // set hours for ot2 obj
ot2.set_rate( 15.5 ); // set overtime rate ...
cout << ot2.pay() << endl; // print overtime pay for this obj

Note also you can construct an array of Overtime obj's ...
Overtime ary[ 5 ]; // construct an array of 5 obj's all initialized to zero
for( int j = 0; j < 5; ++j )
{ 
   cout << "enter hours: " << flush;
   double tmp;
   cin >> tmp;
   ary[ j ].set_hours( tmp );
   cout << "enter rate: " << flush;
   cin >> tmp;
   ary[ j ].set_rate( tmp );
}


This post has been edited by David W: 22 November 2011 - 10:46 AM

Was This Post Helpful? 0
  • +
  • -

#12 blondielox  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 24-August 11

Re: Struct and class ...not comprehending ?

Posted 20 November 2011 - 09:45 AM

Here is an overtimepay program that I did for another assignment. I am assuming I can use this and just make changes to fit the new assignment


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

using namespace std;

//Global Declarations of Variables
double iovertime_hours=0, iovertime_pay=0, iovertime_extra=0;
int ihours, iwage ;
string cname ;

int main ()
{
	//Enter Employee Information
	cout << "\n\nEnter the employee name = ";
	cin >> cname ;
	cout << "Enter the hours worked  = ";
	cin >> ihours;
	cout << "Enter his or her hourly wage = ";
	cin >> iwage ;

	// Determine if hours are greater than 40 
	if (ihours < 40) 
	{
		//Do Calculations
		iovertime_hours=ihours+40;
		iovertime_pay=iwage-1.5 ;
		iovertime_extra=iovertime_hours*iovertime_pay;

		// Display Employee Details
		cout << "\n\n";
		cout << "Employee Name ............. = " << cname << endl ;
		cout << "Base Pay .................. = " << iwage*40 << endl ;
		cout << "Hours in Overtime ......... = " << iovertime_hours << endl ;
		cout << "Overtime Pay Amout......... = " << iovertime_extra << endl ;
		cout << "Total Pay ................. = " << iovertime_extra+(40*iwage) << endl;
	}    
	else // Else hours are less than 40 hours
	{
		cout << "\n\n";
		cout << "Employee Name ............. = " << cname << endl ;
		cout << "Base Pay .................. = " << iwage*40 << endl ;
		cout << "Hours in Overtime ......... = " << iovertime_hours << endl ;
		cout << "Overtime Pay Amout......... = " << iovertime_extra << endl ;
		cout << "Total Pay ................. = " << iovertime_extra+(40*iwage) << endl;
	} // End of the primary if statement 

} //End of Int Main


Was This Post Helpful? 0
  • +
  • -

#13 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Struct and class ...not comprehending ?

Posted 20 November 2011 - 01:16 PM

Firstly ... you will need to fix your above program so that it does what it seems it is supposed to do ...

// overTime.cpp

#include <iostream>
#include <string>
//#include <iomanip> // not used below ...

using namespace std;

// NOTE: these could/should be DECLARED inside main ... 
// but if next step is to use a struct or a class ... 
// you will see how that avoids these Global variables ...
// Global Declarations of Variables 
double hours, hour_rate;
string name;

int main ()
{
	while( true ) // do this while loop forever until break ...
	{
		//Enter Employee Information
		cout << "Enter the employee name = ";
		getline( cin, name );
		cout << "Enter the hours worked  = ";
		cin >> hours;
		cout << "Enter hourly wage = ";
		cin >> hour_rate ;
		if( cin.good() )
		{
			cin.sync(); // flush cin stream ... 
			break; // ok ... good data entered ... so break out of this while loop NOW
		}
		// else 
		cout << "ERROR! \nYou entered non-numeric data for hours or hourly rate ...\n";
		cin.clear(); // clear all error flags ...
		cin.sync();
	}

	// Determine if hours are greater than 40 
	if( hours > 40 )  // you had < i.e. less than
	{
		//Do Calculations
		double overtime_hours = hours - 40; // you had +
		double overtime_rate = hour_rate * 1.5; // you had -
		double overtime_extra = overtime_hours * overtime_rate;

		// Display Employee Details
		cout << "\n\n";
		cout << "Employee Name ............. = " << name << endl ;
		cout << "Base Pay .................. = " << hour_rate * 40 << endl ;
		cout << "Hours in Overtime ......... = " << overtime_hours << endl ;
		cout << "Overtime Pay Amout......... = " << overtime_extra << endl ;
		cout << "Total Pay ................. = " << overtime_extra + (40 * hour_rate) << endl;
	}    
	else // Else hours are less than or equal to 40 hours
	{
		cout << "\n\n";
		cout << "Employee Name ............. = " << name << endl ;
		cout << "Total Pay ................. = " << (hours * hour_rate) << endl;
	} // End of the primary if statement 

	cout << "\nPress 'Enter' to continue/exit ... " << flush;
	cin.get(); // wait for enter key to be pressed ...

} //End of Int Main



This post has been edited by David W: 20 November 2011 - 01:19 PM

Was This Post Helpful? 0
  • +
  • -

#14 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Struct and class ...not comprehending ?

Posted 20 November 2011 - 02:02 PM

Here is the above ... using a struct ...

Note that the struct( or class ) can also contain functions( methods ) to do the tasks requested on the data stored in the struct ...

See what you can do to convert this to a class ...
Hint: if you use private data, you will need to have some 'set' methods in your class to set the private data fields ... OR ... one could code an overloaded >> to overload cin >> ot ... so that it would input all your data into an Overtime object ...

// overTimeStruct.cpp

#include <iostream>
#include <string>

using namespace std;

struct Overtime
{
	string name;
	double hours, hour_rate;
	void show_pay()
	{
		if( hours > 40 )  // you had < i.e. less than
		{
			//Do Calculations
			double overtime_hours = hours - 40; // you had +
			double overtime_rate = hour_rate * 1.5; // you had -
			double overtime_extra = overtime_hours * overtime_rate;

			// Display Employee Details
			cout << "\n\n";
			cout << "Employee Name ............. = " << name << endl ;
			cout << "Base Pay .................. = " << hour_rate * 40 << endl ;
			cout << "Hours in Overtime ......... = " << overtime_hours << endl ;
			cout << "Overtime Pay Amout......... = " << overtime_extra << endl ;
			cout << "Total Pay ................. = " << overtime_extra + (40 * hour_rate) << endl;
		}    
		else // Else hours are less than or equal to 40 hours
		{
			cout << "\n\n";
			cout << "Employee Name ............. = " << name << endl ;
			cout << "Total Pay ................. = " << (hours * hour_rate) << endl;
		} // End of the primary if statement 
	}

} ;



int main ()
{
	Overtime ot; // constuct an empty Overtime object ... ot

	// get values for each field in 'ot' struct ... in a loop, until good data entered
	while( true ) // do this while loop forever until break (after good data entered)
	{
		cout << "Enter the employee name  : " << flush; // ensure printed now
		getline( cin, ot.name );
		cout << "Enter the hours worked   : " << flush;
		cin >> ot.hours;
		cout << "Enter hourly rate of pay : " << flush;
		cin >> ot.hour_rate ;
		if( cin.good() )
		{
			cin.sync(); // flush cin stream ... 
			break; // ok ... good data entered ... so break out of this while loop NOW
		}
		// else 
		cout << "ERROR! \nYou entered non-numeric data for hours or hourly rate ...\n";
		cin.clear(); // clear all error flags ...
		cin.sync(); // flush cin stream ...
	}

	ot.show_pay();

	cout << "\nPress 'Enter' to continue/exit ... " << flush;
	cin.get(); // wait for enter key to be pressed ...

}



If you were to code an overloaded operator >> friend function for your class Overtime ... your 'main' could be coded very simply like this ...

int main ()
{
	do
	{
		Overtime ot; // constuct an empty Overtime object ... ot

		cin >> ot; // using overloaded >> as in friend function

		ot.show_pay();
	}
	while( more() );
}


Of course ... you would have to code a more() function that asks if you want more ... and then returns false if you answer 'n' (no) or true if ...

Hint 2: your overloaded operator >> friend function would contain all the code as per the while loop above ... and would start like below, placed maybe, as the first thing ... inside your class:

You would then also need to ...
#include <fstream> // re. istream objects

friend istream& operator >> ( istream& is, Overtime& ot )
{
   // code as in the above while loop goes here instead, only use ... is ... in the place of ... cin  ... as above

   // then finally ...
   return is;
}


This post has been edited by David W: 22 November 2011 - 10:40 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1