Basic help with the class structure

This is a discussion on Basic help with the class structure within the C++ Programming forums, part of the General Programming Boards category; I'm failry new to C++ programming, and I'm at the point where I'm starting to learn classes. I'm using C++ ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    1

    Basic help with the class structure

    I'm failry new to C++ programming, and I'm at the point where I'm starting to learn classes. I'm using C++ for Dummies and various online tutorials, and I was trying to make a very basic program utilizing a class, since a lot of the examples I'm finding are fairly compplex (at least for me right now).
    Basically, I'm trying to prompt the user to enter their first and last name, then get them displayed both together, using a class. I know how this could be done using just arrays or strings and displaying each name separately, but it's my understanding that by using a class, you can sort of consolidate the info. If you could steer me in the right direction, I'd really appreciate it - this code is getting all kinds of compile errors.


    Code:
    #include <stdlib.h>
    #include <iostream.h>
    using namespace std;
    
    class testClass
    {
         public:
               char firstName[10];
               char lastName[12];
    };
    
    int main()
    {
          testClass tc;
          cout << "Enter your first name:\n";
          cin >> tc.firstName;
          cout << "Enter your last name:\n";
          cin >> tc.lastName;
          cout << "Here's what you entered:\n";
          cout << tc;
    
          cin.ignore();
          cin.get();
          return 0;
    }

    Also, I know if I wanted to, I could simply put


    Code:
    cout << tc.firstName << tc.lastName;

    and that would display the first and last name, but aren't you supposed to be able to display all components of a class with just one command? I thought that was the magic of a class.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    The magic of a class is being a type

    However, you can do that by defining a member function that returns a concatenated string of firstName and lastName.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Not sure what tutorial you're using, but <iostream.h> was deprecated long ago. And prefer <cstdlib> to <stdlib.h>.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Person
    {
    public:
      string firstName ;
      string lastName ;
    };
    
    ostream& operator <<(ostream &os, const Person& person)
    {
      os << person.firstName << " " << person.lastName ;
      return os;
    }
    
    int main()
    {
      Person tc;
      cout << "Enter your first name:\n";
      getline( cin, tc.firstName );
      cout << "Enter your last name:\n";
      getline( cin, tc.lastName );
      cout << "Here's what you entered:\n";
      cout << tc;
    
      cin.ignore();
      cin.get();
      return 0;
    }

  4. #4
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    yep as Dante Shamest noted
    Code:
    cout << tc;
    won't work because the program doesn't know what to output. Its like trying to:
    Code:
    tc + tc
    they compiler will have no idea what you are trying to add. You can either:

    1) Overload the << operator as Dante Shamest did so the compiler knows what to output in the class.

    2) Create a output or display function.

    Code:
    #include <stdlib.h>
    #include <iostream>
    using namespace std;
    
    class Person
    {
         public:
               char firstName[10];
               char lastName[12];
               void displayName()
    };
    
    void Person::displayName()
    {
      cout << firstName << " " << lastName;
    }
    
    int main()
    {
          Person tc;
          cout << "Enter your first name:\n";
          cin >> tc.firstName;
          cout << "Enter your last name:\n";
          cin >> tc.lastName;
          cout << "Here's what you entered:\n";
          tc.displayName();
    
          cin.ignore();
          cin.get();
          return 0;
    }

  5. #5
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    aren't you supposed to be able to display all components of a class with just one command? I thought that was the magic of a class.
    That's the magic of overloading operators.
    Code:
    ostream& operator<< (ostream& outs , const testClass& tC)
    {
      outs << tc.FirstName << " " << tc.LastName;
      return outs;
    }
    Along with Dante's suggestions, I'd go with std::strings instead of char arrays.
    There is a difference between tedious and difficult.

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, 08:18 PM
  2. structure vs. class
    By $l4xklynx in forum C++ Programming
    Replies: 7
    Last Post: 01-22-2009, 02:00 AM
  3. help with class structure assignment
    By Crcullen3916 in forum C++ Programming
    Replies: 4
    Last Post: 05-30-2007, 02:51 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM

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