-
class structure
I am writing a program which asks for a first and last name and puts them in a class structure. I am haveing difficulities withthe arguments for the constructors. I don't understand what I need. Does anyone have any pointers for me? The code is :
class Name {
public:
Name( char *, char * );
Name();
~Name();
bool operator==( const Name &n ) const;
const Name operator+=( const Name &n );
char first[ 26 ];
char last[ 26 ];
friend ostream &operator<<( ostream &, const Name &n );
friend istream &operator>>( istream &, Name &n );
};
#endif
Name::Name(char *thefirst, char *thelast )
{
}
ostream &operator<<( ostream &output, const Name &n )
{
output << n.first << n.last;
return output;
}
istream &operator>>( istream &input, Name &n )
{
input >> setw( 26 ) >> n.first;
input >> setw( 26 ) >> n.last;
return input;
}
bool Name::operator==( const Name &n ) const
{
if (n.first != n.last )
return false;
if ( n.first == n.last )
return true;
}
Name::operator +=( char )
{
Name&last += Name&first;
return &last;
}
char main()
{
Name name;
cout << "Enter full name in the form of abcdefghijk abcdefghijk:\n";
cin >> name;
cout << " The name entered was: " << name << endl;
return 0;
}
Okay I have tried what acouple of your suggestions and it still won't work. I guess I just don't understand. My teacher says that I have to use the == and += operators, so that's why that part is there. I think I just don't get the argument part and then using the main to use the class.
-
what i see missing is this:
Code:
Name::Name(char *thefirst, char *thelast)
{
strcpy(first, thefirst);
strcpy(last, thelast);
}
I don't know what you are going to use total for, cant help u there. The class isn't exactly How I would do it. I would dynamically allocate memory for first, last. That way I wouldn't have the possability to run out of space for the names ("unlimited" length of name!)
Hope it helps.
-
Code:
class Name
{
//make the friend functions public, not private
friend ostream &operator<<( ostream &, const Name &n );
//the insertion operator doesn't make much sense in context of
//the member variables.
friend istream &operator>>( istream &, Name &n );
public:
//include a default constructor and a copy constructor. Neither
//will be provided by default if you declare the following
//constructor
Name( char *, char * );
~Name();
bool operator==( const Name &n ) const;
const Name operator+=( const Name &n );
char first[ 26 ];
char last[ 26 ];
char total[ 52 ];
};
//need to name the variables sent as parameters here. see post
//by ninebit
Name::Name(char *, char * )
{
}
ostream &operator<<( ostream &output, const Name &n )
{
output << n.first << n.last; //or output << total;
return output;
}
istream &operator>>( istream &input, Name &n )
{
input >> setw( 26 ) >> n.first;
input >> setw( 26 ) >> n.last;
return input;
}
bool Name:perator==( const Name &n ) const
{
//sinc first and last are null terminated strings use strcmp() to
//compare them, not == or !=
if (n.first != n.last )
return false;
if ( n.first == n.last )
return true;
}
//nee to name the parameter sent in and use :: rather than :
Name:perator +=( char )
{
//since first and last are null terminated strings, use strcat()
//rather than += to concatenate them
Name&last += Name&first;
return &last;
}
char main()
{
Name name; //declaring instance of class using default
//constructor, of which there is none
cout << "Enter full name in the form of abcdefghijk abcdefghijk:\n";
/* use getline to enter string that includes whitespace. place
initially in total. then parse total into two names and then
stcpy the two names into first and last respectively or use two
separate input lines to place data into first and last and then
strcat() the two to get total id name declared with default constructor or use getline() to read string with whitespace into dummy buffer, parse into two strings and declare name using two parameter constructor passing the two strings you developed by parsing
*/
cin >> name;
cout << " The name entered was: " << name << endl;
return 0;
}