![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Posts: 2
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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
};
__________________
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 |
|
|
|
|
|
#3 | |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4
![]() |
Quote:
__________________
"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 |
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#6 | |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4
![]() |
Quote:
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 |
|
|
|
|
|
|
#7 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Quote:
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. |
|
|
|
|
|
|
#8 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#9 |
|
Newbie
Join Date: Jun 2005
Posts: 2
Rep Power: 0
![]() |
hey everyone thanks for all the help, great to get so many replys so quickly
|
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
"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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|