Advertisement

passing arrays into classes..

Started by April 22, 2002 09:38 PM
1 comment, last by code_fx 22 years, 6 months ago
I am having some trouble in this catagory. lets say that I want to pass my name into a class class Names { ....char name[31], .........id*; ....public: ......void Get_Name(); ......void Display_Name(); ......Names(char, char*); }; etc would this be in the ballpark or even close and could some on give me examples of the accessor functions... what part does what especially if there are string copy commands...
class Names{   char name[31], id*;public:   void GetName( char* buffer ) const;   void DisplayName( FILE* fd );   Names(char* _name, char* _id);};void Names::GetName( char* buffer ){  strcpy( buffer, name );};void Names::DisplayName( FILE* fd = stdout ){  fprintf( fd, "%s", name );}Names::Names( char* _name, char* _id ){  strncpy( name, _name, 31 );  id = _id;} 


Of course, it is much better with C++ strings (hey, if you''re using C++...)

using namespace std;class Names{   string name, id;public:   string GetName() const;   void DisplayName( ostream& os );   Names(const string& _name, const string& id );};string Names::GetName() const{  return name;}void DisplayName( ostream& os ){  os << name;}Names::Names( const string& _name, const string& id ): name( _name ), id( _id ){} 


[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
You could then overload operators for your objects, such as
name =+ name2
would concant the two strings.

If at first you don''t succeed, use profanity and try, try again.
If at first you don't succeed, use profanity and try, try again.

This topic is closed to new replies.

Advertisement