Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 30th, 2005, 7:27 AM   #1
AbalahDoon
Newbie
 
Join Date: Jun 2005
Posts: 2
Rep Power: 0 AbalahDoon is on a distinguished road
newb needs help with classes and char arrays

hey im trying to find out how to write a class, say class name, that stores a name inputed by the user using member function SetName() or something.

i need to be able to prompt the user for a name, use cin>>, the pass that to SetName() to change the name stored in the class (christ i hope that made sense, im a newb so im sorry).

anyway if you can help it would be greatly appreciated, please reply, perhaps give some example code.
or you can msg me on:

aim - cplusplusnewbie
or
msn - danielmcd_5005@hotmail.com
thanks alot
AbalahDoon is offline   Reply With Quote
Old Jun 30th, 2005, 7:45 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Defining such a class is simple. I realize you're new, but it appears that you haven't consulted any reference material at all. Check out a book, write your code, and ask for help when your "SetName ()" method doesn't work. Here's an example, but put some sweat of your own into it!
class className 
{
private:
   ...constructors/destructors, often default
   ....private members/methods/declarations
public:
   ...public members/methods/declarations
};
Nothing fancy there, you can expect to work your way up to inheritance, etc.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 30th, 2005, 9:15 AM   #3
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
Quote:
Originally Posted by DaWei
class className 
{
private:
   ...constructors/destructors, often default
   ....private members/methods/declarations
public:
   ...public members/methods/declarations
};
you want to make your constructors public, as you will not be able to instantiate the class otherwise.
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand."
- B. Russell

http://web.bryant.edu/~srk2
skuinders is offline   Reply With Quote
Old Jun 30th, 2005, 10:29 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Oops! I need to get my head out of where it's so dark :o . Here's an example, sorry to mislead you (slaps own wrist with a ruler just like the nuns used to do).
#include <iostream>
#include <string>

using namespace std;

class myClass
{
private:
    string myText;
public:
    myClass (){};
    myClass (string s)
    {
        myText = s;
    }
    void putText (string pT)
    {
        myText = pT;
    }
    string getText ()
    {
        return myText;
    }
};

int main (int argc, char* argv[])
{
    myClass textEntity;

    textEntity.putText ("This is my text entity");
    myClass* newEntity = new myClass ("This is the new text");

    cout << textEntity.getText () << endl 
         << newEntity->getText () << endl;
    return 0;
}
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 30th, 2005, 3:15 PM   #5
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
regardless of spelling, is "imputed" even a word?!?


You know, I generally prefer to list my attributes (private) first too, but every member of the crew at my new place of work unanimously agrees that the methods should be listed first (they are quite nitpicky when it comes to things like that.. sheesh). Not that it is really of much importance, but which is seen more often?
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Jun 30th, 2005, 3:32 PM   #6
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
Quote:
Originally Posted by stevengs
regardless of spelling, is "imputed" even a word?!?
well typically 'input' is used as a noun but I think the computer age has brought about its use as a transitive verb. I believe it is correct (although slang-like) to say 'inputted'

I just use 'entered' to dodge the issue.
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand."
- B. Russell

http://web.bryant.edu/~srk2
skuinders is offline   Reply With Quote
Old Jul 1st, 2005, 8:02 AM   #7
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by stevengs
You know, I generally prefer to list my attributes (private) first too, but every member of the crew at my new place of work unanimously agrees that the methods should be listed first (they are quite nitpicky when it comes to things like that.. sheesh). Not that it is really of much importance, but which is seen more often?
Usually I see public members (and attributes, if any) placed first in a header. The usual reason given is that private/protected members are implementation details, while public members are what is necessary to use the class. And, if more than one person uses the class, it is better to place what they need near the top of the header rather than forcing someone to scroll down through implementation details they aren't interested in.

People who care about the ability of customers to use their class library are often notable for insisting that public members appear near the top of a class declaration. That probably means that people who prefer private members near the top are more concerned with making it easier to modify the class implementation, and less concerned about the impact that has on other people who use their classes.
grumpy is offline   Reply With Quote
Old Jun 30th, 2005, 8:10 PM   #8
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by DaWei
class className 
{
private:
   ...constructors/destructors, often default
   ....private members/methods/declarations
public:
   ...public members/methods/declarations
};
I usually put public above private, so if you do that a lot of the time then constructors and destructors can go at the top.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Jul 1st, 2005, 12:56 AM   #9
AbalahDoon
Newbie
 
Join Date: Jun 2005
Posts: 2
Rep Power: 0 AbalahDoon is on a distinguished road
hey everyone thanks for all the help, great to get so many replys so quickly
AbalahDoon is offline   Reply With Quote
Old Jun 30th, 2005, 7:12 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
"Basically", I wouldn't "utilize" it that way. Those are two of my least favorite words -- one because it's overused and the other because it's worthless. Essentially, I don't use either of them. I feel much better now that I have inputted that :p .
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC