Jay Rohrssen
Jay Rohrssen

Reputation: 243

class function with buffer as a parameter, confused

I have an exercise I need to complete for school that has me confused. I just need a hint at the right direction to take with this as it's not making sense to me.

Add a member function to the Employee class:

void Employee::format(char buffer[], int buffer_maxlength)

The member function should fill the buffer with the name and salary of the employee. Be sure not to overrun the buffer. It can hold buffer_maxlength characters, not counting the '\0' terminator.

What I don't get is what the parameter is that is passed to the function. Shouldn't the name be passed, then fed into the buffer? Or, shouldn't the function not take any parameters and populate a buffer? If the buffer is a parameter, how do i populate it?

Confused here.

Haven't started coding because I don't understand the exercise yet.

Don't need anyone to write the program for me, just need a hint as to what's going on here.

Thanks.

EDIT: Here is the code I have so far, it seems to work. The one thing I'm not sure about is the buffer overrun. Since I can't resize the buffer should I just make it a size that I know can't be overrun with the existing data? This seems inefficient, but not sure what else to do.

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string.h>
#pragma warning(disable : 4996) // had to include this for the strcpy function, not sure why

using namespace std;

/**
A basic employee class that is used in many examples
in the book "Computing Concepts with C++ Essentials"
*/
class Employee
{
public:
   /**
   Constructs an employee with empty name and no salary.
   */
Employee();
/**
  Constructs an employee with a given name and salary.
  @param employee_name the employee name
  @param initial_salary the initial salary
*/
Employee(string employee_name, double initial_salary);
/**
  Sets the salary of this employee.
  @param new_salary the new salary value
*/
void set_salary(double new_salary);
/**
  Gets the salary of this employee.
  @return the current salary
*/
double get_salary() const;
/**
  Gets the name of this employee.
  @return the employee name
*/
string get_name() const;

void format(char buffer[], int buffer_maxlength);

private:
   string name;
   double salary;
   char buffer;
};

Employee::Employee()
{  
   salary = 0;
}

Employee::Employee(string employee_name, double initial_salary)
{  
   name = employee_name;
   salary = initial_salary;
}

void Employee::set_salary(double new_salary)
{  
   salary = new_salary;
}

double Employee::get_salary() const
{  
   return salary;
}

string Employee::get_name() const
{  
   return name;
}

void Employee::format(char buffer[], int buffer_maxlength)
{   
string temp_name;
//string space = " ";
char terminator = '\0';
double input_salary = salary;   
string s;   
stringstream output_salary;   
output_salary << input_salary;   
s = output_salary.str();
temp_name = name.c_str() + s + terminator;
strcpy(buffer, temp_name.c_str());
cout << buffer << endl; 
}


int main()
{
const int BUFFER_SIZE = 100;
char input_buffer[BUFFER_SIZE];

string temp_string;
string space = " ";

Employee bob_buffer("Buffer, Bob", 100000);
bob_buffer.format(input_buffer, BUFFER_SIZE);   

system("pause");

return 0;
}

EDIT: Used strncpy instead of strcpy to protect against the overrun

Upvotes: 1

Views: 1786

Answers (3)

Josh Townzen
Josh Townzen

Reputation: 1348

What your instructor is asking for is a function that could be used as follows:

  1. A caller allocates a buffer of whatever size it chooses.
  2. The caller calls Employee::format, passing both the buffer and the buffer size.
  3. Employee::format fills the buffer with the employee name and salary, ensuring that it doesn't write past the end of the buffer.
  4. The caller now has a string representation of the Employee, which it can use for whatever it needs to do (printing to the console, saving to a file, displaying in a GUI widget, etc.).

Essentially, the buffer argument is a return value. Employee::format's sole responsibility is to write the name and salary to the buffer for the caller to use. With that in mind, here are some changes you might want to make:

  • You have a buffer member in Employee, which looks like it may have been added for format to make use of. This is unnecessary, since the caller is responsible for allocating the buffer. Additionally, the buffer argument in format hides the member of the same name, so that member never gets used anyways.
  • The cout << buffer << endl; line at the end of format prints the employee information to the console, but the caller may not want that. format should only write the employee information to the buffer.

Upvotes: 1

pb2q
pb2q

Reputation: 59617

If name and salary are class members, then you'll already have access to them in a member function: no need to pass them in. The buffer parameter passed is what you're filling with your function: a user of your Employee class will call the function, passing in a buffer to be filled with the result.

So your assignment is to define a part of the Employee class API, supplying a specific representation of the class internals, and to learn about buffer boundaries/string handling.

Upvotes: 1

Rango
Rango

Reputation: 1105

I guess your teacher is requiring you to format the name and salary information into the buffer, and make sure your formatted text dont overrun the buffer size.

The name and salary must be member variables of your employee class.

Upvotes: 0

Related Questions