class structure

This is a discussion on class structure within the C++ Programming forums, part of the General Programming Boards category; I am writing a program which asks for a first and last name and puts them in a class structure. ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    13

    Question 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:perator==( const Name &n ) const
    {
    if (n.first != n.last )
    return false;

    if ( n.first == n.last )
    return true;
    }

    Name:perator +=( 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.
    Last edited by collegegal; 03-03-2002 at 10:47 AM.

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    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.

  3. #3
    Unregistered
    Guest
    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; 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Structure vs. Class
    By bobbelPoP in forum C++ Programming
    Replies: 4
    Last Post: 07-09-2008, 10:44 AM
  3. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  4. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 09:54 AM
  5. Converting a C structure into a C++ class
    By deadpoet in forum C++ Programming
    Replies: 7
    Last Post: 01-07-2004, 02:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21