Thread: Class !!

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    36

    Class !!

    How to WRITE

    A class Person in C++ with attributes name, date of birth and residence address. Data type for name and address should be char*

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Start by making a class, then name that class Person. Add member variables for name, date of birth and address with appropriate types.

    I would suggest using string instead of char*, since the C++ string class is easier to use and safer than C style strings that are stored with char *'s, especially in classes. If your "boss" requires char*, then that "boss" isn't teaching you very well, but at least you know what the types are supposed to be so you have one step accomplished.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    Sorry for so many questions, but I am newbie in this line...

    Can you help me with the code ?

    Or may be with an example of Class ?


    Quote Originally Posted by Daved View Post
    Start by making a class, then name that class Person. Add member variables for name, date of birth and address with appropriate types.

    I would suggest using string instead of char*, since the C++ string class is easier to use and safer than C style strings that are stored with char *'s, especially in classes. If your "boss" requires char*, then that "boss" isn't teaching you very well, but at least you know what the types are supposed to be so you have one step accomplished.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There are tons of examples of classes, just do some searching, or look in your C++ book or tutorial.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    Code:
    class Person
    {
    public:
    	Person();
    	char name();
    	double DOB();
    	char Address();
    	void Properties();
    };
    
    #endif

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    OK, but this class needs data members (a private section with them). What you have are member functions.

    Also, do you really think a single char would be enough for storing the name and address and that a double would be a suitable data-type for date of birth?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That's a good start. Do you know what the #endif is for? It's part of a header include guard. If that code is in a header, you are missing the rest. If it is not, then remove the #endif part.

    Your class has a constructor and four other functions. It does not have any member variables. You should probably change those four functions to variables.

    Your name and address use type char, but char holds only a single character. I'd strongly advise switching to std::string (and #include <string>) but if you are required to use char* then don't forget the *, or make them character arrays with a max size. If you aren't familiar with character arrays, time for another trip to the book/tutorial/search engine.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Also note that #endif is not part of the class, but is a pre-processor directive (it matches a #ifdef or similar somewhere above).

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    DOB is not a good member function name...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    Sorry ... Agreed...

    1) I will remove endif .or beter, may be add a header :-)
    2) How to change function to variables ?
    3) char name(); to std::string name(); with a header as #include <string>

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    3) is good, but it's still a function.

    Do you know the difference between a function declaration and a variable declaration?

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    function declaration and a variable declaration : No I guess

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    You should REALLY go look at the tutorials provided on this very site. It will walk you through the basics if you are up to a little reading and A LOT of practice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM