This is my solution for an exercise from a book. It's a simple Golf class with member variables fullname and handicap. The constructor sets the variables to the provided name and handicap using values passed as arguments. The setgolf() function solicits name and handicap from user, creates a temporary Golf object and assigns it to the invoking object.
Is this a proper solution? I'm particularly interested in the setgolf() method.
golf.h:
#include <string>
class Golf
{
private:
std::string fullname;
int handicap;
public:
Golf();
Golf(const std::string name, int hc);
int setgolf();
void sethandicap(int hc);
void showgolf();
};
golf.cpp:
#include "golf.h"
#include <cstring>
#include <iostream>
Golf::Golf(const std::string name, int hc)
{
fullname = name;
handicap = hc;
}
int Golf::setgolf()
{
std::string name;
int hc;
std::cout << "Enter the name: ";
getline(cin, name);
if (name == "")
return 0;
std::cout << "Enter the handicap: ";
std::cin >> hc;
*this = Golf(name, hc);
return 1;
}