Simple Class

This is a discussion on Simple Class within the C++ Programming forums, part of the General Programming Boards category; Umm, I have tried writing a simple class that returns a person's name, but I am not exactly sure why ...

  1. #1
    Unregistered
    Guest

    Simple Class

    Umm, I have tried writing a simple class that returns a person's name, but I am not exactly sure why it doesn't work. I have tried fiddling with it for about 20 mins but I'm going no where, please help.

    #include <iostream.h>

    class name
    {
    public:
    char getname(){return itsname};
    void setname(name){itsname = name};
    private:
    char itsname[50];
    };

    int main()
    {
    name name;
    char name [50];
    cout << "\nWhat is your name?";
    cin >> name;
    cout << name.getname;
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    first i believe you can't have an object or variable with the same name as the class.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Perhaps this would work better:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Name 
    { 
      public: 
        char *getname() { return itsname; } 
        void setname(char *name) { strncpy ( itsname, name, 50 ); }
      private: 
        char itsname[50]; 
    };
    
    int main() 
    { 
      Name name; 
      char szName[50]; 
      cout << "What is your name?"<<endl; 
      cin.getline ( szName, 50 );
      name.setname ( szName );
      cout<<"Your name is "<< name.getname() <<endl; 
      return EXIT_SUCCESS; 
    }
    And you can also do the same thing a different way by using the input entered by the user as the constructor argument for the object. You can then get rid of the setname function.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Name 
    { 
      public:
        Name ( char *input ) : itsname ( input ) {}
        char *getname() { return itsname; }
      private: 
        char *itsname; 
    };
    
    int main() 
    {  
      char szName[50]; 
      cout << "What is your name?"<<endl; 
      cin.getline ( szName, 50 );
      Name *name = new Name ( szName );
      cout<<"Your name is "<< name->getname() <<endl; 
      return 0; 
    }
    -Prelude
    Last edited by Prelude; 03-12-2002 at 04:05 PM.
    My best code is written with the delete key.

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    lol...

    *Semicolons go after program statements, not function definitions
    *You can't return an array from a function with return type single char
    *You can't set an array to a type of nothing... -> wtf is this: void setname(name){itsname = name}; ??
    *You can't redefine name 3 times- the ident of a class, the ident of a character array, and an instance of a class!
    *You can't access a function like a member variable- you have to express arguments, even if the function takes none ()

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >lol...
    Inappropriate, if you don't have anything better to do than laugh at people with less experience then go elsewhere.

    >*Semicolons go after program statements, not function definitions
    Though they cause no harm since a floating semicolon is considered an empty statement that does nothing.

    >*You can't return an array from a function with return type single char
    No you can't, but you could also mention that the proper return type for an array is char *.

    >*You can't set an array to a type of nothing... -> wtf is this: void setname(name){itsname = name}; ??
    [sarcasm]
    Oh yes, I'm sure these are two mistakes you've never made before.
    [/sarcasm]

    >*You can't redefine name 3 times- the ident of a class, the ident of a character array, and an instance of a class!
    Finally, a useful answer.

    >*You can't access a function like a member variable- you have to express arguments, even if the function takes none ()
    I'll assume the line you were talking about was
    cout << name.getname;
    since you didn't specify where the error was, most useful.

    We're here to help, not to mock people who are just learning the language. If you feel that you are better than they are then think about all of the people who helped you out when you were just starting while they could have just as easily laughed at you.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    I second that Prelude

    If you don't have anything nice or helpful to say rmullen3 you are probably better of not saying it .

  7. #7
    Unregistered
    Guest
    Thanks for the help prelude!!!! Everything makes sense and you helped more than you can imagine!!! Now I can continue learning. I was wondering how many classes are in the Microsoft Foundation Class...I just started reading up on some Visual C++ and I guess MFC is everything from the Windows application programming interface but put into classes (so you don't have to remember all of the thousands of functions from the API). If you don't know then I will just post a new thread, you have helped me enough already.


    Mullen why do you bother posting if you use "lol" and "wtf is this..." in your replies? I don't care how bad my code may seem, you should be encouraging beginners, not the opposite...and if you have a big ego because you think you are so much better at programming you are ignorant, or have forgotten of, the fact that you were once in the same place as I am right now...
    Oh hey bigshot, I forgot to tell you, when you put function definitions after what you call the "program statements", but I think you meant class declarations, it is called an inline function and is perfectly legal...look at prelude's first solution to my program........

  8. #8
    Unregistered
    Guest

    Exclamation One more thing!

    I was just wondering, what does "using namespace std" mean? You have that included in both of your versions of my code, but I did not include nor have I heard of it before.


    Thanks for defending me you guys, mullen could cause some serious damage to younger people trying to learn C++.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I was wondering how many classes are in the Microsoft Foundation Class
    Several hundred I believe, but I'm not too familiar with MFC.

    >what does "using namespace std" mean?
    Namespaces were added to C++ to help in encapsulation, if you currently use the old C++ header <iostream.h> then you probably aren't familiar with namespaces. The current C++ standard recommends the use of the header file <iostream> and using namepace std; simply makes programming a bit easier for the smaller stuff. For example:
    Code:
    #include <iostream.h>
    
    int main() 
    {  
      cout<<"Something"; // This works
      return 0;
    }
    Code:
    #include <iostream>
    
    int main() 
    {  
      cout<<"Something"; // This doesn't, cout is undeclared
      std :: cout<<"Something"; // This works
      /* You have to use the scope operator to tell the compiler
      ** where to find cout when you use new C++
      */
      return 0;
    }
    Code:
    #include <iostream>
    using namespace std;
    /* The using command will tell the compiler that
    ** the default namespace is whatever you give it.
    ** In this case, the standard namespace
    */
    
    int main() 
    {  
      cout<<"Something"; // This works, the default namespace is std
      return 0;
    }
    >Thanks for defending me you guys
    You'll find that most of us are here to help and any flamers who show up trying to cause trouble will get shredded.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Unregistered
    Guest
    Okey dokey, this all sounds good.

    I wanted to ask a couple of quick questions about the simple class problem. The second solution you to my code was this:

    #include <string.h>


    class Name
    {
    public:
    Name (char *input) : itsname ( input ) {}
    char *getname () {return itsname; }
    private:
    char *itsname;
    };

    int main()
    {
    char szName[50];
    cout << "What is your name?";
    cin.getline ( szName, 50);
    Name *name = new Name(szName);

    cout << "Your name is " << name->getname() << endl;
    return 0;
    }

    I like this one better because you don't need the setname function, instead the name is "set" when the constructor is called and the program flows along nicely. What I don't quite get is you never actually assign itsname to input using an equals symbol:

    Name (char *input) : itsname ( input ) {}

    I have never seen this done before, why did you write it like that? Is that just another way of assigning one string to another?

    Also, how come you only used one ":" in the constructor? I have not seen that done before either.

    Alright this is my last post for the night, Thanks a bunch!!! I don't know how to thank you!!! I have printed all the examples you have showed me and I put them in a folder so I can take another look at them tonight before bed. THANK YOU!!! I will try to help others when I get as good as you Prelude.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Name (char *input) : itsname ( input ) {}
    That's an initializer list for the class constructor, it tends to be more efficient than something like
    Name ( char *input ) { itsname = input; }
    but the syntax trips some people up. To use an initializer list simply follow the constructor argument list with a colon and then pretend each class member is a function with the arguments to that function being the arguments from the constructor.
    Code:
    Name ( char *input ) // One argument, input
    :                    // An initializer list follows
    itsname ( input )    // Assign input to itsname
    {}                   // Empty body for the constructor
    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Other people's misinterpretation of "lol" is not a concern of mine.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Other people's misinterpretation of "lol" is not a concern of mine.
    Then don't bother posting because just about every time they'll think you're making fun of them.

    Just out of curiosity, how do you interpret your post?

    -Prelude
    My best code is written with the delete key.

  14. #14
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    "
    LINEAR OBJECT-PROGRAMMING LESSON...

    *Semicolons go after program statements, not function definitions
    *You can't return an array from a function with return type single char
    *You can't set an array to a type of nothing... -> wtf is this: void setname(name){itsname = name}; ??
    *You can't redefine name 3 times- the ident of a class, the ident of a character array, and an instance of a class!
    *You can't access a function like a member variable- you have to express arguments, even if the function takes none ()

    "


  15. #15
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210

    Question Hmm...

    funny I don't recall my instructors ever using "wtf" in their lesson handouts.
    "The mind, like a parachute, only functions when open."

Page 1 of 2 12 LastLast
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. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  4. Replies: 8
    Last Post: 10-02-2005, 01:27 AM
  5. Errors in a simple hash table class.
    By TheSquid in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2005, 04:49 AM

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